简体   繁体   中英

How do I recursively visit and delete all files in a particular folder which has spaces in sh?

I am currently using

p=` ls -l -p $MYDIR | egrep '^d' | awk '{print $9}' 

for getting all the folders and then

for dirs in ${p}
do

for recursively opening the folders. It works fine for folder name without spaces, but for folder names with spaces, the second part of the folder name is selected as a seperate folder.

To iterate over all directories under $MYDIR ,

 find "$MYDIR" -type d |
 while read dir; do
    printf '%s\n' "Deleting files in <$dir>"
    rm -f "$dir"/*
 done

Note that you must double quote the dir variable when using it to prevent the shell from performing word-splitting at spaces.

Skipping $MYDIR if you don't need it left as an exercise.

You can use:-

find /opt/test  -type d ! -name "test" -exec echo rm -rf \"{}\" \; | sh

or

find -type d ! -name "." -exec echo rm -rf \"{}\" \; | sh

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