mv command confuses directory name with command option

12

1

I have a directory called --pycache--, which I need to move to __pycache__. Using the mv command in the following way, gives me the listed output. How can I use the CLI to do what I want?

$ mv --pycache-- __pycache__
/bin/mv: unrecognized option '--pycache--/'

user81557

Posted 2011-11-26T05:30:28.220

Reputation: 223

Answers

22

This is a standard issue with filenames/directories starting with less conventional symbols. Your problem is that mv is treating --pycache-- as long option name (since it starts with --, there are also short options, they start with -). Please see manpage for getopt for details about long and short options.

The standard workaround in this situation is to use an empty double dash -- before all argument, which tells the command (mv in your case, but will work with others, cp for example) to stop treating what follows as options and treat it as arguments.

Thus, your command will become:

$ mv -- --pycache--/ __pycache__

and won't fail.

vtest

Posted 2011-11-26T05:30:28.220

Reputation: 4 424

8good answer but I think you mean -- tells the command to stop treating what follows as options and treat it as arguments, not the other way round. – RoundTower – 2011-11-26T12:24:59.280

1I think it's more portable to prepend ./: mv ./--pycache-- __pycache__. – Kevin – 2011-11-26T18:32:02.107

3@Kevin That works only if the argument is a file name. – starblue – 2011-11-26T18:42:23.140

16

Your first character - is ambiguous for the mv command (or rather, it unambiguously means that an option name follows).

Try this instead:

mv ./--ppycache-- __pycache__

Source: linux.about.com

yms

Posted 2011-11-26T05:30:28.220

Reputation: 708

2+1 for alternate solution that also works if the command does not support -- – Sverre Rabbelier – 2011-11-26T12:02:50.480