Applications 24 - Compression with tar

J

Jarret W. Buse

Guest
Applications 24 - Compression with tar

In the previous article, Applications 23 - Compression with gzip, the basis that small files can help speed up a system. File compression is making files smaller than they are so they can read/written faster and transferred over the network/Internet quicker.

There is also the ability to write more compressed files on portable media such as USB, CD/DVD, etc. The tar compression method is another typical method used on Linux systems.

The basic syntax for using tar is:

tar <operation> [option] [folder/file]

The Operation specifies what the program will do to the specified file. The Operations and their examples are:

  • -A, (--catenate, --concatenate) - append tar files to an existing archive
To copy the contents of file1.tar into file2.tar, use the command “tar -A --file file2.tar file1.tar”. The “--file” option is used to specify a file name.

  • -c, (--create) – create a new tar file
To create a tar archive of all files and folders in the current directory and output to a file called “test.tar”, use the command “tar -c * > test.tar”.

  • -d, (--diff, --compare) – find differences between system files and those in a tar file
To compare the contents of the archive “level.tar” with the system files, perform the command “tar -df level.tar”. If any differences are found, they will be displayed.

  • -r, (--append) – append files to the end of an archive
To append new files into an existing archive, use the “-r” operation. To add “file1.txt” into the archive “file.tar”, use the command “tar -rf file1.tar file1.txt”.

  • -t, (--list) – list contents of the tar file
To list the contents of a archive, use the operation “-t”. To see the contents of the archive “file1.tar” use the command “tar -t file1.tar”.

  • -u, (--update) – append newer files into an existing archive
To add updated files to an existing tar, you use the “-u” operation. To update the modified file called “file1.txt” into the archive “file.tar”, the command would be “tar -uf file.tar file1.txt”.

  • -x, (--extract, --get) – extract files from tar archive
At some point, files need to be extracted from a archive. To extract files, use the “-x” operation. To extract the file from the archive “file.tar” to the current folder, the command would be “tar -xf file.tar”.

  • --delete - delete from the tar
To delete a file or files from an archive, use the “--delete” operation. To delete the file “file1.txt” from the archive “file.tar” use the command “tar --delete -f file.tar file1.txt”.

Now that the Operations are covered, we can look into the common Options and some examples.

  • -C, (--directory DIR)
The “-C” option is used to specify a folder. If used when creating a tar, the specified directory is compressed into a tar. If used when extracting files, the files will be placed into the specified folder.

  • -f, (--file F)
The “-f” option specifies the file to use. To delete the file “file1.txt” from the archive “file.tar”, use the command “tar --delete -f file.tar file1.txt”.

  • -j, (--bzip2)
Use bzip2 when compressing and use bzip2 when decompressing. To compress the folder “level2” to an archive called “file.tar.bz2”, use the command “tar -cjf file.tar.bz2 level2”. If the files need to be extracted back to “level2” from the file “file.tar.bz2”, use the command “tar -xjf mytar.tar.bz2 -C level2”.

  • -p, (--preserve-permissions)
When permissions need to be preserved for a file, use the “-p” option. To compress the files in the “level3” folder with their permissions to a file called “permissions.tar”, use the command “tar -xpf permissions.tar -C level3”

  • -v, (--verbose)
To show more information during a tar process, use the “-v” option with any other command given. This usually gives a list of files being processed.

  • -z, (--gzip)
The “-z” option is used to create a “.gzip” file from the tar file. So, to create an archive of the files in “level3” and then run through gzip to a file called “archive.tar.gz”, the command is “tar -czf archive.tar.gz -C level3 .”. The extra “.” causes the files in “level3” to be processed.
It is best to try the Operations and Options to understand how they work as well as working together with other Options. Of course, you may wonder about the command-line tar program and not just use a Graphical User Interface (GUI) compression program. Always be prepared in the case you are on a system with no compression program with a GUI. As well as not being able to access the Internet to download a GUI based compression program. There are a few questions on the Linux+ Certification exam about tar (as well as gzip).
In the next article, I will cover the non-common options for tar.
 

Attachments

  • slide.jpg
    slide.jpg
    32.9 KB · Views: 82,863

Staff online

Members online


Latest posts

Top