Delete all files starting with a question mark

17,128

Solution 1

No need for any fancy stuff. Simply escape the ? so that it's not considered part of the glob:

rm -f ./\?*

This works for ! too:

rm -f ./\!*

Or in one fell swoop:

rm -f ./{\?,\!}*

Update

Just noticed that you were suggesting to grep the output of ls. I wanted to bring your attention to the fact that you shouldn't parse the output of ls

Solution 2

In my case, the characters were not really question marks, but unicode characters that apparently could not be displayed in my console.

Using rm -i * worked for me. If you don't want to do this, you can also delete by inode, as described at http://www.cyberciti.biz/tips/delete-remove-files-with-inode-number.html.

To find the inode, use:

ls -il

Then do:

find . -inum [inode-number] -exec rm -i {} \;
Share:
17,128

Related videos on Youtube

recluze
Author by

recluze

Systems/graphics designer and perpetual student. See more on the blog.

Updated on September 18, 2022

Comments

  • recluze
    recluze almost 2 years

    I have a folder in which I have around 4k files. Some of these files start with a a ? or ! character. I need to delete them but can't find an expression that would do so:

    rm -f ./?*

    just deletes everything. I can possibly use grep on ls and pipe it through xargs and move files to another folder but I was hoping there was a proper way of doing this. Need help on both the ? and ! files.

  • user
    user almost 11 years
    @recluze Not stupid; it's far from obvious how to do it if you don't know about wildcard escaping.
  • Jeff Hewitt
    Jeff Hewitt almost 11 years
    @recluze Still feel stupid after 5 votes on your question? :) By the way, see the updated answer.
  • recluze
    recluze almost 11 years
    Thanks and thanks :) ... Was aware of the problems in ls output but my files had a particular pattern that I could count on. Nevertheless, good to know :)