How to delete undelatable files and folders in linux

There are filenames that are so weird that cannot be deleted by neither GNOME/KDE/etc. nor the rm shell command. This how to explains you different ways to cope with those files often dropped there simply by accident but sometimes actually being rootkits.

NOTE: This post takes for granted that the file or folder is not undelatable due to permission issues. Since linux has an advanced access permission schema please first check that you actually are allowed to delete that file. A quick way is to issue the command ls -l filename an example of access not granted is

dr-xr-xr-x 2 user user 4.0K 2009-01-01 00:00 MyFolder
-rw-r--r-- 1 root root 1G 2009-01-01 00:00 Archive.zip

In the first case notice the missing write access (w) for the user (it should have been drwxr-xr-x etc.) in the second case notice that although the write permission is granted it is granted only to the owner which is a different user (for instance root). In order to get the access rights use commands chown and chmod. Check their man pages for help.

man chown
man chmod

Here we deal only with undelatable files due to wrong character encoding and use of control characters or characters which cannot be typed on the keyboard or special character such as ?, * ^ etc. Problems with such files generally happen while working with NFS or SAMBA shares or while unpacking zip files created on other systems.

method 1 (files or directories)

For both files or directories with wired names  move all other files to a different location (not subfolder!) and type the following command. NOTE> this command deletes the current directory and all its subdirectories and all of its files regardless!

rm -rf *

method 2 (files)

use the \ character before the command rm

rm "\��i�+����+�����"

method 3

use the — (double dash) to tell program rm “end of switches”

rm -- "\+Xy \+\8"

method 4  (delete by inode)

Identify the inode number of the file or folder

ls -il filename

or

stat filename

than you could use the find command to remove the file

find . -xdev -inum inodenumber -exec rm -i {} \;

Method 5  (for FTP)

use the mdel program which would prompt to delete one by one all the files in the current directory eventually displaying the weired one.

mdel *

Method 6

If the filename has a first common character then type

rm ./"a

and press TAB for the autocompeltion. In the given example the filename starts with an a

Method 7

List the directory using octal representation. So you know the control characters in the name.

root@liam # ls -lb
total 14
drwxr-xr-x 2 root other 512 Dec 22 12:59 10ms\177

Now simply rm “Control V””Backspace”ms”Control V””Delete”

Where Control V is a key combination and Backspace is a key. As is Delete. In the above example 10 is BS (BackSpace) and \177 is Delete.

Having an ASCII table at hand is necessary. Typing

man ascii

is the most handy way to get it! (many thanks HSmade!)

Method 8

use the unlink function

unlink filename

Method 9

use debugfs rm command