How do I Compress a Whole Linux or UNIX Directory?
Originally by VIVEK GITE on FEBRUARY 11, 2006 ·
Modified by Droidzone
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: .tar.gz
- -j: Compress using bzip2: .tar.bz2
- -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[/code]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[/code]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 -[/code]
Qn:
I have a directory I need to backup using "tar" and/or "zip". I need
to exclude a directory from the archive. Backup script running on
Unix.Directory to archive: /home/
Directory to exclude: /home/cpanel/I need the solution for zip and tar.[/code]
Ans:zip -r backup.zip /home -x "/home/cpanel/*"tar -cf backup.tar /home --exclude "/home/cpanel" or[/code]
tar -cf backup.tar /home --exclude="/home/cpanel"[/code]
If you have a long list of directories to exclude, use:tar -cf backup.tar /home --exclude={android,blackberry,animated_favicon1.gif,build,build.xml,db,error.jsp,favicon1.ico}[/code]
Or you can have the list of directories in an exclude text file:--exclude-from=/PATH_TO/exclude.txt[/code]
Joel G Mathew, known in tech circles by the pseudonym Droidzone, is an opensource and programming enthusiast.
He is a full stack developer, whose favorite languages are currently Python and Vue.js. He is also fluent in Javascript, Flutter/Dart, Perl, PHP, SQL, C and bash shell scripting. He loves Linux, and can often be found tinkering with linux kernel code, and source code for GNU applications. He used to be an active developer on XDA forums, and his tinkered ROMS used to be very popular in the early 2000s.
His favorite pastime is grappling with GNU compilers, discovering newer Linux secrets, writing scripts, hacking roms, and programs (nothing illegal), reading, blogging. and testing out the latest gadgets.
When away from the tech world, Dr Joel G. Mathew is a practising ENT Surgeon, busy with surgeries and clinical practise.