简体   繁体   中英

How to remove all subdirectories? (unix shell scripting)

I have a directory called "cdrs-roaming". Everyday I receive one or more.zip files and unzip them with this:

#!/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"; "example2.zip" and "example2"

I'm removing all zip files (in this case: "example1.zip" and "example2.zip") with this:

#! /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". I've tried this:

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

But it also removes "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

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)

Since you are using bash, how about

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

The final slash ensures that bash only expands the pattern to directories.

Use -mindepth 1 option:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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