Solved

How would i gzip every direcory within a directory and then list its size

Posted on 2014-01-28
5
190 Views
Last Modified: 2014-02-01
Hi

Lets say i have my top level directory and these have subdirectories called dir1, dir2, dir3 etc. These subdirectories can in turn have subdirectories.

I would like a command which will gzip every directory in my current working directory. I am hoping this will zip any subdirectories therein. The name of the zipped top level directory can simply be the name of the original directory appended with .gz

I've seen this but this will zip and the directories and create a tar acrhive?
tar czvf mytar.tar.gz dir1 dir2 .. dirN

What if i just want to gzip the files and not create the tar file?
0
Comment
Question by:andieje
5 Comments
 
LVL 27

Assisted Solution

by:serialband
serialband earned 250 total points
ID: 39816123
I don't believe that gzip works on directories.  It's why we use tar with options to compress.  You should use tar for tar'ing up the directories and use the z option to gzip the archived directories.  Use j if you want to bzip them for better compression.

cd top_level;  for i in * ; do tar czf $i  $i.tgz; done

cd top_level;  for i in * ; do tar cjf $i  $i.tar.bz; done



If you want to gzip the files in every subdirectory individually, you could use find.

find . -name "*" -exec gzip {} \;


I think this will also delete the original file when the .gzipped version is created.

gzip * will gzip all the files in the current directory.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39816701
gzip only works on individual files.

If you want to create a compressed archive of directories and multiple files, then you need to either tar/compress, or use zip.
0
 
LVL 8

Accepted Solution

by:
Surrano earned 250 total points
ID: 39817090
addition to serialband's comment: tar first filename param should be the archive file  and the rest should be the files to archive. Dropping in some tricks to
- archive dirs only (not single files),
- including hidden ones
- removing the source, i.e. emulate what gzip does

for d in */ .[^.]*/; do 
  tar czf $d.tgz --remove-files $d
  ls -l $d.tgz
done

Open in new window

0
 
LVL 27

Expert Comment

by:serialband
ID: 39818204
@Surrano,

Thanks for catching that.  I typed those after a 12 hour work day doing an "after hours" server upgrade/transition.
0
 

Author Closing Comment

by:andieje
ID: 39826881
wonderfuol
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now