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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
Method 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 {} ;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0