0

In bash, I want to delete files starting with a #. I tried rm #*, but got message: rm: missing operand.

So how to achieve this?

6

The octothorpe (#), or pound sign, is a comment character, described in the POSIX grammar here as saying:

If the current character is a '#', it and all subsequent characters up to, but excluding, the next <newline> shall be discarded as a comment. The <newline> that ends the line is not considered part of the comment.

So you need to quote or escape the pound sign so that it is not interpreted as a comment:

rm '#'*

or

rm "#"*

or

rm \#*
3

While the # starts a comment, it only does so at the start of a word. So prefixing a path, like ./ for current directory, also works to disable it.

rm ./#*

Incidentally, prefixing ./ is also one of the two ways to prevent file names starting with a dash
(-like-this) being interpreted as options to the command.

3

If your shell is bash, you can disable the use of # as a comment character in an interactive shell with

shopt -u interactive_comments

With this shell option cleared,

rm #*

should work as you initially expected it, without the need to escape the #.

0

depending on the shell setup you might use

rm \#file

or

rm '#file'

or if it is a special character interpreted and displayed as # then

rm ?file

could work

  • All of those could not work, error: rm: cannot remove ‘#rm’: No such file or directory, rm: cannot remove ‘#rm’: No such file or directory, rm: cannot remove ‘?rm’: No such file or directory. By the way, I am using the cluster. – lanselibai Jan 19 '18 at 18:26
  • 1
    @lanselibai - he probably meant rm \#*... the point is, you only have to escape the # so that bash doesn't ignore it. – don_crissti Jan 19 '18 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.