25

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.

2 Answers 2

26

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

3
  • 4
    @recluze Not stupid; it's far from obvious how to do it if you don't know about wildcard escaping.
    – user
    Commented Jul 13, 2013 at 11:41
  • @recluze Still feel stupid after 5 votes on your question? :) By the way, see the updated answer.
    – Joseph R.
    Commented Jul 13, 2013 at 21:16
  • 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 :)
    – recluze
    Commented Jul 14, 2013 at 5:51
24

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 {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .