Admit it. Almost every time you deal with a tar” file or “gzip” file, you wonder what was the command that I used last time to “——“. You can fill the blanks with “Creating a new tar file, how to gzip a tar file, how to unzip a file, how to get a peek of what is in the tar file, and so on. Here are the ten useful linux commands to deal with tar and gzip files. “Tar” in a tar file simply means a tap archives and gzip is specific way compress files.
How to Create a Tar file?
Creating a archive of multiple files using tar command is simple. Use the tar command with -c and -f options as
tar -cf myFirst.tar file1.txt file2.txt file3.txt
where “-c” tells to create a new tar (archive) file and “-f” tell to use the file. A more convenient way to create a tar file is to use verbose “-v” option. The verbose “-v” option give more details about the archiving process by listing the files it uses and often help us to make sure we have created a tar file of what we needed.
How to Create a gzipped tar File?
Creating tar file help us organize our file. However, a more handy feature is is to compress the “tar” archive and use less space to store the archive. To create an archive and compress using the gzip algorithm, use
tar -czf files.tar.gz file1 file2
where the option “-c” tells to create a new tar (archive) file and “-z” tells to compress the archive using the compression program gzip. If you want to create a archive and compress it through bzip2, use the option “-j” instead of “-z”. For example
tar -cjf files.tar.bz2 file1 file2
will create bz2 file of the archive.
How to Extract a compressed (gzipped/bzipped) tar file?
To extract a tar file that is compressed with gzip, use
tar -xzf files.tar.gz
Similarly, if you want to extract a archive file compressed with bzip2, use
tar -xjf files.tar.gz
How to Extract a compressed (gzipped/bzipped) tar file in to Specific Directory?
Extracting a compressed tar file saves the extracted files in the current working directory. It will create a mess if you have archived a large number of files. One option is to extract the archive in a separate directory given by use. To extract a compressed archive in to a directory
tar -xzf file.tar.gz -C myDir
Remember the myDir should exists in the directory.
How to Get Peek of What is Inside a Compressed tar File?
To just see what files are in a compressed tar file, use the tar command with the option “-t” as
tar -tf myfiles.tar.gz
How to Extract a Specific File From a compressed (gzipped/bzipped) tar File?
Often you just want to extract a specific file instead of extracting all the files from a compressed tar file. The command
tar -xzf all.tar.gz wantThisFile.txt
will extract the file “wantThisFile.txt” from the gzipped tar file.
How to Update a tar file with a new file?
To update an archive, with a newer version of file use the option “-u” as
tar -uf newFileVersion.txt myfiles.tar.gz
The new version of the file will replace the existing file only if the modification date is newer.
How to Create a Gzipped Tar file of a Directory?
If you want to gzip and tar a whole directory use tar as
tar -zvcf myDir.tar.gz path_2_dir