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

x
?
Solved

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

Posted on 2014-01-28
5
Medium Priority
?
205 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
[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
5 Comments
 
LVL 30

Assisted Solution

by:serialband
serialband earned 1000 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 1000 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 30

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

636 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