Link to home
Start Free TrialLog in
Avatar of vlg
vlg

asked on

Bash script needed to zip all subdirs

Hello all

In a dir, call it /home/fred, I have many subdirs, many with subdirs themselves.

I'd like a bash script that gets all the dir names in /home/fred, and zips them into zipfiles named after the directory itself with a ".zip" at the end.

Something like ls -D | xargs zip -r somehow_get_the_dir_name.zip dir_name would work from the command line but I can't figure out how to pass the dir names to the zip command, so I'm guessing that some scripting is called for here.

Thanks!

v
Avatar of liddler
liddler
Flag of Ireland image

something like (untested):

for dir in `find /home/fred -type d`
do
cd $dir
zip -r $dir.zip `ls -D .`
done
Avatar of yuzh
yuzh

If you have GUN tar, it is better to do:

tar -czvf  backup.tar.gz `find /home/fred -type d`

If you want related path, use:

cd /home/fred
tar -czvf  backup.tar.gz `find . -type d`

To verify what's in the *.gz file:

tar -ztvf backup.tar.gz

To extract  files from the backup .gz file:
tar -zxvf backup.tar.gz

I will go for yuzh suggestion, tar and gz is zip the file and compress it, and you can also do with a directory :)
Avatar of vlg

ASKER

Hello

Yes, I'd love to use tar, but it will be decompressed by Windows people who have no tar, so I have to use the zip format.

liddler, your code is descending into the subdirectories in /home/fred -- can I make it not do that?  Just take any dir it finds in /home/fred, and zip it.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of liddler
liddler
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Winzip, or the compress utilities comes with Window XP can handle the *.tar.gz file.

So, the "find"command is the answer for you?

It doesn't make much sense to me for the question !

Ok, guys, Merry Xmas and Happy New Years, Cheers!