Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Bash script needed to zip all subdirs

Posted on 2003-12-11
7
463 Views
Last Modified: 2013-12-16
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
Comment
Question by:vlg
7 Comments
 
LVL 18

Expert Comment

by:liddler
ID: 9920855
something like (untested):

for dir in `find /home/fred -type d`
do
cd $dir
zip -r $dir.zip `ls -D .`
done
0
 
LVL 38

Expert Comment

by:yuzh
ID: 9925543
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
 
LVL 12

Expert Comment

by:paullamhkg
ID: 9925833
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:vlg
ID: 9928036
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
 
LVL 18

Accepted Solution

by:
liddler earned 125 total points
ID: 9928115
To find all  files in this directory:

find . -name "*" -prune  -type f
0
 
LVL 38

Expert Comment

by:yuzh
ID: 9928327
Winzip, or the compress utilities comes with Window XP can handle the *.tar.gz file.

0
 
LVL 38

Expert Comment

by:yuzh
ID: 9962384
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question