Move a File Whose Name Starts with a Dash Using mv --
Move a file whose name starts with a dash using mv -- or a ./ prefix. Without either, mv reads the name as an option, as it does in coreutils 9.1.
A script loops over file names, and one of them is called -v.
touch -- -v keep
f=-v
mv "$f" renamed
mv -- "$f" renamed && echo "moved with --"
ls
touch ./-v
mv ./-v renamed2 && echo "moved with ./"
lsmv: missing destination file operand after 'renamed'
Try 'mv --help' for more information.
moved with --
keep
renamed
moved with ./
keep
renamed
renamed2Utilities read every word that starts with a dash as an option until they meet --, which ends option parsing, so mv -v renamed is a complaint about a missing destination. The first command in the run shows that error, and both mv -- "$f" renamed and mv ./-v renamed2 moved the file. The ./ prefix works with any program, including those that do not accept --. The convention is in the Utility Syntax Guidelines.
A glob such as * expands a name like -v as it is, so put -- or ./* in front of the pattern in a script. A few programs do not support --, and for them ./ is the only fix.