Compress file in Linux
Tar command in Linux
In Linux, Tar command is used to reduce the file size
Tar related Options
$ tar –help
Options | Description | |
---|---|---|
-c | –create | Create a new archive. |
-x | –extract | Extract files from an archive. |
-t | –list | List the contents of an archive. |
-f | –file=ARCHIVE | Use archive file or dir ARCHIVE. |
-v | –verbose | Verbosely list files processed. |
Compression Options with Tar
Options | Description | |
---|---|---|
-a | –auto-compress | Use archive suffix to determine the compression program. |
-j | –bzip2 | Filter the archive through bzip2. |
-J | -xz –lzma | Filter the archive through xz. |
-z | –gzip | Filter the archive through gzip. |
Compress Directory with option ‘-cf’
$ tar -cf <folder-name.tar> <folder-name>
In below example, i compressed Drupa8-vagrant directory with Drupa8.tar name.
Verbose output shows which files and directories are added to the archive, use the -v option
Compress Directory with Option ‘-v’
$ tar -cvf <folder-name.tar> <folder-name>
Ex:-
Compress Directory with Option ‘-z’
For archiving a folder compressed ‘gzip’, you have to use the -z option
$ tar -czf <folder-name.tar.gz> <folder-name>
Compress Directory with Option ‘-j’
You can instead compress the archive with ‘bzip2’, by using the -j option
$ tar -cjf <folder-name.tar.bz2> <folder-name>
Ex:-
Compress Directory with Option ‘-J’
$ tar -cJf <folder-name.tar.xz> <folder-name>
Ex:-
Extract directory from archive
$ sudo tar -xf <folder-name.tar>
List the Compressed data
List the contents of an archive file without extracting it:
$ sudo tar -tf <folder-name.tar.bz2>