Tar commands on linux

Содержание
  1. 10 Tar command examples in Linux to create and extract archives
  2. Tar command on Linux
  3. Using the tar command
  4. 1. Extract a tar.gz archive
  5. 2. Extract files to a specific directory or path
  6. 3. Extract a single file
  7. 4. Extract multiple files using wildcards
  8. 5. List and search contents of the tar archive
  9. 6. Create a tar/tar.gz archive
  10. 7. Ask confirmation before adding files
  11. 8. Add files to existing archives
  12. 9. Add files to compressed archives (tar.gz/tar.bz2)
  13. 10. Backup with tar
  14. 11. Verify archive files while creation
  15. Conclusion
  16. 18 Tar Command Examples in Linux
  17. 1. Create tar Archive File in Linux
  18. 2. Create tar.gz Archive File in Linux
  19. 3. Create tar.bz2 Archive File in Linux
  20. 4. Untar tar Archive File in Linux
  21. 5. Uncompress tar.gz Archive File in Linux
  22. 6. Uncompress tar.bz2 Archive File in Linux
  23. 7. List Content of tar Archive File in Linux
  24. 8. List Content tar.gz Archive File in Linux
  25. 9. List Content tar.bz2 Archive File in Linux
  26. 10. Untar Single file from tar File in Linux
  27. 11. Untar Single file from tar.gz File in Linux
  28. 12. Untar Single file from tar.bz2 File in Linux
  29. 13. Untar Multiple files from tar, tar.gz, and tar.bz2 File
  30. 14. Extract Group of Files using Wildcard in Linux
  31. 15. Add Files or Directories to tar Archive File in Linux
  32. 16. Add Files or Directories to tar.gz and tar.bz2 Files
  33. 17. How To Verify tar, tar.gz, and tar.bz2 Archive File
  34. 18. Check the Size of the tar, tar.gz, and tar.bz2 Archive File
  35. Tar Usage and Options
  36. If You Appreciate What We Do Here On TecMint, You Should Consider:
  37. tar command in Linux with examples

10 Tar command examples in Linux to create and extract archives

Tar command on Linux

The tar (tape archive) command is used to create and extract archives in Linux. It can compress and store multiple files in a single archive.

The commonly seen file extensions are .tar.gz and .tar.bz2 which is a tar archive further compressed using gzip or bzip algorithms respectively.

In this tutorial we shall take a look at simple examples of using the tar command to do daily jobs of creating and extracting archives on linux desktops or servers.

Using the tar command

The tar command is available by default on most linux systems and you do not need to install it separately.

1. Extract a tar.gz archive

Well, the more common use is to extract tar archives. The following command shall extract the files out a tar.gz archive

Here is a quick explanation of the parameters used —

Those are some of the important options to memorise

Extract tar.bz2/bzip archives

Files with extension bz2 are compressed with the bzip algorithm and tar command can deal with them as well. Use the j option instead of the z option.

2. Extract files to a specific directory or path

To extract out the files to a specific directory, specify the path using the «-C» option. Note that its a capital C.

However first make sure that the destination directory exists, since tar is not going to create the directory for you and will fail if it does not exist.

3. Extract a single file

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

More than once file can be specified in the above command like this

4. Extract multiple files using wildcards

Wildcards can be used to extract out a bunch of files matching the given wildcards. For example all files with «.txt» extension.

5. List and search contents of the tar archive

If you want to just list out the contents of the tar archive and not extract them, use the «-t» option. The following command prints the contents of a gzipped tar archive,

Pipe the output to grep to search a file or less command to browse the list. Using the «v» verbose option shall print additional details about each file.

For tar.bz2/bzip files use the «j» option

Use the above command in combination with the grep command to search the archive. Simple!

6. Create a tar/tar.gz archive

Now that we have learnt how to extract existing tar archives, its time to start creating new ones. The tar command can be told to put selected files in an archive or an entire directory. Here are some examples.

The following command creates a tar archive using a directory, adding all files in it and sub directories as well.

The above example does not create a compressed archive. Just a plain archive, that puts multiple files together without any real compression.

Читайте также:  Astra linux поддержка ntfs

In order to compress, use the «z» or «j» option for gzip or bzip respectively.

7. Ask confirmation before adding files

A useful option is «w» which makes tar ask for confirmation for every file before adding it to the archive. This can be sometimes useful.

Only those files would be added which are given a yes answer. If you do not enter anything, the default answer would be a «No».

8. Add files to existing archives

The r option can be used to add files to existing archives, without having to create new ones. Here is a quick example

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

Its already mentioned that its not possible to add files to compressed archives. However it can still be done with a simple trick. Use the gunzip command to uncompress the archive, add file to archive and compress it again.

For bzip files use the bzip2 and bunzip2 commands respectively.

10. Backup with tar

A real scenario is to backup directories at regular intervals. The tar command can be scheduled to take such backups via cron. Here is an example —

Run the above command via cron and it would keep creating backup files with names like —
‘archive-20150218.tar.gz’.

Ofcourse make sure that the disk space is not overflown with larger and larger archives.

11. Verify archive files while creation

The «W» option can be used to verify the files after creating archives. Here is a quick example.

Note that the verification cannot be done on compressed archives. It works only with uncompressed tar archives.

Conclusion

Besides the tar command, there are many other commands available on the linux command line that can be used to compress and pack files in an archive. For example the zip and unzip command can also be used to create compressed archives.

