5

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
amir jj
  • 1,212
  • 7
    Having such file/directory names would also urge me to first run fsck on the filesystem after having made sure I have a backup. – Ruslan Apr 23 '17 at 18:20
  • Do you need to examine the directory listing to determine which items should be deleted, or can you delete all items there? – can-ned_food Apr 24 '17 at 00:26
  • where's the blue directory? – phuclv Apr 24 '17 at 01:05
  • sorry, blue directories are those with junk characters like ?;%??; all of them I mistaken by saying blue – amir jj Apr 24 '17 at 09:21
  • actually it doesn't need fsck, I know the cause,there is a script which according to data make directory,It worked fine till the data encoding changed and I was a disaster @Ruslan – amir jj Apr 24 '17 at 09:22

2 Answers2

11

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?

Kusalananda
  • 333,661
4

An alternative method to easily remove directories and/or files named with non-printing characters, is to ascertain the inode number of the errant directories (or files) and using it, delete the directory (or file):

cd /mypath || exit 1
ls -il    #...to show the inode <number> in the leftmost column output
find . -xdev -inum <number> -exec rm -rf {} +

The -xdev option insures that find does not descend directories on other filesystems. Inode numbers are guaranteed to be unique only within a filesystem.

Add '-f' as an additional find option to return only files.

JRFerguson
  • 14,740