Search code examples
linuxunixspecial-characters

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 ?


Solution

  • 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.