Using tar command in linux

How to use Tar Command in Linux with examples

The tar command stands for tape achieve, which is the most commonly used tape drive backup command used by the Linux/Unix system. It allows for you to quickly access a collection of files and placed them into a highly compressed archive file commonly called tarball, or tar, gzip, and bzip in Linux. The algorithm used for compressing of .tar.gz and .tar.bz2 are gzip or bzip algorithms respectively.

1) Extract a tar.gz archive

The following command shell helps to extract tar files out a tar.gz archive.

v – Verbose, print the file names as they are extracted one by one

z – The file is a “gzipped” file

f – Use the following tar archive for the operation

2) Extract files to a specific directory or path

We can extract the files into a specified directory by using the parameter “-C”.

# tar -xvzf bigfile.tar.gz -C /folder/subfolder/

The tar command doesn’t create any files or folders.

3) Extract a single file

To extract a single file out of an archive just add the file name after the command like this

To extract more than one file out of an archive

4) Extract multiple files using wildcards

To extract a group of files from archive

5) List and search contents of the tar archive

To list out the tar archive without extracting, we can use ‘-t’

For better viewing we can use less command or grep the pipe output for searching a file.

# tar -tvz -f Music.tar.gz | grep two.mp3

The verbose option “v” will provide more details about each file.

For tar.bz2/bzip files we should use “j” verbose option.

6) Create a tar/tar.gz archive

Using tar command, we can create tar archive using a directory, by adding all files in an archive or an entire directory.

The above command does not provide compression, but allows you to merge multiple files into one.

For the compression we could use the “z” or “j” option for gzip or bzip respectively.

The extension names does not have much more importance. What really matter is “.tar.gz” which is commonly used with gzip compression and .”tar.bz2″ and “.tbz”.They are commonly used extensions for bzip compressed files.

Читайте также:  Forgot my root password on linux

7) Permission before adding files

The option “w” allows tar asking for permission for archiving each file. The files which are replied by are only archived and with no reply will be considered as “No”.

The result of the above executed command is, which can be viewed by listing:

8) Add files to existing archives

By using the “r” option it adds files to existing archives, without the creation of a new one.

The main thing to be aware of is this option works only with normal archives, and not with the compressed archives.

9) Add files to compressed archives (tar.gz/tar.bz2)

It was mentioned earlier that it is not possible to add files to a compressed archive. However, it can be done by performing a simple step. Use the gunzip command to uncompress the archive, add the file to the archive and finally compress it.

# gunzip Institution.tar.gz

# tar -rf Institution.tar ./College/Engineering/top.ppt

# gzip Institution.tar

If you need any further assistance please reach our support department.

Источник

tar command in Linux with examples

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.

tar [options] [archive-file] [file or directory to be archived]

Options:
-c : Creates Archive
-x : Extract the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays Verbose Information
-A : Concatenates the archive files
-z : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file

Читайте также:  What is linux bumblebee

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

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

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

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

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

5. Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.

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

6. Untar single tar file or specified directory in Linux : This command will Untar a file in current directory or in a specified directory using -C option.

$ tar xvfj file.tar or $ tar xvfj file.tar -C path of file in directory 

7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive 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 size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

$ tar czf file.tar | wc -c or $ tar czf file1.tar.gz | wc -c or $ tar czf file2.tar.tbz | wc -c

9. Update existing tar file in Linux

Читайте также:  Gcc компилятор linux использование

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

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

$ tar tvf file.tar | grep "text to find" or $ tar tvf file.tar | grep "filename.file extension"

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

13. Viewing the Archive using option -tvf

-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 referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?),which represents a single character.

14. To search for an image in .png format : This will extract only files with the extension .png from the archive file.tar. The –wildcards option tells tar to interpret wildcards in the name of the files
to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.

$ tar tvf file.tar --wildcards '*.png'

Note: In above commands ” * ” is used in place of 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 your article appearing on the GeeksforGeeks main page and help other Geeks.

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

Источник

Оцените статью
Adblock
detector