DEV Community

Cover image for Archiving files in Linux
Muhammad Aditiya Rakhman
Muhammad Aditiya Rakhman

Posted on • Edited on • Originally published at dev.to

Archiving files in Linux

To archive files we can use the tar command. Tape archiver or tar is an archiving program designed to store multiple files in a single file archive and can manipulate such archives. The archive can be either a regular file or a block device, which can be located either on the local or on a remote machine.

Note that you can also archive the directory


Syntax of the tar Command in Linux

$ tar [Options] [Archive-file] [File or Directory to be archived]
Enter fullscreen mode Exit fullscreen mode

Options :
-c, --create : Create a new archive.
-x, --extract : Extract from an existing archive.
-t, --list : List the table of contents of an archive.
-u, --update : Add and update files stored in the archive.
-A, --catenate, --concatenate : Add archive file members to another archive.
-d, --diff, --compare : Used to compare and verify whether there are files on our system that are stored in the archive.
-r, --append : Forcefully add files to the archive
-v, --verbose : Show which files get archived or extracted.
-f, --file=NAME_ARCHIVE : Archive file name.
-z, --gzip : Use gzip compression (.tar.gz).
-j, --bzip2 : Use bzip2 compression (.tar.bz2). bzip2 typically achieves a better compression ratio than gzip.
-J, --xz : Use xz compression (.tar.xz). The xz compression typically achieves a better compression ratio than bzip2.


Example of using the tar command

  • Create archive files without compression
$ tar -cvf tar-image image1.png image2.png image3.png image4.png

Output:
image1.png
image2.png
image3.png
image4.png
Enter fullscreen mode Exit fullscreen mode

The above command will create an archive file with the name tar-image containing the files image1.png, image2.png, image3.png, and image4.png without compressing them. You can check that the archived files are not compressed by looking at the number of archived file sizes that will be the same or slightly larger than the size of the tar-image file.

$ ls -lh 

Output:
total 360K
-rw-r--r-- 1 guest guest 16K Oct 11 17:04 image1.png
-rw-r--r-- 1 guest guest 90K Oct 11 17:04 image2.png
-rw-r--r-- 1 guest guest 11K Oct 11 17:04 image3.png
-rw-r--r-- 1 guest guest 60K Oct 11 17:04 image4.png
-rw-rw-r-- 1 guest guest 180K Oct 11 17:10 tar-image
Enter fullscreen mode Exit fullscreen mode
  • View the list of files in the archive file
$ tar -tvf tar-image

Output:
-rw-r--r-- guest/guest 15616 2024-10-11 17:10 image1.png
-rw-r--r-- guest/guest 92160 2024-10-11 17:10 image2.png
-rw-r--r-- guest/guest 10259 2024-10-11 17:10 image3.png
-rw-r--r-- guest/guest 60866 2024-10-11 17:10 image4.png
Enter fullscreen mode Exit fullscreen mode
  • To add and update files stored in the archive

Note: that if the file already exists in the archive, tar will not append and update the requested file.

$ tar -uvf tar-image image5.png 

Output:
image5.png

Or we can do it like this:
$ tar --add-file=image5.png -uf new-tar-image

List and check:
$ tar -tvf tar-image

Output:
-rw-r--r-- guest/guest 15616 2024-10-11 17:10 image1.png
-rw-r--r-- guest/guest 92160 2024-10-11 17:10 image2.png
-rw-r--r-- guest/guest 10259 2024-10-11 17:10 image3.png
-rw-r--r-- guest/guest 60866 2024-10-11 17:10 image4.png
-rw-r--r-- guest/guest 10259 2024-10-11 17:12 image5.png
Enter fullscreen mode Exit fullscreen mode
  • Add files from another archive source(archive must have the same format).

For example, if we want to add file members from the tar-image archive to the new-tar-image, we can do so by adding the -A option.

$ tar -Af new-tar-image tar-image

List and check:
$ tar -tf new-tar-image
image1.png
image2.png
image3.png
image4.png 
Enter fullscreen mode Exit fullscreen mode
  • Used to compare and verify whether there are files on our system that are stored in the archive.

If an archive is compared to a file and the file is contained in the archive, tar will output the name of the file being compared.

$ tar -dvf new-tar-image image1.png image2.png image3.png

Output: 
image1.png
image2.png
image3.png
Enter fullscreen mode Exit fullscreen mode

Conversely, if the file is not in the archive, tar will output an error

$ tar -dvf new-tar-image image6.png

Output:
tar: image6.png: Not found in archive
tar: Exiting with failure status due to previous errors
Enter fullscreen mode Exit fullscreen mode
  • Add files to the archive regardless of whether or not they already exist in the archive(Recursively). The -r option is also the same as the -c option or can create archive files

Note: you can add the same files or directories to the archive

$ tar -rvf dir-tar-image image5.png

Output:
image5.png

$ tar -rvf next-tar-image image image5.png

Output:
image/
image/image1.png
image/image2.png
image/image3.png
image/image4.png
image5.png
image5.png

$ tar -tvf next-tar-image 

Output:
drwxrwxr-x guest/guest     0 2024-10-11 15:13 image/
-rw-r--r-- guest/guest 10259 2024-10-11 15:13 image/image1.png
-rw-r--r-- guest/guest 60866 2024-10-11 15:13 image/image2.png
-rw-r--r-- guest/guest 92160 2024-10-11 15:13 image/image3.png
-rw-r--r-- guest/guest 15616 2024-10-11 15:13 image/image4.png
-rw-r--r-- guest/guest 10259 2024-10-11 22:01 image5.png
-rw-r--r-- guest/guest 10259 2024-10-11 22:01 image5.png
Enter fullscreen mode Exit fullscreen mode
  • Delete files from the archive
$ tar -tf tar-image

Output:
image1.png
image2.png

$ tar --delete -f tar-image image1.png

$ tar -tf tar-image

Output:
image2.png
Enter fullscreen mode Exit fullscreen mode
  • Extract archive files
$ tar -xvf tar-image

Output:
image1.png
image2.png
image3.png
image4.png
image5.png
Enter fullscreen mode Exit fullscreen mode

To make sure you get the extracted files from the archive, you can delete the previously archived files in the tar-image file first. Warning to be careful in deleting files.

  • Creating archives and compressing files with the gzip program.

These files are normally given the extensions .tar.gz to show that they are tar archives zipped up with gzip. You may also see the extension .tgz.

Creating archive file:
$ tar -czvf image.tar.gz image1.png image2.png image3.png 

Output:
image1.png
image2.png
image3.png

Listing and Checking:
$ tar -tvf image.tar.gz 

Output:
-rw-r--r-- guest/guest 15616 2024-10-11 17:04 image1.png
-rw-r--r-- guest/guest 92160 2024-10-11 17:04 image2.png
-rw-r--r-- guest/guest 10259 2024-10-11 17:04 image3.png

$ file image.tar.gz 

Output:
image.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 122880

Extract:
$ tar -xvf image.tar.gz 

Output:
image1.png
image2.png
image3.png
Enter fullscreen mode Exit fullscreen mode
  • Create archives using a file compression program called bzip2. These archives usually have a .tar.bz2 extension.
Creating archive file:
$ tar -cjvf image.tar.bz2 image4.png image5.png

Listing and Checking:
$ tar -tvf image.tar.bz2 image4.png image5.png 

Output:
-rw-r--r-- guest/guest 60866 2024-10-11 17:07 image4.png
-rw-r--r-- guest/guest 10259 2024-10-11 17:07 image5.png

$ file image.tar.bz2 

Output:
image.tar.bz2: bzip2 compressed data, block size = 900k

Extract:
$ tar -xvf image.tar.bz2 

Output:
image4.png
image5.png
Enter fullscreen mode Exit fullscreen mode
  • Create archive files with efficient compression through the xz program. These archives usually have the extension .tar.xz.

xz is a compressed file format that utilizes the LZMA2 compression algorithm. It is designed to replace the gzip and bzip2 formats. xz files are created and saved as binary files onto disk. Data on this format can achieve compression ratios of up to 50%.

Creating archive file:
$ tar -cJvf image.tar.xz image1.png image2.png image3.png image5.png 

Output:
image1.png
image2.png
image3.png
image5.png

Listing and Checking:
$ tar -tvf image.tar.xz

Output
-rw-r--r-- guest/guest 15616 2024-11-11 17:11 image1.png
-rw-r--r-- guest/guest 92160 2024-11-11 17:11 image2.png
-rw-r--r-- guest/guest 10259 2024-11-11 17:11 image3.png
-rw-r--r-- guest/guest 10259 2024-11-11 17:11 image5.png

$ file image.tar.xz 

Output:
image.tar.xz: XZ compressed data, checksum CRC64

Extract:
$ tar -xvf image.tar.xz 

Output:
image1.png
image2.png
image3.png
image5.png
Enter fullscreen mode Exit fullscreen mode

If you have difficulty or confusion using the tar command, you can run this command in your terminal.

$ man tar
$ tar --help
Enter fullscreen mode Exit fullscreen mode

Reference:


See you.....

Top comments (1)

Collapse
 
aditech profile image
Muhammad Aditiya Rakhman

Up