Super Quick Mini Post: Removing Weird Files in Linux

For the past few months, there has been a file in my home folder called ???. Deleting it with a file manager like Nautilus or Dolphin didn’t work, and “rm \?\?\?” didn’t work either. I finally deleted it, and in this Super Quick Mini Post, I will tell you how.

The reason “rm \?\?\?” didn’t work is that the file isn’t actually called ???; it’s name contains characters that are not contained in the terminal’s font, so it doesn’t know how to display them. So the easy way to delete them is with regular expressions. All you have to do is use sed to make rm ignore all files with normal characters. In my case, because there were no normal characters in the filename, this was easy. All I had to do was “rm $(ls | sed -e ‘s/[^a-zA-Z].*//’)” less the enclosing double quotes. If you’re familiar with regular expressions, you can probably see what this does; it’s fairly straightforward. In English, it says delete the following: list the directory, and remove all characters in lines that start with any letter between a and z or between A and Z. If you just run the commands inside the brackets, you’ll get a bunch of lines of nothing (which won’t do anything to rm) and the file you’re looking for. If the weird file contains alphanumeric characters, then you’ll probably have to modify the regular expression to suit the file exactly. So if your file is called “doof?s”, then you can do this “rm $(ls | sed -e ‘s/doof.s//’)” but that will delete any files starting with “doof” and ending with “s”.

I’ll write another post soon explaining fun things like where I’ve been for the past week, but I wanted to get this post out there for people experiencing this bizarre problem, and to show how useful and amazing regular expressions are. They’re one of the lesser known tools that would make many people’s computational lives so much simpler.

Note: Windows will not let you name files with characters it can’t hondle. However, I don’t believe it will prevent you from writing a program that accidentally creates files with unacceptable names, it can’t prevent people from naming files weirdly on removable media, and I’m almost positive that it can’t delete a file named using weird characters. That’s not a criticism of Windows, but simply an attempt to prevent people from saying “SEE! LINUX IS STOOPYD! U can’t do that in WINDOWS!! NERD!”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s