15 Tar commands should try in Linux
On * nix operating systems, such as Linux, the commonly used utility is tar. The name of this command comes from ARchive Tape, because it was originally designed to back up data on tape.
Sometimes users need to pack multiple files and / or folders into a single file. It will be easier to transfer a file over the network and even all the files in the USB (but the more files, the slower the transfer will be). On operating systems like Windows, this is usually done by storing in .zip or .rar files. On * nix operating systems, such as Linux, the commonly used utility is tar. The name of this command comes from ARchive Tape, because it was originally designed to back up data on tape. Today, it can still be used for this purpose, but in most cases, it is simply the method of creating 'tarball'. The purpose of tarball is to facilitate uploading, downloading and moving data or keeping backups.
Tar commands are common in Linux
- 1. Refer to the tar user manual
- 2. Create a Tarball folder
- 3. Add content to an existing Tarball
- 4. List the contents of tar file
- 5. Extract content from Tarball
- 6. Create Tarball compression (gzip)
- 7. Create smaller compressed Tarball (bzip)
- 8. Extract a file or folder
- 9. Extract only the files / folders that match a template
- 10. Preserving access rights
- 11. Delete files or folders from Tarball
- 12. Exclude files or folders from being added to Tarball
- 13. In a file system
- 14. Preserve extended attributes
- 15. Create new folders for the extracted files
1. Refer to the tar user manual
Each command in Linux has a tutorial page. You can refer to the tar user manual by entering the following command in the terminal:
man tar
Look through the content using the arrow keys, PAGE UP and PAGE DOWN . Press q to exit the tutorial.
2. Create Tarball folder
Create a directory with 9 empty files to check tar commands:
mkdir $HOME/test9 && touch $HOME/test9/{1.9}
Now switch to the main directory:
cd
To transfer the contents of a directory to a tar file:
tar -cvf test.tar test9
- c - stands for 'create'
- v - Verbose, export the file currently added to the repository; Useful for time-consuming tasks and check progress
- f - Inform the utility of creating a tar file, with the name specified as test.tar
3. Add content to an existing Tarball
Create another folder with empty files whose names are sorted from a to z:
mkdir $HOME/test-az && touch $HOME/test-az/{a.z}
Now, add it to the existing tar archive (parameter r):
tar -rvf test.tar test-az
If you open test.tar in the graphics storage manager, you will see the following content.
4. List the contents of tar file
Use the -t flag to list the contents of the tar file.
tar -tf test.tar
5. Extract content from Tarball
Content will be extracted in the current directory. Create a new folder and then move the content there:
mkdir $HOME/test-extract && cd $HOME/test-extract
Now extract the test.tar in the current directory:
tar -xvf $HOME/test.tar
6. Create Tarball compression (gzip)
If you intend to transfer tar files over the network, making it smaller is a great idea to increase transfer speeds. Change back to the home directory.
cd
And create a compressed tar file:
tar -cvzf test.tar.gz test9
As can be seen, the only difference is that the z parameter is added to guide the tar to use gzip to compress the file.
7. Create smaller compressed Tarball (bzip)
Instead of the z parameter, users can use j to compress with bzip.
tar -cvjf test.tar.bz test9
This will continue to reduce the file size but take longer to compress and decompress.
8. Extract a file or folder
Just add the correct name of the file or directory, as it appears in the tar archive:
tar -xvf test.tar test9/7
This only extracts the file test9 / 7 .
9. Extract only the files / folders that match a template
If you want to extract all the files ending with the .jpg extension :
tar -xvf test.tar --wildcards '*.jpg'
10. Preserving access rights
Access is not important when creating tarballs from documents, images and similar media files. But if you are backing up the system, you can add p parameter to maintain file access:
tar -cvpf test-allow.tar test9
11. Delete files or folders from Tarball
Only works on uncompressed tar files.
tar -f test.tar --delete test-az
This removes the test-az directory from the tarball, including the files contained therein:
tar f test.tar --delete test-az / z will only delete test file test-az / z .
12. Exclude files or folders from being added to Tarball
tar -cvf test-exclude.tar --exclude test9/9 test9
As you can see, file 9 has been removed from the tar archive. Can add many exclusion parameters:
tar cvf test-exclude.tar --exclude test9/9 --exclude test9/8 test9
13. In a file system
--one-file-system can be added in a command like:
tar -cvpzf backup.tar.gz --one-file-system /
This will back up the original file system, except for other file systems that you may have mounted in / home, / mnt, / media, / proc, etc.
14. Preserve extended attributes
Besides file access, some Linux distributions also have file extension attributes. To preserve all of them, add the following parameters: --acls --selinux --xattrs .
Example command:
tar -cvpzf backup.tar.gz --one-file-system --acls --selinux --xattrs /
15. Create new folders for the extracted files
Usually, tar will extract to the user's current directory. You can use mkdir new_directory and cd new_directory before using tar, if the directory you want to extract does not exist. But the tar guide does everything for you better:
tar -xvf test.tar --one-top-level=new_directory
These are the most commonly used tar commands. But this utility is very flexible and can be used in many different ways. If you don't like reading the instructions in the terminal, you can find the HTML version of the tar guide for easier reading.
See more:
- Basic Shell commands in Linux
- Kali Linux commands from AZ and commonly used commands
- Combine all the most basic AutoCAD commands
You should read it
- 5 Linux commands every sysadmin needs to know
- Kali Linux commands from AZ and commonly used commands
- How to Run Linux Commands on Windows with WSL 2
- Guide to network operation for Linux users: 11 commands to know
- 14 interesting Linux commands in Terminal
- Useful commands in Unix / Linux
- Top 10 examples of Netstat commands on Linux
- Basic Linux commands everyone needs to know
- How to run 2 or more Terminal commands at the same time on Linux
- 10 most useful Linux commands
- How to run Linux commands when starting Windows Subsystem for Linux on Windows 10
- Extremely dangerous commands on Windows, Linux, Mac, don't try even once
Maybe you are interested
Instructions to retrieve password Zalo Pay What is Display Manager on Linux? What is a filter? How to use Filter in photography? Call the function by pointer in C ++ Travel backpack turns into a sleeping bivy tent for 30 seconds In your opinion, should you clean or wash after going to the toilet? The answer will startle you