Score:8

error when trying to mv a file with -r in the name of the file

in flag

Here is my problem :

I have a file with -r in the name: -r.jpg

When I tried to do a mv *.jpg old/ I get:

mv: invalid option -- 'r'

Even for

for g in *.jpg;do echo $g &&mv "${g}" old/;done

I have the same error.

paladin avatar
id flag
Use `mv -- *.jpg old/`, `--` disables all additional "parameters" after `--`.
pl flag
It's a good thing that you didn't try to `rm` a file that includes `-rf` ...
Score:16
kz flag

Most command-line utilities have a provision to separate options from arguments.

That prevents a multitude of problems with "special" file names that look like options.

The most common separator indication the end of the options is two hyphens -- or -- which is also supported by mv.

In other words:

 mv [OPTIONS] -- SOURCE DEST

or since you didn't specify any options:

 mv -- *.jpg old/
Score:2
cn flag

The simplest solution would be to prepend the filename with the ./ describing the relative path

for g in *.jpg; do echo $g && mv "./${g}" old/; done

It is also possible to use the absolute path

for g in $(pwd)/*.jpg; do echo $g && mv $g old/; done

Alternatively, you may want to use the find command as it already provides the relative path ( you can use the --max-depth if you want to process only the current directory )

for g in $(find . -name "*jpg"); do mv $g old; done

And in the worst case whereby you have many complex un-parsable characters, you can refer to the inode numbers combined with the find -inum and process your files accordingly

 $ for g in $(ls -i1 ./*jpg | cut -f1 -d' '); do find . -inum $g -print0 | xargs -0 -I '{}' mv '{}' old; done
in flag
using "./" is a clever hint ; and the inum hint a very good solution :)
iBug avatar
um flag
There's literally no "unparsable" characters: All you need to do is to quote variables properly. In most cases a pair of double quotes suffices.
Peter Cordes avatar
ke flag
`mv ./*.jpg old/` would work; you don't need to loop one file at a time.
Peter Cordes avatar
ke flag
@Bussiere: The `find -inum` hack is not particularly useful unless you want to find hard links. `find . -maxdepth 1 -name '*.jpg' -print0 | xargs -0 mv -t old/` or something like that will do the same thing, and eventually run `mv -t old ./-r.jpg`, like this command. (Possibly with multiple other filenames on the same command line). It just wastes a lot of time starting `find` to scan the whole subtree for files with that inode number for each input file, then printing the filename. It's *not* running `mv` on the inode number; Unix system-calls can't do that.
Peter Cordes avatar
ke flag
@Bussiere: The key thing that makes it work (`./*.jpg` instead of `*.jpg`) is already used on the `ls` command line to avoid running `ls -r.jpg`, the rest is just wasted effort. I can't picture a case where having find look for an inode number is better than just using find's own filename matching like `-iname` or `-path`, and ideally using `-exec mv -t old/ {} +` to avoid even needing xargs.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.