Linux – corrupted directories, problem in deleting them

character encodingdirectorylinuxrm

There is a directory in my Linux system because of some software malfunction some directories with junk name as you can see below is created, I have problem in deleting them,

$ ll
total 1532
drwxr-xr-x   2 sensage sensage   4096 Apr 19 16:56 -?$??
drwxrwxr-x 248 sensage sensage   4096 Apr 23 11:37 .
drwxrwxr-x  99 sensage sensage   4096 Apr 16 14:23 ..
drwxr-xr-x   2 sensage sensage   4096 Apr  6 14:54 }???;?
drwxr-xr-x   2 sensage sensage   4096 Apr 19 03:01 }??=?|
-rw-r--r--   1 sensage sensage     88 Apr 22 13:37 $
drwxr-xr-x   2 sensage sensage   4096 Apr  2 12:43 ?
drwxr-xr-x   2 sensage sensage   4096 Mar 20 02:51 ?=??&?
drwxr-xr-x   2 sensage sensage   4096 Apr 11 08:40 ?;%??;
drwxr-xr-x   2 sensage sensage   4096 Apr 14 09:38 ?:????
drwxr-xr-x   2 sensage sensage   4096 Mar 22 17:21 ?(?>~?
drwxr-xr-x   2 sensage sensage   4096 Apr  1 13:45 ?[???%
drwxr-xr-x   2 sensage sensage   4096 Apr  3 14:03 ?@????
drwxr-xr-x   2 sensage sensage   4096 Apr 12 16:18 ??
drwxr-xr-x   2 sensage sensage   4096 Apr 17 16:38 ??&???
drwxr-xr-x   2 sensage sensage   4096 Mar 25 02:43 ??+???
drwxr-xr-x   2 sensage sensage   4096 Apr 19 00:46 Ü¡?,??
drwxr-xr-x   2 sensage sensage   4096 Mar 28 18:54 ÚŸ??"?
drwxr-xr-x   2 sensage sensage   4096 Mar 27 01:04 ???(?
drwxr-xr-x   2 sensage sensage   4096 Apr 19 22:41 ??ͨ?`
drwxr-xr-x   2 sensage sensage   4096 Apr 15 11:44 ?????-

as you can see directory names in blue. when I want to delete them I get below error:

$ ls -1  | grep -v 20 | xargs rm -rf 
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
rm: invalid option -- ¼
Try `rm ./'-¼$Þ¸Í'' to remove the file `-\274$\336\270\315'.
Try `rm --help' for more information.

what should I do with them?

Best Answer

ls will print non-ASCII characters (or rather, characters not supported in the current locale) as ?. This is one of the reasons why parsing the output of ls is a bad thing to do. The output from ls is meant to be looked at. In some cases, like this, those are not the actual names that exist in the filesystem.

Try instead something like (these will delete all files and directories, including /path/to/dir)

rm -rf /path/to/dir

or

find /path/to/dir -delete

or

find /path/to/dir -exec rm -rf {} +

or

find /path/to/dir -print0 | xargs -0 rm -rf

Modify to fit your needs. To only delete files, add -type f after the path in the find examples, for example.

Doing just rm -rf * inside that directory (that's important, the current working directory must be the directory whose files and directories you want to delete) may also be enough.

See also Why not parse ls?

Related Question