How to archive files in linux using tar command

Data archiving in Linux is the method of  storing one or more files into a single file for backup or distribution (transferring files locally or through network)  purpose. There are many tools available in Linux that can perform data archiving. In this post we will discuss  about tar, which is the basic command line archiving utility available in Linux.
tar command can archive files on a local disk as well as on a tape device. Similarly it can extract data from an archive file as well as from the tape device. The syntax of tar command is

tar [option] archive_name.tar directory_name/
OR
tar [option] archive_name.tar file1 file2 file3
OR
tar [option] archive_name.tar directory1/file1/ directory2/file2

Some of the commonly used option are,
-f, --file : Use archive file or device ARCHIVE
-c, --create : Create an archive
-x, --extract : Extract files from the archive
-u, --update : Update an archive
-r, --append : Append files to the end of an archive
-t, --list : List contents of an archive
-v, --verbose : verbose
--delete : Delete files from archive

Examples of tar command usage

#1) To create a tar file, execute
tar -cf my_archive.tar mydoc.pdf file1.txt2 file2.txt
The above command will create an archive file my_archive.tar comprising of files mydoc.pdf, file1.txt and file2.txt in the current working directory.

#2) To create an archive of a directory and its contents, execute
tar -cvf backup.tar myData/
Sample Output:










#3) Similarly, to archive multiple directories, execute
tar -cvf backup.tar myData/ work/error.log
The above command will create an archive file named backup.tar. The archive will contain all the contents of myData directory and a single file error.log inside the directory work.

#4) To list the contents of an archive, execute
tar -tvf backup.tar
Sample Output:









#5) To extract the contents of an archive to the current working directory, execute
tar -xvf backup.tar

#6) To archive the contents of a directory to a tape device, execute
tar -cvf /dev/st0 myData/
where /dev/st0 is the device driver for block level tape device
#7) To delete a file from the archive, execute
tar --delete -f backup.tar myData/license.pdf
The above command will delete the file license.pdf from myData directory archived inside backup.tar file.

#8) To update newer files to the archive, execute
tar -rvf backup.tar myData/
tar command will check for newer files inside myData directory and if present will append the new file to the existing archive. tar command when executed with update option will consider a modified file as a new file and will append the file to the archive. Now the archive will contain older and newer version of the file with same name and path. This is one of the limitations of a tar utility and upon extraction usually the newer version of the file will overwrite the older version

Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux