- The tar command is one of the most versatile that we have within the Linux operating system.
- Many of its distributions allow us to take advantage of it to compress files or directories
- How to compress these elements, either separately or in volumes?
Linux is still somewhat of a mystery to people who are taking their first steps with it or one of its distributions, or who are thinking of doing so. Therefore, we consider that any trick in this regard will be of great use to you. In this article we show you, step by step, how to compress your files using the tar command in Linux, probably one of the best tutorials on this OS.
The first thing you have to know is that the tar command in Linux is used to create archive files. We are talking about files that we can quickly distinguish because they are .tar.gz or .tgz, also called “tarballs”.
But beyond that, This command has a large number of options that we cannot lose sight of. Eventually, it will allow us to compress files with the tar command, one by one or in groups.
That said, the GNU tar command included with Linux distributions has a built-in compression feature. You can create a .tar file and then compress it with gzip or bzip2 compression in a single command. Consequence of it, the resulting file is a .tar.gz file or a .tar.bz2 file.
Compress an entire directory or a single file in Linux
As we said, you can use this command to compress an entire directory or a single file in Linux. Remember that all other directories within the directory will be compressed that you have specified.
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
if you wonder what each component of the command meansis the next:
- -c: Create a file.
- -z: Compress the file with gz ip.
- -v – Shows the progress in the terminal while the file is being created, also known as “verbose” mode. It should be noted that the v is always optional in these commands, but it is useful.
- -f : Allows you to specify the file name of the file.
![compress linux tar files 2](https://wpdig.com/wp-content/uploads/2022/08/Compress-files-with-the-tar-command-in-Linux.webp.webp)
Suppose then that we have a directory called Stuff in the directory, and we want to save it inside a file called archive.tar.gz. The command that we must execute then is the following:
tar -czvf archive.tar.gz cosas
By running that tar command, you will then be compressing the directory into archive.tar.gz.
Compress multiple directories or files at once in Linux
While tar is often used to compress a directory, you can also use tar to compress multiple directories. you simply have to provide a list of files or directories instead of a single file or directory.
![compress linux tar archives 3](https://wpdig.com/wp-content/uploads/2022/08/1660304977_351_Compress-files-with-the-tar-command-in-Linux.webp.webp)
Let us again take as an example that you want to compress the /home/ubuntu/Downloads directory, the /usr/local/stuff directory, and the /home/ubuntu/Documents/notes.txt file. You would have to run the following command:
tar -czvf archive.tar.gz /home/ubuntu/Downloads /usr/local/stuff /home/ubuntu/Documents/notes.txt
As you can see, just add all the directories you want to compress to the command.
Have you been able to compress your files and directories with the tar command on Linux?