Solved

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

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

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

939 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

11 Experts available now in Live!

Get 1:1 Help Now