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

StackOverflow https://stackoverflow.com/questions/22864687

  •  27-06-2023
  •  | 
  •  

문제

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top