15 Tar commands should try in Linux
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
- 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
May be interested
- Basic Linux commands everyone needs to know - Operations on Linux are much fasterwhen using linux, you should also learn basic linux commands to perform operations more quickly and easily. below are some basic linux commands you should know.
- 10 most useful Linux commandsfor those who are new to linux, administration will face many difficulties. here are the 10 most useful linux commands that make your administration a lot easier.
- How to run Linux commands when starting Windows Subsystem for Linux on Windows 10the latest beta version of windows 10 allows users to automatically run linux commands when windows subsystem for linux starts.
- How to schedule Linux commands with 'at'utility at very easy to use. users only need to give time and date as command line parameters, then enter one or more commands to execute.
- Linux Error SUDO allows you to run commands as roota flaw in the sudo linux command has been discovered, which may allow non-privileged users to execute commands as root.
- 20+ essential Linux security commandshere are some of the most important security commands for everyday work on linux systems. if you're interested in security issues on your linux system, don't ignore these helpful security commands.
- 7 commands to manipulate the most basic files and folders everyone must knowon windows or linux operating systems, there are a number of things that users can only do with commands. in the following article, the network administrator will introduce you to the 7 most basic commands that any user must 'understand' to perform operations faster.
- Basic utilities: print, email in Unixby this chapter, you have a few basic insights about unix systems and some of its basic commands. this chapter will briefly discuss some of the basic but important utilities of unix utilities that you will use in your daily activities.
- Extremely dangerous commands on Windows, Linux, Mac, don't try even oncethere are commands that are used to provide certain features to the operating system. however, if used improperly, they cause danger and damage the operating system.
- How to execute Linux commands in the backgroundlinux commands are a great way to interact with the system using terminal. however, it can sometimes take a while to complete the task at hand. this forces the user to wait a significant amount of time or create a new shell entirely.