How to delete files or directories that contains special characters

root's picture

The other day I had this file (-rw-r--r--) created under strange and unknown circumstances. If you treat it as "normal" file, you will get error messages.
There are many ways to deal with such files/directories but the quickest way is to give the full path or the path from the current location. In other words, to put /full/path/ or ./ in front of it:

shell@gz:~$ ls -la
total 0
drwxr-xr-x    2 root     system         4096 Nov 03 09:00 .
drwxr-xr-x    4 root     system          256 Jul 19 14:04 ..
-rw-r--r---    1 root     system            0 Nov 02 16:30 -rw-r--r--
Fail:
shell@gz:~$ rm -f '-rw-r--r--'
rm: Not a recognized flag: w
Usage: rm [-firRe] [--] File...
shell@gz:~$ ls -la "-rw-r--r--"
ls: Not a recognized flag: w
ls: Not a recognized flag: -
ls: Not a recognized flag: -
ls: Not a recognized flag: -
ls: Not a recognized flag: -
ls: Not a recognized flag: -
Usage: ls [-1ACFHLNRSabcdefgiklmnopqrstuxEUX] [File...]
Success:
shell@gz:~$  ls -la ./-rw-r--r--
-rw-r--r--    1 root     system            0 Nov 02 16:30 ./-rw-r--r--
shell@gz:~$ rm ./-rw-r--r--
shell@gz:~$ ls -la ./-rw-r--r--
ls: 0653-341 The file ./-rw-r--r-- does not exist.

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content