The tar
command stands for “tape archive.” It is used to create, maintain, modify, and extract files from a tar archive. Tar archives can be compressed to save space and make file transfers more efficient. The tar
command is commonly used to back up files and directories, as well as to transfer files over networks.
Initial Example
Using tar
to create a tar archive called archive.tar
from a directory called example_dir
:
tar -cvf archive.tar example_dir
TAR Parameters
Parameter | Description |
---|---|
-c, --create | Create a new archive |
-x, --extract | Extract files from an archive |
-f, --file | Use archive file or device (required parameter) |
-v, --verbose | Verbosely list files processed |
-t, --list | List the contents of an archive |
-r, --append | Append files to the end of an archive |
-u, --update | Only append files newer than the copy in the archive |
-d, --diff | Find differences between archive and file system |
-z, --gzip | Filter the archive through gzip |
-j, --bzip2 | Filter the archive through bzip2 |
-J, --xz | Filter the archive through xz |
-C, --directory | Change to directory before performing any operations |
--exclude | Exclude files matching the pattern |
--exclude-from | Read exclude patterns from a file |
--checkpoint | Display progress messages during operation |
--checkpoint-action | Run a user-defined action after a checkpoint |
-p, --preserve-permissions | Preserve file permissions when extracting files |
--numeric-owner | Use numeric user and group IDs |
--transform | Apply a transformation to file names |
-S, --sparse | Handle sparse files efficiently |
--remove-files | Remove files after adding them to the archive |
-P, --absolute-names | Do not strip leading slashes from file names |
--atime-preserve | Preserve the last access time of files |
--ignore-failed-read | Do not exit with nonzero on unreadable files |
--no-recursion | Avoid recursively descending directories |
--wildcards | Process wildcard characters in file names |
--owner | Specify the owner of files |
--group | Specify the group of files |
--mode | Set the file permissions |
--newer | Only store files newer than a specific date |
--one-file-system | Stay in one file system when creating an archive |
--sparse-version | Set the version of the sparse file format |
--strip-components | Strip the given number of leading components from file names |
--usage | Display a short usage message and exit |
--version | Show version information and exit |
--help | Display help information and exit |
Examples
1. Create a Tar Archive
To create a tar archive from a directory:
tar -cvf archive.tar directory_name
2. Extract a Tar Archive
To extract the contents of a tar archive:
tar -xvf archive.tar
3. Create a Compressed Tar Archive with gzip
To create a gzip-compressed tar archive:
tar -cvzf archive.tar.gz directory_name
4. Extract a Compressed Tar Archive with gzip
To extract the contents of a gzip-compressed tar archive:
tar -xvzf archive.tar.gz
5. Create a Compressed Tar Archive with bzip2
To create a bzip2-compressed tar archive:
tar -cvjf archive.tar.bz2 directory_name
6. Extract a Compressed Tar Archive with bzip2
To extract the contents of a bzip2-compressed tar archive:
tar -xvjf archive.tar.bz2
7. List Contents of a Tar Archive
To list the contents of a tar archive without extracting them:
tar -tvf archive.tar
8. Add Files to an Existing Tar Archive
To add files to an existing tar archive:
tar -rvf archive.tar newfile.txt