Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bash script needed to zip all subdirs

Posted on 2003-12-11
7
Medium Priority
?
478 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
[X]
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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

618 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