Linux 如何在 UNIX 上删除带有特殊字符的目录名和带有 env 变量名的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11027566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:50:40  来源:igfitidea点击:

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

linuxunixspecial-characters

提问by dicaprio

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 $HOMEthen all the files and subdir's under $HOME which is current directory will be deleted . How do I delete these unwanted directories.

我在我的客户端环境中遇到了这个问题,一个自定义应用程序正在创建带有环境变量“$SRCDIR”和“$HOME”的目录以及创建这些目录的目录,它本身就是 HOME 目录路径。如果我说,rm -rf $HOME那么当前目录 $HOME 下的所有文件和子目录都将被删除。如何删除这些不需要的目录。

-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 ?

如何删除它们?

采纳答案by lanzz

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

对于垃圾名称,最容易构建一个只能捕获它们的通配符。选择名称的可读部分(例如_6子字符串)并将其用星号括起来。先试试看:

ls *_6*

If it lists onlythe 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.

继续操作,直到找到仅匹配不需要的文件的通配符。

回答by Pavan Manjunath

Just escape the dollar symbol. Try like this-

只是逃避美元符号。试试这样-

rm -rf $HOME

Same with even the ?symbol.

甚至?符号也一样。

回答by rosco

You may use a cprogram to avoid shell/escape problems. Look at the answers to this question.

您可以使用c程序来避免 shell/escape 问题。看看这个问题的答案。

回答by Hyman

try zsh shell in linux

在 linux 中尝试 zsh shell

[root@rhel5-8 ~]# zsh
[root@rhel5-8]~

then use tab completion to remove that directory.

然后使用制表符完成删除该目录。

or if using bash

或者如果使用 bash

[root@rhel5-8 ~]# cd "??????w?O???*????_6??t??ó??>?tP????|?C???????>?ì¤-???y?X???N?x??H?????D§)?n?5????{@?~]?" 

to get into that directory

进入那个目录

回答by edib

if you create a directory like --rw-r-r--. to delete this file do the following rm -rf ./--rw-r-r--

如果你创建一个像--rw-rr--这样的目录。要删除此文件,请执行以下操作 rm -rf ./--rw-r-r--

回答by a_secenthusiast

You can try rm -rf '$HOME' '$SRCDIR' , as the single quotes would prevent the expansion of the shell variables. tried with bash and ksh.

您可以尝试 rm -rf '$HOME' '$SRCDIR' ,因为单引号会阻止 shell 变量的扩展。尝试使用 bash 和 ksh。

回答by Oleg Neumyvakin

Determine inode numbers of necessary files/folders:

确定必要文件/文件夹的 inode 编号:

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

And pipeline it to rm:

并将其输送到rm

find . -inum 14549980  -exec rm -ir {} \;