Linux – tar Command

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.



Tutorials dojo strip

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

ParameterDescription
-c, --createCreate a new archive
-x, --extractExtract files from an archive
-f, --fileUse archive file or device (required parameter)
-v, --verboseVerbosely list files processed
-t, --listList the contents of an archive
-r, --appendAppend files to the end of an archive
-u, --updateOnly append files newer than the copy in the archive
-d, --diffFind differences between archive and file system
-z, --gzipFilter the archive through gzip
-j, --bzip2Filter the archive through bzip2
-J, --xzFilter the archive through xz
-C, --directoryChange to directory before performing any operations
--excludeExclude files matching the pattern
--exclude-fromRead exclude patterns from a file
--checkpointDisplay progress messages during operation
--checkpoint-actionRun a user-defined action after a checkpoint
-p, --preserve-permissionsPreserve file permissions when extracting files
--numeric-ownerUse numeric user and group IDs
--transformApply a transformation to file names
-S, --sparseHandle sparse files efficiently
--remove-filesRemove files after adding them to the archive
-P, --absolute-namesDo not strip leading slashes from file names
--atime-preservePreserve the last access time of files
--ignore-failed-readDo not exit with nonzero on unreadable files
--no-recursionAvoid recursively descending directories
--wildcardsProcess wildcard characters in file names
--ownerSpecify the owner of files
--groupSpecify the group of files
--modeSet the file permissions
--newerOnly store files newer than a specific date
--one-file-systemStay in one file system when creating an archive
--sparse-versionSet the version of the sparse file format
--strip-componentsStrip the given number of leading components from file names
--usageDisplay a short usage message and exit
--versionShow version information and exit
--helpDisplay 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




Linux Playground

Scroll to Top