What is tar
?
tar
is primarily used for combining multiple files and directories into a single archive file (aka tarball), simplifying storage, backup, and transfer. It can also compress these archives to save disk space.
Syntax
tar <operation> [options] <archive-files> <files/directories>
- operation: The main action (eg:create,extract,list).
- options: Additional behaviours (eg: compression,verbosity).
- archive-files: Name of the archive.
- files/directories: What to include or extract.
Essential tar
Options
Option | Purpose |
---|---|
-c | Create a new archive |
-x | Extract files from an archive |
-t | List contents of an archive |
-f | Specify archive file name |
-v | Verbose output |
-z | Use gzip compression |
-j | Use bzip2 compression |
-J | Use xz compression |
Practical Usage
- Create a basic
tar
archive:
tar -cvf archive.tar file1.txt file2.txt directory1/
Combine file1.txt
, file2.txt
and directory1/
into a single archive named archive.tar
.
- Extract an archive:
tar -xvf archive.rar
- List contents of an archive:
tar -tvf archive.tar
Displays the contents of the archive without extracting.
Intermediate tar
Usage
Compression
- Create a
gzip
-compressed archive:
tar -czvf archive.tar.gz dirName/
- Create a
bzip2
compressed archive:
tar -cjvf archive.tar.bz2 dirName/
Extracting With Compression
- Extract a
gzip
-compressed archive:
tar -xzvf archive.tar.gz
- Extract a
bzip2
-compressed archive:
tar -xjvf archive.tar.bz2
- Extract to a specific directory:
tar -xvf archive.tar /home/iamyaash/extractLocation/
- Exclude files when archiving:
tar -cvf archive.tar dirName/ --exclude="*log"
Excludes only .log
files when archiving.
5. Extract a single file:
tar -xvf archive.tar /archiveDir/file.txt
If you want to extract only
wordpress/xmlrpc.php
fromarchive.tar
, you would run the above command. This command will extract only that file, not the entire archive in the current directory.
- Extract a single file to a specific directory using the
-C
option:
tar -xf archive.tar -C /target/directory path/to/file
- Extract files matching a pattern:
tar -xvf archive.tar --wildcards "*.php"
Advanced tar
Usage
Append or Update Files In An Archive
- Append a file:
tar -rvf archive.tar newFile.txt
- Update only newer files:
tar -uvf archive.tar updatedFile.txt
Delete Files From An Archive
tat --delete -f archive.tar unwantedFile.txt
Check Archive Size
tar -czf - dirName/ | wc -c
Outputs the size in Bytes.
Verify Archive Integrity
tar -tvg archive.tar
It lists the contents and checks for errors during listing.
Compare Archive With File System
tar -df archive.tar dirName/
Shows differences between the archive and current files.
xz
Compression:
tar -cJvf archive.tar dirName/
Split Large Archives
While tar
itself doesn’t split files, you can combine it with split
command:
tar -cvf - dirName/ | split -b 100M -archive_part_
splits the archive into
100MB
parts.
This is really a useful method of using archives, when you archive a single sometimes it get’s way over the size limit to copy to another machine. If a single file exceeds the certain size limit, the operating system won’t let you make the copy/move operation. It happens in Windows as far as I know. I have once tried to copy a games which is archived into a single file, when attempted to make copy operation, the Windows shows a dialog box stating that this file exceed the size limit and the operation can’t be made.
Task | Command Example |
---|---|
Create archive | tar -cvf archive.tar dir1/ |
Create gzip archive | tar -czvf archive.tar.gz dir1/ |
Create bzip2 archive | tar -cjvf archive.tar.bz2 dir1/ |
Extract archive | tar -xvf archive.tar |
Extract gzip archive | tar -xzvf archive.tar.gz |
Extract bzip2 archive | tar -xjvf archive.tar.bz2 |
List archive contents | tar -tvf archive.tar |
Exclude files | tar -cvf archive.tar dir1/ --exclude='*.log' |
Append files | tar -rvf archive.tar file.txt |
Delete from archive | tar --delete -f archive.tar file.txt |
Extract to directory | tar -xvf archive.tar -C /target/dir/ |