Linux/UNIX: Move File Starting With A Dash

by on February 20, 2010 · 13 comments· LAST UPDATED February 21, 2010

in

In Unix or Linux operating systems, how do I move file(s) starting with a dash (e.g., /home/you/--filename.txt or /home/you/-filename.txt)?

Many meta-characters such as semicolons, spaces, backslashes, dollar signs, question marks, and asterisks are characters that are interpreted under Unix as commands. - or -- interpreted as an option passed to the command. Try the following suggestions for moving (mv) or copying (cp) these files.

Create 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 command:

ls -l -- *.txt

cp and mv commands

Same can be used to copy or move files:

cp -- '--bar.txt' /path/to/dest

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

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 commands such as rm, cp, mv, ls, ln and so on:

 command -- 'file'
command [options] -- 'file'
rm -- '--filename'
rm -fr -- '-dirname'
rmdir -- '--dirname'
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 Zdenek Styblik February 21, 2010 at 11:21 am

touch — ‘-foo.txt’ in case you’re getting error (in Bash) :)

Reply

2 Philippe Petrinko February 21, 2010 at 11:56 am

@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

Reply

3 nixCraft February 21, 2010 at 1:37 pm

@ Zdenek: I’ve now used > without giving out any hint about ‘–‘ to new users.

@Philippe: :)

Reply

4 Daniel Morgan February 21, 2010 at 2:03 pm

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?

Reply

5 nixCraft February 21, 2010 at 2:11 pm


>'~'
rm '~'

Or use inode to delete the same.

Reply

6 Philippe Petrinko February 21, 2010 at 2:43 pm

@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)?

Reply

7 Chris F.A. Johnson February 21, 2010 at 4:02 pm

The easy way to remove /home/you/–filename.txt is:

rm /home/you/--filename.txt

Reply

8 xxerin August 28, 2011 at 10:07 am

Thanks. I was having trouble moving files.

Reply

9 Charles Marketing February 3, 2012 at 2:51 pm

Thanks for taking the time to explain this.

Charles

Reply

10 a May 15, 2013 at 6:06 am

# rm -rf ./\-1931673251/

Reply

11 Ricky September 4, 2013 at 5:31 pm

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.

Reply

12 Wastrel May 8, 2014 at 5:25 am

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

Reply

13 Srikanth October 25, 2014 at 3:51 am

didnt worked solved my giving full path to access cat ./-filename

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: