Q. How can I compress a whole directory under Linux / UNIX using a shell prompt?
A. It is very easy to compress a Whole Linux/UNIX directory. It is useful to backup files, email all files, or even to send software you have created to friends. Technically, it is called as a compressed archive. GNU tar command is best for this work. It can be use on remote Linux or UNIX server. It does two things for you:
=> Create the archive
=> Compress the archive
You need to use tar command as follows (syntax of tar command):
tar -zcvf archive-name.tar.gz directory-name
Where,
- -z: Compress archive using gzip program
- -c: Create archive
- -v: Verbose i.e display progress while creating archive
- -f: Archive File name
For example, you have directory called /home/jerry/prog and you would like to compress this directory then you can type tar command as follows:
$ tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog
Above command will create an archive file called prog-1-jan-2005.tar.gz in current directory. If you wish to restore your archive then you need to use following command (it will extract all files in current directory):
$ tar -zxvf prog-1-jan-2005.tar.gz
Where,
- -x: Extract files
If you wish to extract files in particular directory, for example in /tmp then you need to use following command:
$ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
$ cd /tmp
$ ls -
See also:
Featured articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins
- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
Excellent !
Step by step tutorial, easy to understand.
Thanks
Alpesh
Very excellent!
That is best guideline
Thank you!!!
yeah, it worked alright. :p it tarballed the directory and every file inside the directory into separate files
problem is, there are about 1k+ files
how do i have them all unzipped?
try: gunzip -rv /directoryname
use tar -zxvf file to untar them:
tar -xvvf file.tar
Can we compress and uncompress a file even without having the write permission to the source file or the zipped by merely having the execute permission.
The system I am running this command does not support tar -z. Hence I tried to do a tar followed by a gzip. Then there was a lack of space error. Is there a way to do this for me?
This is cool but I need to compress directory which has many folders and files within each other. Without knowing each folder name, can I zip the directory using your example?
To zip the files try:
zip prog.zip /home/jerry/prog/*
Mark,
The -r option recurse into directories,
just what I needed!! thanks for putting this up..
Superb..!
easy to understand..
thanks for your guideline.
how to list the file under the tar.gz?
I have try tar -tf
but fail and come out a message “This does not look like a tar archive”
THANKS! :)
Hey Man its really gr8
Thanks for the help
This is an excellent tutorial! Simple and easy to understand. Cool!
Very usefull been a long time since I done this had to refresh and this was perfect thanks
thanks..
Thanks, its really very helpful!
Thank you very much.
Truly very helpful.
Thanks for compress in linux tutorial.
Hiii
Thanks for ur excellent help it did a lot for meeeeeeeeeeeeeee
very good
can u give how to compress detail directory
HI Excellent Tutorial .
i Have one question, Is it possible to gzip all the files in a directory and remove all the files once we gziped is finished.
Hope u got my point ?
Wowwwww really simple and efficient !!!!!!!!!!!!! Thanks for the useful thing
thanks
Thanks, easy to understand and excellent guide.
Thanks, this is good, but usaully when i compress an archive i want also crypt it using a password. Could you help me to do it??????????????? :-)
Excellent tutorial !!!
Hi, I have to compress multiple dir within a dir and i used tar -czf option.. it created tgz file, but it doesnt remove the original directories.. Is there any way or option in zip/compress/archive to remove the original files after compression.. thanks
Nice article
Thanks
THANK YOU, this saved my first Minecrafts world. I was playing around with compiz and trying to make my windows slightly transparent like you see people have in videos. But forgot to turn the setting down from 100% or 0% or whatever before setting the window type to “any”. so then every single window dissappeared and I couldn’t see even an outline or shadow. AHHHGGGG!!!!! Luckily the only thing I actually cared about on that system was the minecraft world I’d been playing on. So I was able to open a terminal, type these commands in blind, then do a move command to get the new “saves.tar.gz” into my dropbox folder.
This post just turned a annoyed and ticked off night, into a “Hallelujah, THANK YOU GOD” kind of night lol
Thanks to Vivek Gite also though, and to all the other Linux/Unix/PC in general Pro’s out there that put these tid-bits of info on the net. They save absent minded people like me all the time!!! :-P
God Bless ya’s,
Daniel
Thanks for helping out – very straightforward.
Well done.
Mike
Nice clear and concise tutorial !! Well done
Regards,
Matthew
Thanks a ton :)
@Matthew Cupples:
try: gunzip -rv /directoryname
Thanks, man!
Very useful
Thank you