- Use tar to combine multiple files into an archive file
- Create an archive file
- Create a compressed archive file
- Extract the contents of an archive file
- Additional information
- Related documents
- 15+ tar command examples in Linux [Cheat Sheet]
- Some Important Options used with tar command
- Different examples to use the tar command
- 1. Create an archive using tar command
- 2. tar command to list the contents of archive without extracting
- 3. Search for files inside archive using tar command
- 4. Display the information verbosely using tar command
- 5. tar command to archive all files in any directory
- 6. tar command to archive only specified file type
- 7. Extract files from an archive using tar command
- 8. Extract specific files from archive using tar command
- 9. Append files to an existing archive using tar command
- 10. Merge one archive into another archive using tar command
- 11. tar command to delete files inside the archive
- 12. tar command to find differences between archive and file
- 13. Append only newer files in the archive
- 14. Extract archive into a different directory using tar command
- 15. Create gzip archive using tar command
- 16. Create bzip2 archive using tar command
- 17. Exclude files while creating an archive
- 18. Show block numbers within the archive
- Conclusion
- What’s Next
- Further Reading
- tar: add all files and directories in current directory INCLUDING .svn and so on
- 16 Answers 16
Use tar to combine multiple files into an archive file
In Unix and Unix-like operating systems (such as Linux ), you can use the tar command (short for «tape archiving») to combine multiple files into a single archive file for easy storage and/or distribution. Additionally, you can use tar in conjunction with a compression utility, such as gzip or compress , to create a compressed archive file.
Create an archive file
To combine multiple files into a single archive file (for example, my_files.tar ), use the following command (replace file1 and file2 with the names of the files you want to combine):
tar -cvf my_files.tar file1 file2
To combine all the files in a directory into a single archive file (for example, my_files.tar ), use the following command (replace /path/to/my/directory with the absolute path to the directory containing the files you want to combine):
tar -cvf my_files.tar /path/to/my/directory
- You can use any name in place of my_files.tar , but you should keep the .tar extension.
- If you don’t use the -f option, tar will assume you want to create a tape archive instead of combining a number of files.
- The -v option tells tar to be verbose (report all files as they are added).
Create a compressed archive file
Many Linux distributions use GNU tar , a version of tar produced by the Free Software Foundation . If your system uses GNU tar , you can use tar in conjunction with the gzip file compression utility to combine multiple files into a compressed archive file.
- To use tar and gzip to combine multiple files into a compressed archive file (for example, my_files.tar.gz ), use the following command (replace file1 and file2 with the names of the files you want to combine):
tar -cvzf my_files.tar.gz file1 file2
tar -cvzf my_files.tar.gz /path/to/my/directory
- In the above examples, the -z option tells tar to use gzip to compress the archive as it is created.
- The file extensions .tgz and .tar.gz are equivalent; both signify a tar archive file compressed with gzip .
If your system does not use GNU tar , but nonetheless has gzip , you can create a compressed tar archive file (for example my_files.tar.gz with the following command (replace file1 and file2 with the names of the files you want to combine):
tar -cvf - file1 file2 | gzip > my_files.tar.gz
If gzip isn’t available on your system, you can use the compress utility to create a compressed archive (for example, my_files.tar.Z ); for example (replace file1 and file2 with the names of the files you want to combine):
tar -cvf - file1 file2 | compress > my_files.tar.Z
Extract the contents of an archive file
To extract the contents of a tar archive file created by tar (for example, my_files.tar ), use the following command:
To extract the contents of a tar archive file compressed with gzip (for example, my_files.tar.gz ), use the following command:
If you are not using GNU tar and need to extract the contents of a tar archive file compressed with gzip (for example, my_files.tar.gz ), use the following command:
gunzip -c my_files.tar.gz | tar -xvf -
To extract the contents of a tar archive file compressed with compress (for example, my_files.tar.Z ), use the following command:
uncompress -c my_files.tar.Z | tar -xvf -
Additional information
When using the tar command, the order of the options sometimes matters. For example, some versions of tar (not GNU tar ) require that the -f option be immediately followed by a space and the name of the tar archive file.
The tar command has many options available. For details, consult the tar manual page; on the command line, enter:
GNU tar comes with additional documentation, including a tutorial, accessible through the GNU Info interface. You can access this documentation by entering:
Within the Info interface, press ? (the question mark) for a list of commands.
At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.
Related documents
This is document acfi in the Knowledge Base.
Last modified on 2023-06-27 11:03:02 .
15+ tar command examples in Linux [Cheat Sheet]
tar stands for tape archive. tar command is used to create and extract the archive file in the Linux system. tar is an important tool as it provides archiving functionality in the system. An archive file compresses all the files and collects them together in a single file. It uses less storage in the device.
The basic syntax of the tar command would be:
$ tar [options] [archive_file [file or directory to be archived]
Some Important Options used with tar command
-c : This option creates the archive file. -f : This option is used to specify the archive file. -x : It extracts the content from the archive file. -t : It displays the list of files inside the archive file. -v : This option shows the detailed or verbose information. -r : It updates the archive file by adding newer files. -j : It is used to filter the archive through bzip2. -z : It filters the archive through gzip.
Different examples to use the tar command
In this article, you will find different examples of using tar command in Linux to manage the archive file.
1. Create an archive using tar command
To create a new archive file, you can use -c or —create option followed by -f and the archive file name. You also have to specify the file or directory names to be archived. tar command does not create an empty archive file.
$ tar -cf archive_filename filename1 filename2 filename3
$ tar --create -f archive_filename filename1 filename2 filename3
Sample Output:
2. tar command to list the contents of archive without extracting
You can use -t or —list option to view the content of a tar archive file.
$ tar --list -f archive_filename
Sample Output:
3. Search for files inside archive using tar command
You can also search files using -t option by specifying the file names.
$ tar -tf archive_file files_to_search
Sample Output:
4. Display the information verbosely using tar command
-v or —verbose option prints the verbose information for every file processed.
$ tar -v [options] archive_filename
$ tar --verbose [options] archive_filename
Sample Output:
5. tar command to archive all files in any directory
To add all the contents of the current directory to tar archive, you can use * instead of the file name.
Sample Output:
6. tar command to archive only specified file type
You can specify a file format that you want to add to the archive file.
$ tar -cf archive_filename *.file_type
Sample Output:
7. Extract files from an archive using tar command
To extract files from the tar archive file, you can use -x or —extract or —get option followed by -f and the archive file name.
$ tar --extract -f archive_filename
$ tar --get -f archive_filename
Sample Output:
8. Extract specific files from archive using tar command
You can specify the file name which you want to extract from an archive file. It only extracts those files from the archive file.
$ tar -xf archive_filename filename1 filename2
Sample Output:
9. Append files to an existing archive using tar command
You can use -r option add a file or directory to an existing tar archive file. It appends files to the end of an archive.
$ tar -rf archive_filename file_to_be_added
10. Merge one archive into another archive using tar command
-A option allows adding contents of one archive to another. It appends files to the end of another archive.
$ tar -Af archive_file archive_files_to_be_added
Sample Output:
11. tar command to delete files inside the archive
—delete option allows you to delete the files from archive. You must specify files inside the archive as arguments.
$ tar --delete -f archive_file files_to_be_deleted
Sample Output:
12. tar command to find differences between archive and file
You can use -d or —diff or —compare option to find differences between archive and file.
$ tar -df archive_file file_name
$ tar --diff -f archive_file file_name
$ tar --compare -f archive_file file_name
Sample Output:
13. Append only newer files in the archive
-u or —update option appends the files which are newer than the copy in the archive. The newer files don’t replace the old copies in the archive. They are appended to the end of an archive.
$ tar -uf archive_file files_to_be_added
$ tar --update -f archive_file files_to_be_added
Sample Output:
14. Extract archive into a different directory using tar command
-C or —directory=DIR option changes to the specified directory before permitting any operations.
$ tar -xf archive_file -C directory_path
$ tar -xf archive_file --directory=DIR
Sample Output:
15. Create gzip archive using tar command
Gzip is an algorithm for file compression and decompression. To compress files with gzip algorithm, you can use -z option. The gzip file should end with tar.gz or tgz .
$ tar -czf file.tar.gz files_to_be_archived
Sample Output:
To extract tar.gz file, you can use:
16. Create bzip2 archive using tar command
Another common algorithm for file compressing is bzip2. To compress files with the bzip2 algorithm, you can use -j option. The bzip2 archive name should end with tar.bz2 or tbz .
$ tar -cjf file.tar.bz2 files_to_be_archived
Sample Output:
To extract tar.gz file, you can use:
17. Exclude files while creating an archive
—exclude=FILE option excludes the FILE when adding to tar archive or extracting from a tar archive. It is useful when you have to ignore some files in a large number of files.
$ tar -cf archive_file *.file_type --exclude=FILE
Sample Output:
To exclude when extracting:
$ tar -xf archive_file --exclude=FILE
Sample Output:
18. Show block numbers within the archive
-R or —block-number option prints the block numbers within the archive.
Sample Output:
Conclusion
Now, you can easily use tar command to create and extract the archive file. You can use this command in any Linux distribution for managing the tar archive. If you have any confusion, you can ask the questions in the comment section.
What’s Next
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
tar: add all files and directories in current directory INCLUDING .svn and so on
The resulting tar includes .svn directories in subdirs but NOT in the current directory (as * gets expanded to only ‘visible’ files before it is passed to tar I tried to tar -czf workspace.tar.gz . instead but then I am getting an error because ‘.’ has changed while reading:
tar: ./workspace.tar.gz: file changed as we read it
Is there a trick so that * matches all files (including dot-prefixed) in a directory? (using bash on Linux SLES-11 (2.6.27.19)
16 Answers 16
Don’t create the tar file in the directory you are packing up:
tar -czf /tmp/workspace.tar.gz .
does the trick, except it will extract the files all over the current directory when you unpack. Better to do:
cd .. tar -czf workspace.tar.gz workspace
or, if you don’t know the name of the directory you were in:
base=$(basename $PWD) cd .. tar -czf $base.tar.gz $base
(This assumes that you didn’t follow symlinks to get to where you are and that the shell doesn’t try to second guess you by jumping backwards through a symlink — bash is not trustworthy in this respect. If you have to worry about that, use cd -P .. to do a physical change directory. Stupid that it is not the default behaviour in my view — confusing, at least, for those for whom cd .. never had any alternative meaning.)
One comment in the discussion says:
I [. ] need to exclude the top directory and I [. ] need to place the tar in the base directory.
The first part of the comment does not make much sense — if the tar file contains the current directory, it won’t be created when you extract file from that archive because, by definition, the current directory already exists (except in very weird circumstances).
The second part of the comment can be dealt with in one of two ways:
- Either: create the file somewhere else — /tmp is one possible location — and then move it back to the original location after it is complete.
- Or: if you are using GNU Tar, use the —exclude=workspace.tar.gz option. The string after the = is a pattern — the example is the simplest pattern — an exact match. You might need to specify —exclude=./workspace.tar.gz if you are working in the current directory contrary to recommendations; you might need to specify —exclude=workspace/workspace.tar.gz if you are working up one level as suggested. If you have multiple tar files to exclude, use ‘ * ‘, as in —exclude=./*.gz .