Solved

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

Posted on 2014-01-28
5
192 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 29

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 29

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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