Solved

Bash script needed to zip all subdirs

Posted on 2003-12-11
7
466 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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