如何仅删除目录并保持文件不变
我在一个目录中有数百个目录和文件。
仅删除目录的最佳方法是什么(无论目录中是否有任何内容,只需将它们全部删除)
目前我使用 ls -1 -d */ ,并将它们记录在文件中,执行sed
,然后运行它。路途相当遥远。我正在寻找仅删除目录的更好方法
I have hundreds of directories and files in one directory.
What is the best way deleting only directories (no matter if the directories have anything in it or not, just delete them all)
Currently I use ls -1 -d */
, and record them in a file, and do sed
, and then run it. It rather long way. I'm looking for better way deleting only directories
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要删除所有目录和子目录并仅在工作目录中保留文件,我发现这个简洁的命令对我有用:
它使用 bash 通配符
*/
,其中星号后跟斜杠将仅匹配目录和子目录。To delete all directories and subdirectories and leave only files in the working directory, I have found this concise command works for me:
It makes use of bash wildcard
*/
where star followed by slash will match only directories and subdirectories.<代码>查找 . -max深度 1 -min深度 1 -输入 d
然后
find 。 -max深度 1 -min深度 1 -type d -exec rm -rf '{}' \;
添加说明:
find
由于而从当前目录开始。-maxdepth
和-mindepth
均设置为1
时,才会保留在当前目录中。-type d
告诉find
仅匹配目录。find
还有一个-exec
标志,可以将其结果传递给另一个函数,在本例中为rm
。'{}' \;
是传递这些结果的方式。 有关{}
和\;
功能的更完整说明,请参阅此答案find . -maxdepth 1 -mindepth 1 -type d
then
find . -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \;
To add an explanation:
find
starts in the current directory due to.
and stays within the current directory only with-maxdepth
and-mindepth
both set to1
.-type d
tellsfind
to only match on things that are directories.find
also has an-exec
flag that can pass its results to another function, in this caserm
. the'{}' \;
is the way these results are passed. See this answer for a more complete explanation of what{}
and\;
do首先,运行:
以确保输出看起来正常,然后:
-type d
仅查找目录,然后-d
确保将子目录放在父目录之前。First, run:
to make sure the output looks sane, then:
-type d
looks only for directories, then-d
makes sure to put child directories before the parent.简单的方法:-
Simple way :-
rm -R $(ls -1 -d */ | grep .. )
rm -R $(ls -1 -d */ | grep .. )
仅 find 命令(支持文件删除)\
-type d 仅查找目录,然后 -深度 确保将子目录放在父母。 -delete 删除过滤的文件/文件夹
find command only (it support file deletion)\
-type d looks only for directories, then -depth makes sure to put child directories before the parent. -delete removing filtered files/folders
我通过添加一种保留一个文件夹的方法来做到这一点。我在子文件夹中执行命令以删除父文件夹中的文件夹。它不适用于带有空格的目录名称:
父文件夹=>清洁系统
subfolder_to_be_preserved =>; Linux
要删除的子文件夹=>窗口
这适用于带有空格的文件夹名称:
<前><代码>查找../。 -type d -name "* *" -execdir bash -c 'mv "$1" "${1// /_}"' _ {} \; 2>/dev/null;rm -r $(ls -1 -d ../*/ | grep -v Linux);
警告!:小心。如果你拼错 Linux,所有内容都会被删除。
I have done this, by adding a way to preserve one folder. I execute the command in a subfolder for removing folders in the parent one. It doesn't work for directory names with spaces:
parent folder => CleanTheSystem
subfolder_to_be_preserved => Linux
subfolder_to_delete => Windows
And this works for folder names with spaces:
Warning!: Be careful. If you misspell Linux, everything will be erased.
一行:(
反引号)
In one line:
(backquotes)