Create multiple tar.gz archives from multiple directories
Without find
:
for dir in */; do tar -czvf "${dir%/}".tar.gz "$dir"; done
where */
makes sure that the glob only matches directories, and "${dir%/}"
removes the trailing slash from directory names.
If there are hidden directories, they're not matched by */
; do get those as well, we can use shopt -s dotglob
.
After some searching and a bit trial/error, I came up with this script to programmatically accomplish what I was trying to do.
#! /bin/sh for dir in `find . -maxdepth 1 -mindepth 1 -type d | sed 's|./||'` do tar -czvf /Path/To/Place/Created/Archives/$dir.tar.gz /Path/To/Directories/$dir; done
Going again off the example I gave in the question, the script would look like this assuming I wanted to create the archives in my Documents
folder.
#! /bin/sh for dir in `find . -maxdepth 1 -mindepth 1 -type d | sed 's|./||'` do tar -czvf /Users/Me/Documents/$dir.tar.gz /Users/Me/Projects/$dir; done
EDIT
After some further research, it seems that the answer given by Benjamin W. is the most straight forward way to do this. I was hoping to find a way which would strip spaces from the newly created archive names to avoid any possible issues on other file systems, and while the method I described does strip white space, it had a few snags I kept running into.