Many meta-characters such as semicolons, spaces, backslashes, dollar signs, question marks, and asterisks are characters that are interpreted under Linux and Unix-like operating systems as shell commands. For example, - or -- (dash or hyphen) interpreted as an option passed to the command. Try the following suggestions for moving (mv) or copying (cp) these files. These options are not limited to the mv command or cp command. Any commands can use the tips when handling a filename starting with a dash (- or --).
Move File Starting With A Dash
The syntax is as follows:
cp -- source dest cp [option] -- source dest
We use “--” to make Linux and Unix command stop parsing shell command line options. Now, let us see some examples.
Create a test file
Type the following command:
cd /tmp/ > '-foo.txt' > '--bar.txt'
List files starting with a dash
Try to list them, enter:
ls -l *.txt
You will get an error as follows:
ls: unrecognized option '--bar.txt' Try `ls --help' for more information.
To delimit the option list use -- i.e. type the following ls command:
ls -l -- *.txt
The cp and mv commands
When a file whose name begins with - or --, copy or move files using the following syntax:
cp -- '--bar.txt' /path/to/dest cp -- '-test.doc' /home/vivek/backups/test.doc
OR
cp -v -- '--bar.txt' /path/to/dest
To move files:
mv -- '--bar.txt' /path/to/dest
OR
mv -v -- '--bar.txt' /path/to/dest
Unix and Linux copy file starting with a dash
The - or -- considered as part of command line options. Therefore, you can not copy, list, delete or move any files starting with those characters. In short, the syntax is as follows:
cp options -- '--filename' /dest mv options -- '--filename' /dest
The -- delimit the option list. Later arguments, if any, are treated a operands even if they begin with - or —. This applies to all Linux/Unix/macOS/*BSD commands such as rm, cp, mv, ls, ln and so on:
command -- 'file' command [options] -- 'file' rm -- '--filename' rm -fr -- '-dirname' rmdir -- '--dirname'
How to use find command for same purpose
The syntax is:
find /dir/to/search -maxdepth 1 -name '--filename' -delete
See GNU find man page here.
Tip: Handling a filename starting with a dash (-)
You can “hide” the dash from the command by starting the filename with ./ (dot slash). For example, try to remove a file named “-filename.txt” using rm command:
> '-filename' rm -filename # will get an error # rm ./-filename
Conclusion
The -- is technically known as delimiting the options list. Later arguments, if any, are treated as operands even if they begin with - and syntax is:
command -- -foo
command -- -bar
command -arg1 -option2 -- -foobar
## sort will reads from the file named '-filename' ##
sort -- -filename
## grep command will find matching string named '--dom' ##
virsh --help | grep --color -- '--dom'
This page explained how to handle a Linux or Unix filename starting with a dash (-). The trick is to either start the filename with ./ or pass the -- before filename.
🐧 14 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
touch — ‘-foo.txt’ in case you’re getting error (in Bash) :)
@Vivek – You are right, Zdenek, [touch] code is wrong in Bash, without [–] end-of-option marker, you cannot use
touch '--DoubleDashName'
touch '-OneDashName'
or you could alternatively use [current directory prefix]:
reading manual [man rm] gives you two tips : second one is using [./] prefix.
touch ./-YouCantRemoveMeSimply
rm ./-YouCantRemoveMeSimply
@ Zdenek: I’ve now used > without giving out any hint about ‘–‘ to new users.
@Philippe: :)
What about files that start with a tilde ‘~’? In particular say someone has happily created a single file called ‘~’ and you have to delete it?
>'~'
rm '~'
Or use inode to delete the same.
@Vivek regarding CurrentDirectoryPrefix
Why don’t you add this tip ( ./ ) , like for many other topics where you give more than one way to do it (You usually seem to have the “There Is More Than One Way To Do It (TIMTOWTDI)” spirit ! :-) )
@Vivek regarding inode, you may have a little more than that to say, don’t you ? (explain find )
Anyway, this topic is a subtopic of the general need to deal with a file which name is difficult to handle, do you feel like creating it someday? (not only dash, but star, etc)?
The easy way to remove /home/you/–filename.txt is:
rm /home/you/--filename.txt
Thanks. I was having trouble moving files.
Thanks for taking the time to explain this.
Charles
# rm -rf ./\-1931673251/
I had issue trying to delete a file named ‘-d’, but for some odd reasons, the above suggestions didn’t work for me, giving me the No such file or directory. What I then tried was using the vim command on the directory the file is on (bringing up vim’s file navigation) and was able to visually select the file and delete it that way.
Yeah. LOL. Well, this is simply a bug that will never get fixed, because we know about it. The command reads the output as part of the command PROBLEM SOLVED NEXT PROBLEM PLEASE
didnt worked solved my giving full path to access cat ./-filename
Living and learning. Thank you!