• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

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
0
vlg
Asked:
vlg
1 Solution
 
liddlerCommented:
something like (untested):

for dir in `find /home/fred -type d`
do
cd $dir
zip -r $dir.zip `ls -D .`
done
0
 
yuzhCommented:
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

0
 
paullamhkgCommented:
I will go for yuzh suggestion, tar and gz is zip the file and compress it, and you can also do with a directory :)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
vlgAuthor Commented:
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!
0
 
liddlerCommented:
To find all  files in this directory:

find . -name "*" -prune  -type f
0
 
yuzhCommented:
Winzip, or the compress utilities comes with Window XP can handle the *.tar.gz file.

0
 
yuzhCommented:
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!

0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now