Archiving and Compressing files and directories in Linux
Archiving and compressing files and directories in Linux is done by the tar command. The tar command, which stands for tape archive, is utilized by Linux. An archive can be made using this command, and its contents can be extracted. It’s one of the crucial commands in Linux that makes archiving capabilities possible. This command allows us to create both compressed and uncompressed archive files, as well as change and manage them.
Users can merge many files into one archive file using the computer program Tar. For distribution and backup purposes, it is commonly referred to as a tarball. The phrase “tape archive” served as the inspiration for the name because it was created to describe data on sequential I/O devices utilizing none of their file systems. Many file system attributes, including directory structure, file access approval, ownership, time stamps, and name, are included in the archive data groups created by tar. POSIX dropped tar in favor of pax, yet tar is still widely used.
Now let’s see how archiving and compressing files and directories in Linux is done.
Syntax
The basic syntax for creating a tar archive is as follows.
tar [options] [archive-file] [file or directory to be archived]
Options
-c : Creates an Archive
-x: Remove the archive.
-f : Generates an archive from the specified filename.
-t : Shows or lists the files within an archived file.
-u: Creates an archive file and adds to it.
-v: Verbose Information is Displayed
-A : The archive files are joined together
-z : Zip instructs the tar command to produce a tar file using gzip.
-j : Tar file filtering using tbzip
-W : Check an archive file.
-r : Edit or add to an existing file or directory.A tar file
Note
Let’s check that you have tar installed on the Linux machine you’re using before we look at how to create a TAR directory.
How to Install Tar on a Linux System
Most contemporary Linux distributions come with tar preinstalled. The following commands can be used to install it if you don’t already have it.
On distributions based on Debian and Ubuntu
sudo apt install tar
On Arch Linux and other distributions based on Arch:
sudo pacman -S tar
Relating to RHEL, CentOS, and Fedora:
sudo yum install tar
Linux TAR Directory Creation with Data Compression
Open the command prompt in the directory containing your files or subdirectories as the first and most important step. Either the cd command or clicking on right in the directory and choosing Open in Terminal from the context menu can be used to do this.
The commands below make it simple to use tar to compress files or directories.
To make a TAR directory
tar -cvf archivename.tar directory_path
produce a TAR.GZ file and directory compression
tar -czvf archivename.tar.gz directory_path
In the commands, the letters c, z, v, and f stand for Create, gzip, Verbose, and Filename, respectively.
You might use tar to compress the Test folder, for example.
tar -czvf Test-file.tar.gz Test
The steps for making TAR and TAR.GZ files
A crucial step in backing up your Linux file system is creating archives. This guarantees that if something goes wrong with your system, your backup won’t be impacted and the contents won’t become corrupt.
Utilizing the tar function
The tar command’s fundamental syntax for creating compressed tarballs is as follows:
tar -xvzf archive.tar.gz
tar -xvf archive.tar
where the compressed file’s name is the archive. Extract, Verbose, gzip, and Filename are the individual symbols for the aggregate -xvzf option, respectively. The input file is anything that comes after the -f option. Keep in mind that you can delete the -z parameter from the commands if you’re working with TAR files.
The compressed file’s content can alternatively be unzipped to a particular place as shown below:
tar -xvzf archive.tar.gz -C /Downloads
tar -xvf archive.tar -C /Downloads
The archive.tar.gz file will be extracted and put in the /Downloads folder using the aforementioned command.
To see an archive’s content before extracting it.
tar -ztvf archive.tar.gz
tar -tvf archive.tar
There is an opportunity to pick which files to extract from the archive. To accomplish this, you provide the default command and specify the file names.
tar -xvzf archive.tar.gz file1 file2
In a similar manner, you can unzip specific folders from the archive.
tar -xvzf archive.tar.gz directory1 directory2
To indicate the file names which you do not wish to extract, use the –exclude flag.
tar -xvzf archive.tar.gz –exclude=/Downloads –exclude=file1.txt
Using the -j option to create compressed tar files
We may generate and compact archive files with this option. In comparison to gzip, decompression and compression both take longer.
The instruction is as follows:
$ tar cvfj file.tar.tbz hello1.txt
Linux command to untar a single given directory or file
Any file in the present directory or within the directory that is given with the -C option can be untarred using this option.
The instruction is as follows:
$ tar -xvf file.tar -C file’s path in the directory
Or,
$ tar -xvf file.tar
Multiple.tar.tbz,.tar.gz, and.tar files can be untarred on Linux
With the aid of this option, we may untar or extract several files from tar.gz, tar.bz2, and tar archive files.
The following is an illustration of this choice.
$ tar -jxvf file.tar.tbz “hello1.txt”
Or,
$ tar -zxvf file.tar.gz “hello1.txt” “hello2.txt”
Or,
$ tar -zxvf file.tar.gz “hello1.txt” “hello2.txt”
Verify the current tar.tbz, tar.gz, or tar file’s size
The command will show us the size in kilobytes (KB) of the aforementioned archive file.
The instruction is as follows.
$ tar -czf – file2.tar.tbz | wc –c
Or,
$ tar -czf – file1.tar.gz | wc -c
Or,
$ tar -czf – file.tar | wc -c
Update the current tar file
To update an existing tar file on Linux, use the command line.
$ tar rvf file.tar *.txt
With the tf option, list and explain the contents of the tar file
We may list the list of all archive files using this option. We may also list specific contents within any tar file.
The instruction is as follows:
$ tar tf file.tar
Use the -tvf option to see the archive
On Linux, we can use the -tvf option to read the archive.
This is the instruction.
$ tar -tvf file.tar
To locate a tar file, pass the name of the file as the input
This option allows you to view the information in the archived files.
The instruction is as follows:
$ tar tvf file.tar filename
‘grep command’ is thrown using a pipe to find what we’re looking for
Using this option will enable us to limit the grep search through archived files to only the specified text or picture.
The instruction is as listed below:
$ tar tvf file.tar | grep “filename.file”
Or,
$ tar tvf file.tar | grep “text to search”
Find a picture in.png format
It will let us extract files from the file.tar bundle that only has the.png extension. The -wildcards option instructs tar how to interpret wildcards in the extracted file name.
To prevent the wildcard (*) from being improperly extended by any shell, the file name (*.png) is contained inside single quotes.
The instruction is as outlined below:
$ tar tvf file.tar –wildcards ‘*.png’
Get rid of the files in the tar archive
The –delete option can be used to remove files and tar archives.
The instruction is as follows:
$ tar –delete -f archive.tar file_name
Creating a Partial Archive with Specific Files
To create a partial archive with specific files, you can use the –files-from option with the tar command. This option allows you to specify a file that contains a list of files or directories to be included in the archive. Here’s how you can do it:
Create a file that contains the list of files or directories you want to include in the archive. Let’s say you have a file named filelist.txt, and it contains the following content:
/path/to/file1
/path/to/file2
/path/to/directory/
Each line represents a file or directory that you want to include.
Use the –files-from option to create the archive using the filelist:
tar -cf archive.tar –files-from=filelist.txt
This command creates a new archive file named archive.tar and includes the files and directories specified in filelist.txt.
Make sure to adjust the paths in filelist.txt to match the actual paths of the files and directories you want to include. The paths can be absolute or relative to the current directory.
By using the –files-from option, you have precise control over the selection of files and directories that go into the archive, allowing you to create partial archives tailored to your needs.
Extracting an Archive with Permissions and Ownership
To extract an archive with permissions and ownership preserved, you can use the -p or –preserve-permissions option with the tar command. This option ensures that the file permissions, ownership, and timestamps are maintained during the extraction process. Here’s the command:
tar -xpf archive.tar
This command will extract the contents of archive.tar while preserving the file permissions, ownership, and timestamps. The extracted files will retain their original attributes.
Note that you may need appropriate permissions to set ownership and permissions on the extracted files. If you don’t have the necessary privileges, you might see warnings or errors during the extraction process.
Additionally, you can use the -C option to specify a target directory for extraction. For example:
tar -xpf archive.tar -C /path/to/directory
This command will extract the contents of archive.tar into the /path/to/directory while preserving the permissions and ownership of the files.
Updating an Archive with New or Modified Files
To update an existing archive with new or modified files, you can use the -u or –update option with the tar command. This option allows you to add new files to the archive or update existing files with newer versions. Here’s how you can do it:
Update an Archive with New Files:
tar -uf archive.tar newfile1 newfile2
This command updates the archive.tar file by adding newfile1 and newfile2 to it. If the files already exist in the archive, they will be replaced with the new versions.
Update an Archive with Modified Files:
tar -uf archive.tar modifiedfile1 modifiedfile2
This command updates the archive.tar file by adding modifiedfile1 and modifiedfile2 to it. If the files already exist in the archive, they will be replaced with the modified versions.
Note that when using the -u option, tar will compare the modification timestamps of the files in the archive with the ones in the filesystem. Only files that have been modified or are new since the last update will be added to the archive.
Provide the correct paths for the new or updated files, replacing archive.tar with the name of your archive file.
Keep in mind that the -u option is used for updating existing archives. If the archive doesn’t exist, a new one will be created instead.
Be cautious when how archiving and compressing files and directories in Linux, especially if they are being used for backup purposes, to ensure that the updates reflect the desired changes accurately.
Final Thoughts
Now that’s how archiving and compressing files and directories in Linux is done. This is the most basic application of the tar command. We cannot reasonably provide all the additional options that are available with the command. for additional details. To display the detailed information page for the tar command, issue the info tar command at the shell. When you’re finished, press Q to close the information page.
You can learn about linux more deeply by clicking the link below
https://linuxiron.com/what-is-linux-a-whole-introduction/
Learn about the other linux commands by clicking the links below
https://linuxiron.com/echo-command-in-linux/
https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/
https://linuxiron.com/how-to-use-kill-commands-in-linux/
https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/
https://linuxiron.com/15-useful-yum-commands-in-linux/
https://linuxiron.com/how-to-use-the-top-command-in-linux/
https://linuxiron.com/17-ps-command-to-monitor-linux-process-with-examples-linuxiron/
https://linuxiron.com/12-cat-commands-in-linux-with-examples/
Use the -c option, the -f option, and the archive name to generate a tar archive. The contents of one or more directories or files can be used to build archives. Without the –no-recursion option, folders are archived recursively by default.
When storing many files in an archive file on Linux, the tar program is used. Later, we can extract the entire archive file’s contents or just the files we want. “Tape archive” is what tar stands for. By default, tar preserves the directory hierarchy of archived files.
An archive is a group of data that has been moved to a storage location for long-term preservation, to be kept apart for compliance needs, or to be removed from the primary storage media. Depending on how a specific program supports archiving, it may contain a basic list of files or files arranged under a directory or catalog structure.
A single file, a collection of files, or all the files in a directory or subdirectory can be archived. You can remove the original file from your workstation after you archive it. To archive files, use the archive command. Examples of how to archive things using the archive command are provided in the following table.