The zip command also supports the zipcloak command that can be used to create password protected .zip archives. The tar format does not support any kind of encryption. If you want to create password protected tar archives then use the ccrypt command to encrypt the .tar or .tar.gz file.

Thats all for now. For more check out the man page for tar command, with «man tar». Or run the «tar —help» to see the list of all options supported by the tar command.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

Источник

18 Tar Command Examples in Linux

The Linux “tar” stands for tape archive, which is used by a large number of Linux/Unix system administrators to deal with tape drives backup.

The tar command is used to rip a collection of files and directories into a highly compressed archive file commonly called tarball or tar, gzip and bzip in Linux.

The tar is the most widely used command to create compressed archive files and that can be moved easily from one disk to another disk or machine to machine.

Linux Tar Command Examples

In this article, we will be going to review and discuss various tar command examples including how to create archive files using (tar, tar.gz, and tar.bz2) compression, how to extract archive file, extract a single file, view content of the file, verify a file, add files or directories to the existing archive file, estimate the size of tar archive file, etc.

The main purpose of this guide is to provide various tar command examples that might be helpful for you to understand and become an expert in tar archive manipulation.

1. Create tar Archive File in Linux

The below example command will create a tar archive file tecmint-14-09-12.tar for a directory /home/tecmint in the current working directory. See the example command in action.

Let’s discuss each option used in the above command to create a tar archive file.

  1. c – Creates a new .tar archive file.
  2. v – Verbosely show the .tar file progress.
  3. f – File name type of the archive file.

2. Create tar.gz Archive File in Linux

To create a compressed gzip archive file we use the option as z. For example, the below command will create a compressed MyImages-14-09-12.tar.gz file for the directory /home/MyImages. (Note: tar.gz and tgz both are similar).

3. Create tar.bz2 Archive File in Linux

The bz2 feature compresses and creates an archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files than gzip, which takes less time.

Читайте также:  Windows developers preview update

To create a highly compressed tar file we use the option j. The following example command will create a Phpfiles-org.tar.bz2 file for a directory /home/php. (Note: tar.bz2 and tbz is similar to tb2).

4. Untar tar Archive File in Linux

To untar or extract a tar file, just issue the following command using option x (extract). For example, the below command will untar the file public_html-14-09-12.tar in the present working directory.

If you want to untar in a different directory then use option as -C (specified directory).

5. Uncompress tar.gz Archive File in Linux

To Uncompress tar.gz archive file, just run the following command. If we would like to untar in different directories, just use option -C and the directory path, as shown in the above example.

6. Uncompress tar.bz2 Archive File in Linux

To Uncompress the highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .flv files from the archive file.

7. List Content of tar Archive File in Linux

To list the contents of the tar archive file, just run the following command with option t (list content). The below command will list the content of the uploadprogress.tar file.

8. List Content tar.gz Archive File in Linux

Use the following command to list the content of the tar.gz file.

9. List Content tar.bz2 Archive File in Linux

To list the content of the tar.bz2 file, issue the following command.

10. Untar Single file from tar File in Linux

To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.

11. Untar Single file from tar.gz File in Linux

To extract a single file tecmintbackup.xml from the tecmintbackup.tar.gz archive file, use the command as follows.

12. Untar Single file from tar.bz2 File in Linux

To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.

13. Untar Multiple files from tar, tar.gz, and tar.bz2 File

To extract or untar multiple files from the tar, tar.gz, and tar.bz2 archive file. For example, the below command will extract “ file 1” “ file 2” from the archive files.

14. Extract Group of Files using Wildcard in Linux

To extract a group of files we use wildcard-based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz, and tar.bz2 archive file.

15. Add Files or Directories to tar Archive File in Linux

To add files or directories to the existing tar archive files we use the option r (append). For example, we add file xyz.txt and directory php to the existing tecmint-14-09-12.tar archive file.

16. Add Files or Directories to tar.gz and tar.bz2 Files

The tar command doesn’t have an option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file. If we do try will get the following error.

17. How To Verify tar, tar.gz, and tar.bz2 Archive File

To verify any tar or compressed archived file we use the option W (verify). To do this, just use the following examples of commands. (Note: You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

18. Check the Size of the tar, tar.gz, and tar.bz2 Archive File

To check the size of any tar, tar.gz, and tar.bz2 archive file, use the following command. For example, the below command will display the size of the archive file in Kilobytes (KB).

Tar Usage and Options

  • c – create an archive file.
  • x – extract an archive file.
  • v – show the progress of the archive file.
  • f – filename of the archive file.
  • t – viewing the content of the archive file.
  • j – filter archive through bzip2.
  • z – filter archive through gzip.
  • r – append or update files or directories to the existing archive files.
  • W – Verify an archive file.
  • wildcards – Specify patterns in UNIX tar command.

That’s it for now, hope the above tar command examples are enough for you to learn, and for more information please use the man tar command.

If you are looking to split any large tar archive file into multiple parts or blocks, just go through this article:

If we’ve missed any examples please do share with us via the comment box and please don’t forget to share this article with your friends. This is the best way to say thanks…..

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

Читайте также:  Key для windows 10 ltsb

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

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.

Syntax:

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 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.

Output :

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

Output :

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.

Output :

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.

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.

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).

9. Update existing tar file in Linux

Output :

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

Output :

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.

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

Output :

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.

Example :

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.

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.

Источник

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