This post shows how to compress and extract files in Linux. Most users use tar to compress files to save disk space or to save network bandwidth if they are transferring files over the network. Files are compressed before they are added to the archive.
The tar command is one of the most commonly used commands in Linux to compress files to create archives. Archives are usually created for backup or transferring files. The tar command uses compression utilities such as the gzip and bzip2 to compress files before adding them to the archive.
In this post, we will use two types of utilities to compress the files.
- tar – archive and extract files using tar command
- gzip – use gzip to compress and extract files. It compresses faster but provides a low compression ratio.
- bzip2 – use bzip2 to compress and extract files. It compresses slower but provides a high compression ratio.
- How to exclude directories and files using tar
- How to view stored files in an archive
1. Archive and extract files using tar command
To archive all the files in the folder –
tar -cvf filename.tar /path/to/dir/
To extract archive using tar command
tar -tvf filename.tar
eg. To archive all the files in the folder
[[email protected]]#ls ==> ls to view files in folder file1.yaml file2.txt
[[email protected]]# tar -cvf file.tar /root/linux/arch/ tar: Removing leading `/' from member names /root/ /root/file1.yaml /root/file2.txt
Process of Archiving files is completed and you can see the archived file with name file.tar by using ls command
[[email protected]]# ls file.tar file1.yaml file2.txt
2. Using gzip to compress and extract tar archives
To use the gzip utility to compress files before adding them to the archive, z will be used with the options cvf.
Use the extension .gz with the archive file name to indicate that the gzip compression is used.
Say you want to compress an entire directory named /home/networkhunt/data/:
$ tar -czvf file.tar.gz /home/networkhunt/data/
To compress multiple directories and files, execute:
$ tar -czvf file.tar.gz /home/networkhunt/data/ /home/networkhunt/pics/ /home/networkhunt/.accounting.db
To extract the gzip tar file
$ tar -xzvf file.tar.gz
3. Using bzip2 to compress and extract tar archives
To create and compress the archive file with the bzip2 compression, we have to use the option j with options cvf and use the file extension .bz2 with the archive file name.
$ tar -cjvf file.tar.bz2 /home/networkhunt/data/
To extract .bz2 tar files
$ tar -jtvf file.tar.bz2
4. How to exclude directories and files when using tar
sometimes we want to exclude certain files when creating a tarball. The syntax is:
$ tar -zcvf archive.tar.gz –exclude=’dir1′ –exclude=’regex’ dir1
For example, exclude ~/Downloads/ directory:
$ tar -czvf /nfs/backup.tar.gz --exclude="Downloads" /home/networkhunt/
5. How to view stored files in an archive?
Below commands can be used to view files stored in an archive without extracting.
To List the contents of a tar.gz file, -ztvf is used in tar command to view the contents of tar.gz files and to list the contents of a tar.bz2 file, -jtvf is used in tar command.
$ tar -ztvf file.tar.gz $ tar -jtvf file.tar.bz2