-1

I am trying to remove a set of multiple zipped directories in BASH. These directories have spaces between them, so when I try to delete them, they interfere with the spaces.

I have tried removing the directories by using a for loop.

 direc=`ls -d *.zip` 
 for eachDir in $direc 
 do
     echo $eachDir
     rm -r $eachDir
 done

Each word in my directory name is being treated as a seperate directory (because of the spaces) which causes an error in deleting the zipped file.

I have also tried using:

FILES=$(find . -name "*.zip")
for val in $FILES
do
        echo $val
done

From both these pieces of code, my result is the sameZipped Files

As seen from the photos, "Whos" "Wants" and "To"... are all seen as seperate files as space is causing this. How can I fix this?

Michael
  • 35
  • 5

2 Answers2

3

find will traverse all sub-directories and delete any *.zip files

find . -name \*.zip -delete
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

Maybe try the following snippet, using the internal field separator and find command:

while IFS=  read -r -d $'\n'; do
   echo "Remove $(basename "$REPLY")"
   rm $(basename "$REPLY")
done < <(find ./*.zip)


Edit: With find you can traverse through directories as well. For example, have a look at this post

agentsmith
  • 1,226
  • 1
  • 14
  • 27
  • If the directories are not empty, the script tries to delete the files from the subdirectories in the current directory. – Cyrus Aug 17 '19 at 19:12
  • Probably you are right, but this is only a snippet how it could be achieved. Of course, there is no checking. And with ```find```you can select *min-* and *maxdepth*. – agentsmith Aug 17 '19 at 20:13