File rename error

hi im new for unix, when i try to rename the file im getting error

$ mv -9file.jpg 9file.jpg

error is
mv: invalid option --"9"
Try 'mv --help' for more information.

Hi,

Try this one,

find . -name "-9file.jpg" | xargs -I '{}' mv {} 9file.jpg

Cheers,
Ranga:)

2 Likes

thanks rangarasan....

To keep it simple, just use

mv -- -9file.jpg 9file.jpg

The special option "--" can be used to delimit the end of the options. This is useful in delimiting non-option arguments that begin with "-" (quoted from the getopt-manpage).

4 Likes

oops, sorry, ignore..

Remember also that you can use backslash to avoid shell interpretation:

 mv ./\-9file.jpg 9file.jpg
2 Likes

as stated above - tells the command to expect options as it is a special character.
You need to remove the special meaning of this either by the backslash or by using quotes.

mv \-9file.jpg 9file.jpg
or
mv '-9file.jpg' 9file.jpg

1 Like

Both won't work, because they protect the - from interpretation by the shell, which is not the problem. The getopt function in the mv command will still interpret the leading - as an optionlist.

$ ls
-9file.jpg
$ mv \-9file.jpg 9file.jpg
mv: illegal option -- 9
mv: illegal option -- l
mv: illegal option -- e
mv: illegal option -- .
mv: illegal option -- j
mv: illegal option -- p
mv: illegal option -- g
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
       mv [-f] [-i] f1 ... fn d1
       mv [-f] [-i] d1 d2
$ mv '-9file.jpg' 9file.jpg
mv: illegal option -- 9
mv: illegal option -- l
mv: illegal option -- e
mv: illegal option -- .
mv: illegal option -- j
mv: illegal option -- p
mv: illegal option -- g
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
       mv [-f] [-i] f1 ... fn d1
       mv [-f] [-i] d1 d2
ant:/home/vbe $ mv \-9file.jpg 9file.jpg
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
       mv [-f] [-i] [-e warn|force|ignore] f1 ... fn d1
       mv [-f] [-i] [-e warn|force|ignore] d1 d2
ant:/home/vbe $ mv ./\-9file.jpg 9file.jpg
ant:/home/vbe $

Dash is not a special character, it does not need to be backslashed, never.

localhost kernel # echo -
-
localhost kernel # echo "-"
-
localhost kernel # echo '-'
-
localhost kernel # echo \-
-
localhost kernel # 

Stick with the -- solution. It's the official :slight_smile:

 
man mv
 
A -- permits the user to mark explicitly the end of any com-
     mand  line  options, allowing mv to recognize filename argu-
     ments that begin with a -. As an aid to  BSD  migration,  mv
     will  accept  -  as a synonym for --. This migration aid may
     disappear in a future release