Software & Apps Linux Example uses of the Linux command 'tar' Tar files are an old-school method for archiving data by Juergen Haas Writer Former Lifewire writer Juergen Haas is a software developer, data scientist, and a fan of the Linux operating system. our editorial process Juergen Haas Updated on February 20, 2020 Linux Switching from Windows Tweet Share Email A tar file is like a zip file—a single archive of a collection of files and folders—but unlike a zip, a tar isn't compressed. It originated with old-school tape archives (hence the tar term) but is still in wide use today because of its ubiquity in the Linux ecosystem. tonymax / Getty Images How to Create a Tar File Create a tar file containing all of your images while maintaining the folder structure using the following command: tar -cvf photos ~/photos The switches are as follows: -c: create-v: verbose-f: files How to List the Files in a Tar File List the contents of a tar file by using the following command: tar -tf tarfilename This provides a list of the files and folders within a tar file. A tar file might extract files to folders you weren't expecting and corrupt parts of your system so knowing which files are going where is a good starting point. At the worst, bad people create something called a tar bomb, which is designed to destroy your system. The previous command simply gives a list of the files and folders. For a verbose view showing file sizes use the following command: tar -tvf tarfilename The switches are as follows: -t: list contents of an archive-f: file-v: verbose How to Extract From a Tar File To extract the contents of a tar file use the following command: tar -xvf tarfile The switches are as follows: -x: extract-v: verbose-f: file How to Append Files to a Tar File To add files into an existing tar file run the following command: tar -rvf tarfilename /path/to/files The switches are as follows: -r: append-v: verbose-f: files How to Append Files Only If They Are Newer The problem with the previous command is that if you added files that already exist in the tar file they would be overwritten. To only add files if they are newer than existing files use the following command: tar -uvf tarfilename /path/to/files How to Prevent 'tar' From Overwriting Files If you are extracting a tar file you may not want to overwrite files if they already exist. This command makes sure that existing files are left alone: tar -xkvf tarfilename Only Extract Files That Are Newer Than Existing Files If you are extracting a tar file you might be happy for files to be overwritten but only if the file in the tar file is newer than the existing file. The following command achieves this result: tar --keep-newer-files -xvf tarfilename How to Remove Files After Adding Them to a Tar File A tar file remains uncompressed, so if you add a 400-gigabyte file to a tar file you will have a 400-gigabyte file in its original location and tar file with a 400-gigabyte file in it. To remove the original file when it is added to a tar file: tar --remove-files -cvf tarfilename /path/to/files Compress a Tar File When You Create It To compress a tar file as soon as it is created, use the following command: tar -cvfz tarfilename /path/to/files Summary The tar command has dozens of switches and more information can be found by using the man tar command or by running tar --help.