Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've tried many commands already for creating a file and deleting a file with a # character, but it does not work. Can anyone tell me the command to create and then delete a file that begins with #?

share|improve this question

migrated from stackoverflow.com Apr 10 '11 at 14:03

This question came from our site for professional and enthusiast programmers.

2  
Note: You should say "a filename that begins with the symbol #", not a file, otherwise some people (like me) will think you mean the first line of the file should start with a #. –  Robin Green Apr 10 '11 at 13:45
add comment

2 Answers

The two canonical ways to create/delete files with "funny characters" are

  1. Quoting, like alex showed. You may use single or double quotes, depending on your expansion needs. A backslash can be used to escape a single funny charater. This works as long as the file name does not look like an option (starts with a dash).
  2. If the file looks like an option, prepend a path: rm ./- "./-rf ."

Modern versions of Unix utilities often support the double dash to indicate the end of options. On such systems, rm -- - removes a file named -.

Note that you cannot create or remove files with a slash or ASCII NUL in their name. If you have such a file (I've seen them), something in your file system has gone terribly wrong.

In your particular case with the hash #, the problem stems from the shell interpreting a word starting with # as starting a shell comment. A good shell lets you disable this shell feature, called interactive comments:

  • zsh: unsetopt interactivecomments
  • bash: shopt -u interactive_comments

With these you can simply touch #; rm # without hassle.

share|improve this answer
add comment

To make...

touch "#file"

To delete...

rm "#file"
share|improve this answer
2  
Also touch \#file or rm \#file –  lacqui Apr 10 '11 at 5:48
2  
or touch '#file' and rm '#file' –  Philipp Apr 10 '11 at 10:04
    
thanks for your helping. but if what to create file and delete file using "-" symbol why cannot already? –  tonberry Apr 10 '11 at 12:58
    
Because - introduces options. rm - looks to rm like you forgot an option and thus complains. –  Jens Apr 16 '11 at 8:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.