bash 在bash中删除给定文件夹中的所有目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8848237/
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
Deleting all directories in a given folder in bash
提问by Registered User
I am trying to write a shell script to delete all the sub directories in a given directory. I know there is an easy approach for the same. Like doing this
我正在尝试编写一个 shell 脚本来删除给定目录中的所有子目录。我知道有一个简单的方法。喜欢这样做
find ./ -type d -exec rm -r {} \;
but since I am learning shell scripting so I prefer to write a script for the same. Here is my approach
find ./ -type d -exec rm -r {} \;
但由于我正在学习 shell 脚本,所以我更喜欢为此编写一个脚本。这是我的方法
for i in `ls ./*`; do
if [ -d $i ];then
rm -r $i
fi
done
When I run this script this gives me following errors
当我运行这个脚本时,这给了我以下错误
rm: cannot remove directory: `.'
rm: 无法删除目录: `.'
after giving this error this stops.So what is the error in my approach.As far as I understand blank names should create some problem. But this script has failed to go that far.
给出这个错误后,这会停止。那么我的方法中的错误是什么。据我所知,空白名称应该会产生一些问题。但是这个脚本并没有走那么远。
回答by Richard
The ls ./* makes a list of all the files in each immediate subdir of . The -d then checks the name of the file but as if it was in . not the subdir it comes from.
ls ./* 列出了 ./* 的每个直接子目录中的所有文件。-d 然后检查文件的名称,但好像它在 . 不是它来自的子目录。
For example if you had:
例如,如果您有:
foo1/
bar
foo2/
baz
then ls ./* would make a list of bar and baz, as the ./* would match foo1 and foo2 and ls would then list the contents of each of those 2 directories.
然后 ls ./* 将列出 bar 和 baz,因为 ./* 将匹配 foo1 和 foo2,然后 ls 将列出这两个目录中的每一个的内容。
The error message you are getting is probably because your ls has been aliased to be 'ls -a' which lists . and .. As the answer by Florin says, you can use ls -A ./* to avoid that issue.
您收到的错误消息可能是因为您的 ls 已被别名为“ls -a”,其中列出了 . 和 .. 正如弗洛林的回答所说,你可以使用 ls -A ./* 来避免这个问题。
If you just want to delete the directories in ., just do:
如果您只想删除 . 中的目录,请执行以下操作:
for i in `ls -A`; do if [ -d $i ]; then rm -r $i; fi; done
回答by l0b0
You shouldn't use ls
in this command. There's a very simple way to make sure you only iterate over directories:
你不应该ls
在这个命令中使用。有一种非常简单的方法可以确保您只遍历目录:
for dir in */
do
echo "$dir"
done
回答by Florin Ghita
.
and ..
should not appear in the output of ls
.
.
并且..
不应出现在ls
.
However, you can test with
但是,您可以使用
ls -A
(-A means 'almost all'
(-A 表示“几乎所有”
-a means 'all')
-a 表示“全部”)
And: why you don't jus use for i in 'ls'
?
并且:你为什么不使用for i in 'ls'
?
回答by user1145202
The problem is that ls command list links for the current folder and the parent folder. So you must test if your i variable is setted to '.' or '..'
问题是 ls 命令列出了当前文件夹和父文件夹的链接。所以你必须测试你的 i 变量是否设置为 '.' 或者 '..'
if [ -d $i ] -a [ $i != '.' ] -a [ $i != '..' ]
then
rm -r $i
fi
回答by Fergie
"*/
" should match all subdirectories
" */
" 应该匹配所有子目录
Try something like this:
尝试这样的事情:
rm -rf /path/to/mySubfolder/*/
rm -rf /path/to/mySubfolder/*/