Home > Software design >  bash script to zip multiple subfolders in a directory
bash script to zip multiple subfolders in a directory

Time:10-14

I need to find resprective subfolders with a pattern and gzip each one separately. I have done the first leg but unsure how to zip them

$ find . \( -iname 'NE*' -type d \)
./NE1
./NE2
./NE3

I tried this but i dont think ive got it right?

 find . \( -iname 'NE*' -type d \) -exec tar -czvf NE*.tar.gz --verbose {} \;

CodePudding user response:

Another option (maybe it's more readable) is using xargs:

find . -iname 'NE*' -type d | xargs -I subdir -- tar -cvzf subdir.tar.gz subdir

CodePudding user response:

Try this :

find . \( -iname 'NE*' -type d \) -exec bash -c 'tar -czvf "$1.tar.gz" --verbose "$1" && rm -rf "$1"' - {} \;
  • Related