简体   繁体   English

如何删除所有子目录? (unix shell 脚本)

[英]How to remove all subdirectories? (unix shell scripting)

I have a directory called "cdrs-roaming".我有一个名为“cdrs-roaming”的目录。 Everyday I receive one or more.zip files and unzip them with this:每天我都会收到一个或多个.zip 文件并用这个解压缩它们:

#!/bin/bash
for i in *.zip
do
    j=${i//\.zip/}
    mkdir $j
    cd $j
    unzip  ../$i
    cd -
done 

Then I have for example: "example1.zip" and "example1";然后我有例如:“example1.zip”和“example1”; "example2.zip" and "example2" “example2.zip”和“example2”

I'm removing all zip files (in this case: "example1.zip" and "example2.zip") with this:我正在删除所有 zip 文件(在本例中为:“example1.zip”和“example2.zip”):

#! /bin/bash
find /dados/cdrs-roaming/*.zip -mtime +1 -exec rm {} \;

So I want to remove the directories (or folders - I really don't know the difference) "example1" and "example2".所以我想删除目录(或文件夹 - 我真的不知道区别)“example1”和“example2”。 I've tried this:我试过这个:

#! /bin/bash
find /dados/cdrs-roaming/ -type d -mtime +1 -exec rm -rf {} \;

But it also removes "cdrs-roaming".但它也删除了“cdrs-roaming”。 I've also tried to use:我也尝试过使用:

find /cdrs-roaming/ -type d -mtime +1 -exec rm -rf {} \;

But it returns: find: '/cdrs-roaming/': No such file or directory但它返回: find: '/cdrs-roaming/': No such file or directory

Any idea for doing this?有什么想法吗? I need to delete only the directories within "cdrs-roaming" but I can't remove anything else inside it (my.sh files are inside of it)我只需要删除“cdrs-roaming”中的目录,但我无法删除其中的任何其他内容(my.sh 文件在其中)

Since you are using bash, how about既然你用的是bash,怎么样

rm -rf /dados/cdrs-roaming/*/

The final slash ensures that bash only expands the pattern to directories.最后的斜线确保 bash 仅将模式扩展到目录。

Use -mindepth 1 option:使用-mindepth 1选项:

find /dados/cdrs-roaming/ -mindepth 1 -type d -mtime +1 -exec rm -rf {} \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM