tar: Create, extract, and manage archives (compressed files)
August 9th, 2024 1:30 PM Mr. Q Categories: Command
Command: tar
The tar
command (tape archive) is used to create, extract, and manage archives. It can combine multiple files into a single archive file, often compressing it to save space. The tar
command supports various compression methods.
Sample Commands and Outputs:
tar -cvf archive.tar /path/to/directory
: Creates a new archive file namedarchive.tar
from the specified directory. Sample Command and Output:
$ tar -cvf archive.tar /home/user/documents
/home/user/documents/
/home/user/documents/file1.txt
/home/user/documents/file2.txt
Description:
-c
: Create a new archive.-v
: Verbose mode, showing the progress of files being archived.-f archive.tar
: Specifies the filename of the archive./path/to/directory
: The directory or files to be included in the archive.tar -xvf archive.tar
: Extracts the contents of thearchive.tar
file. Sample Command and Output:
$ tar -xvf archive.tar
/home/user/documents/
/home/user/documents/file1.txt
/home/user/documents/file2.txt
Description:
-x
: Extract the contents of an archive.-v
: Verbose mode, showing the files being extracted.-f archive.tar
: Specifies the filename of the archive.tar -cvzf archive.tar.gz /path/to/directory
: Creates a compressed archive using gzip. Sample Command and Output:
$ tar -cvzf archive.tar.gz /home/user/documents
/home/user/documents/
/home/user/documents/file1.txt
/home/user/documents/file2.txt
Description:
-z
: Compress the archive using gzip.-f archive.tar.gz
: Specifies the filename of the compressed archive.tar -xvzf archive.tar.gz
: Extracts a gzip-compressed archive. Sample Command and Output:
$ tar -xvzf archive.tar.gz
/home/user/documents/
/home/user/documents/file1.txt
/home/user/documents/file2.txt
Description:
-z
: Decompress the archive using gzip.tar -cvjf archive.tar.bz2 /path/to/directory
: Creates a compressed archive using bzip2. Sample Command and Output:
$ tar -cvjf archive.tar.bz2 /home/user/documents
/home/user/documents/
/home/user/documents/file1.txt
/home/user/documents/file2.txt
Description:
-j
: Compress the archive using bzip2.-f archive.tar.bz2
: Specifies the filename of the bzip2-compressed archive.tar -xvjf archive.tar.bz2
: Extracts a bzip2-compressed archive. Sample Command and Output:
$ tar -xvjf archive.tar.bz2
/home/user/documents/
/home/user/documents/file1.txt
/home/user/documents/file2.txt
Description:
-j
: Decompress the archive using bzip2.tar -tvf archive.tar
: Lists the contents of the archive without extracting. Sample Command and Output:
$ tar -tvf archive.tar
drwxr-xr-x user/user 0 2024-08-09 12:00 /home/user/documents/
-rw-r--r-- user/user 1234 2024-08-09 12:00 /home/user/documents/file1.txt
-rw-r--r-- user/user 5678 2024-08-09 12:00 /home/user/documents/file2.txt
Description:
-t
: List the contents of the archive.-v
: Verbose mode, showing file details.-f archive.tar
: Specifies the filename of the archive.tar -cf archive.tar --exclude='*.tmp' /path/to/directory
: Creates an archive excluding files with a.tmp
extension. Sample Command and Output:
$ tar -cf archive.tar --exclude='*.tmp' /home/user/documents
Description:
--exclude='*.tmp'
: Excludes files matching the pattern (e.g.,.tmp
files).
Note: The tar
command is versatile for managing file archives and supports various compression formats. Use appropriate options based on whether you need to create, extract, or inspect archives.