Tar command in Linux with examples – GeeksforGeeks

The Linux ‘tar’ stands for tape archive

, it is used to create files and extract the archive files.tar command in Linux is one of the important commands that provides archiving functionality in Linux. We can use the Linux tar command to create compressed or uncompressed files and also maintain and modify them.

Syntax

: tar [options] [archive-file] [file or directory to archive]Options: -c : Create file -x : Extract the file -f : creates a file

with a given file name -t : displays or lists files in archived file -u : files and adds to an existing archive –v : Displays detailed information -A : Concatenates archive files –z : zip, tells the tar command to create tar file using gzip -j : filter tar file using tbzip -W : Verify an archive -r : Update or add file or directory to an existing .tar file

What is an archive file? An archive file is a file that consists of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easy portability and storage, or simply to compress files to use less storage space.

Examples: 1. Creating an uncompressed tar file using the -cvf option: This command creates a tar file named file.tar which is the file of all .c files in the current directory.

$ tar cvf file.tar *.c

Output:

os2.c os3.c os4.c

2. Extracting files from Archive using the -xvf option: This command extracts files from Archives.

$ tar xvf file.tar

Output

: os2.c os3.c os4.c 3.

gzip compression on the tar file, using the -z option: This command creates a tar file called archive.tar.gz which is the .c file file.

File $ tar cvzf.tar.gz *.c

4. Extracting a gzip file tar *.tar.gz using the -xvzf option: This command extracts files from archived files tar.tar.gz archives.

File $ tar xvzf.tar.gz

5. Creating tar compressed files on Linux using the -j option: This command compresses and creates an archive file smaller than the size of the gzip. Both compressing and decompressing takes longer than gzipping.

$ tar cvfj file.tar.tbz example.cpp Output: $tar cvfj.tar.tbz file example.cpp example.cpp $tar tvf.tar.tbz file -rwxrwxrwx root/root 94 2017-09-17 02

:

47 example.cpp

6. Unzip a single tar file or specified directory on Linux: This command will unzip a file to the current directory or to a specified directory using the -C option.

File $ tar xvfj.tar or file $ tar xvfj.tar -C file path to the file in directory

7. Unzip multiple files .tar, .tar.gz .tar.tbz on Linux: This command will extract or decompress multiple files from the tar, tar.gz, and tar.bz2 file. For example, the above command will extract “fileA” “fileB” from the archive files.

$ tar xvf file.tar “fileA” “fileB” or $ tar zxvf file1.tar.gz “fileA” “fileB” or $ tar jxvf file2.tar.tbz “fileA” “fileB”

8. Check the size of the existing tar, tar.gz, tar.tbz file on Linux: The above command will display the size of the compressed file in kilobytes (KB).

$ tar czf file.tar | WC -C or $ tar CZF file1.tar.gz | wc -c o $ tar czf file2.tar.tbz | WC -C

9. Update the existing tar file on Linux $ tar

rvf file.tar *.c

Output

: os1.c

10. List the contents and specify the tarfile file using the -tf option: This command will display the complete list of archived files. We can also list specific content in a tarfile

file $ tar tf.tar

Output:

example.cpp

11. Applying pipe to through ‘grep command’ to find what we are looking for: This command will display only the text or image mentioned in grep from the archived file.

$ tar tvf file.tar | grep “text to find” o $ tar tvf file.tar | grep “filename.file extension

12. We can pass a file name as an argument to search for a tarfile file: This command sees the archived files along with their details.

$ tar tvf file.tar file name

13. Viewing the file using the

-tvf $ tar option tvf file.tar Output : -rwxrwxrwx root/root 191 2017-09-17 02:20 os2.c -rwxrwxrwx root/root 218 2017-09-17 02:20 os3.c -rwxrwxrwx root/root 493 2017-09-17 02

:

20 os4.c

What are wildcards in Linux Alternatively known as a “wildcard character” or “wildcard character,” a wildcard is a symbol used to replace or represent one or more characters. Wildcards are usually an asterisk (*), which represents one or more characters, or a question mark (?), which represents a single character.

Example :

14. To search for an image in .png format: This will extract only files with the .png extension from the archive file.tar. The -wildcards option instructs tar to interpret wildcards in the name of the files to be extracted; The file name (*.png) is enclosed in single quotation marks to prevent the shell from incorrectly expanding the wildcard (*).

$ tar tvf file.tar -wildcards ‘*.png’

Note: In the above commands ” * ” is used instead of the file name to take all the files present in that particular directory.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L This article is contributed by Akansh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See their article listed on the GeeksforGeeks homepage and help other Geeks.

Please write comments if you find something wrong, or if you want to share more information about the topic discussed above.