Skip to content

How to escape “-” character at the beginning of filename?

How to escape "-" character at the beginning of filename?

Answer:

Assume you have file named -test.txt, if you want to print it out by the cat command:

# cat -test.txt
cat: invalid option -- '.'
Try `cat --help' for more information.

You will experienced the above error, similar errors will happen with commands such as mv, cp etc.

To correctly handle these kind of files that with file name start with the character -, you need to escape with --

e.g.

#cat -- -test.txt
test
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  1. xargs when the filename contains a newline
  2. Wide character in print… warning in Perl
  3. Character classes not working in grep command
  4. Remove newline character from the end of a string in Perl
  5. Capturing group difference in Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE)

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*