Contents...
Tar command is used to create .tar.gz or .tgt archive files in Linux. It is also called tarballs. There are lots of option with tar command that can be use, but you just need to remember a few of them to quickly create tar file. Tar command is also used to extract the tar file.
In this article I will describe some useful tar command example.
Compress an Entire Directories or Single File
Follow the below command to create a tar archive using directory, adding all files in it and sub directories as well.
# tar -cvf data.tar data ./data/ ./data/xyz.txt ./data/xyz.txt
Where:
c :- Create a tar file
v :- Verbose mode
z :- Gzipped File
f :- Use the following tar archive for the operation
Above command does not create a compressed archive. It does only make plain archive with multiple files without any compression.
If you want compress, Follow the below command.
# tar -zcvf data.tar data
Extension of the file name does not matter. You can use tar.gz and tgz because both are common extensions which is used for files compression with gzip. “.tar.bz2” and “.tbz” are commonly used extensions for bzip compression files.
Confirmation Before Adding Files Into Archive
You can use “w” option to confirmation before adding every files into archive.
Only those file will be added which are given “yes” answer. Default is “No” if you don’t enter anything.
Add only needed files
# tar -czw -f data.tar.gz /data/* add ‘./data/abc.txt’?y add ‘./data/cde.txt’?y add ‘./data/newfile.txt’?n add ‘./data/santosh’?y add ‘./data/dantosh/in.txt’?n
Listing added files
# tar -t -f abc.tar.gz ./data/abc.txt ./data/cde.txt ./data/santosh/
Adding Files Into Existing Archives
You can also add file in archives using -r option without having to create new tar file.
# tar -rv -f data.tar newfile.txt
Note:- You can add new file only in plain tar archive, but you can not add any file into compressed archives (gz or bzip).
Adding Files Into Compressed Archives
I have already mentioned you can not add in any new file in compressed archives, but it can be done with some simple trick. First use the gunzip command to un-compress the archive, add new file to archive and compress it again.
# gunzip data.tar.gz # tar -rf data.tar newfile.txt # gzip data.tar
Verify Archive Files During Creation
You can verify the files while creation using “W” option.
# tar -cvW -f data.tar data ./data/ ./data/cde.txt ./data/santosh/ ./data/santosh/in.txt ./data/newfile.txt ./data/abc.txt Verify ./data/ Verify ./data/cde.txt Verify ./data/santosh/ Verify ./data/santosh/in.txt Verify ./data/newfile.txt Verify ./data/abc.txt
Note:- Verification can not be done on compressed archives. It works only with uncompressed tar archives.
Extract a .tar.gz Archive
Follow the below command to extract tar files.
# tar -xvzf data.tar.gz
Where:
x :- Extract files
v :- Verbose mode
z :- Gzipped File
f :- Use the following tar archive for the operation
Extract .tar.bz2 Files
bzip algorithm is used to make bz2 compression files and tar command can deal with them as well. You can use” j “option to extract bz2 archives.
# tar -xvjf data.tar.bz2
Extract .tar.gz File On Specific Location
You can use -C option to extract file .tar file on specific location.
# mkdir /opt/directory # tar -xvzf data.tar.gz -C /opt/directory/
Extract single file from .tar.gz file
Follow the below command to extract single file from tarballs.
# tar -xz -f data.tar.gz "/data/abc.txt"
For multiple files
# tar -xv -f data.tar.gz "/data/cde.txt" "/data/abc.txt"
Extract Multiple Files With Wildcards
You can use the wildcards to extract bunch of files matching the given wildcards.
Example:
# tar -xv -f data.tar.gz --wildcards "*.txt"
Listing The Contents Of The tar File
You can list out the content of the tar file without extracting them, use -t option.
# tar -tz -f data.tar.gz ./data/ ./data/cde.txt ./data/santosh/ ./data/santosh/in.txt ./data/abc.txt ...
OR
For additional details use “v” option.
# tar -tvz -f data.tar.gz | grep abc.txt -rw-rw-r-- opt/backup 0 2017-04-98 11:40 ./data/abc.txt
I hope this article will help you to compress and extract files in Linux. If you have more option to tar and untar file please share with us using comment section.
Thanks:)
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
RAR file is used to store other files inside,it is a compressed folder if you want to open RAR files then you have to UNRAR it you need to download it from internet using free tool.if you having any kid of issue follow this link.