Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove directory name with special characters and directories with env variable name on UNIX

I did a search in this repository and I didn't find any similar questions or may be my search was incorrect.

I have this problem in my clients environment, a custom application is creating directories with an environmental variable "$SRCDIR" and "$HOME" and the dir's where these are created , itself is the HOME dir path. if I say rm -rf $HOME then all the files and subdir's under $HOME which is current directory will be deleted . How do I delete these unwanted directories.

-rw-r--r--  1 grp domain users 418051450 Apr 18 18:09 $SRCDIR
-rw-r--r--  1 grp domain users 418051450 Apr 18 18:09 $HOME

Also some directories are junk characters as below example.

-rwxr-xr-x  1 grp domain users  0 Feb  7  2106 ??????w?O???*????_6??t??Ó¡?>?tP??Ñ?|?C

How do I delete them ?

like image 216
dicaprio Avatar asked Jun 14 '12 06:06

dicaprio


2 Answers

For the junk names, it would be easiest to construct a wildcard that would catch only them. Select a readable portion of the name (e.g. the _6 substring) and wrap it in asterisks. First try it out:

ls *_6*

If it lists only the junk name, proceed to delete it:

rm *_6*

If it lists other names as well, try to make the wildcard more specific, using other readable characters in the name:

ls *w*_6*tP*N*x*

Proceed until you've found a wildcard that would match only unwanted files.

like image 113
lanzz Avatar answered Sep 20 '22 11:09

lanzz


Determine inode numbers of necessary files/folders:

# ls -ila 
14549980 drwxr-xr-x  3 root root       4096 Mar  5 20:45 ">?<

And pipeline it to rm:

find . -inum 14549980  -exec rm -ir {} \;
like image 21
Oleg Neumyvakin Avatar answered Sep 23 '22 11:09

Oleg Neumyvakin