Your cart is currently empty!
Tar Command Cheatsheet

The tar command in Linux is used for archiving and compressing files and directories. It is a versatile utility with various options for creating, extracting, and managing archive files.
1. Create a Tar Archive:
To create a new tar archive, use the following command:
tar -cvf archive.tar file1 file2 directory/
- -c: Create a new archive.
- -v: Verbose mode (display progress).
- -f: Specify the archive file name.
This command will create an archive named archive.tar containing file1, file2, and all contents within the directory/ directory. Adjust the file and directory names as needed.
2. Create a Gzipped Tar Archive:
tar -czvf archive.tar.gz directory/
- -z: Compress the archive with gzip.
3. Extract a Tar Archive:
tar -xvf archive.tar
- -x: Extract files from an archive.
4. Extract a Gzipped Tar Archive:
tar -xzvf archive.tar.gz
- -z: Decompress a gzip-compressed archive.
5. List Contents of an Archive:
tar -tvf archive.tar
- -t: List archive contents.
6. Append Files to an Existing Archive:
tar -rvf archive.tar newfile.txt
- -r: Append files to an existing archive.
7. Create a Tar Archive with Compression (Using XZ):
tar -cJvf archive.tar.xz directory/
- -J: Compress the archive with xz.
8. Extract a Tar Archive with Compression (Using XZ):
tar -xJvf archive.tar.xz
- -J: Decompress an xz-compressed archive.
9. Create a Tar Archive and Exclude Files:
tar --exclude='file1' -cvf archive.tar directory/
- --exclude: Exclude specified files or directories from the archive.
10. Extract a Single File from an Archive:
tar -xvf archive.tar file_to_extract.txt
11. Create a Tar Archive and Set Compression Level (Using Bzip2):
tar -cvjf archive.tar.bz2 directory/
- -j: Compress the archive with bzip2.
12. Extract a Tar Archive with Compression (Using Bzip2):
tar -xjvf archive.tar.bz2
- -j: Decompress a bzip2-compressed archive.
Recommendations:
- Use tarwith options appropriate for your task, such as-cfor creating,-xfor extracting, and-tfor listing.
- When creating archives, specify a descriptive file name, and use compression options like -z(gzip),-J(xz), or-j(bzip2) for efficient storage.
- Be cautious when using wildcards like *in archive creation; double-check which files are included.
- Consider using the --excludeoption to omit unnecessary files from your archive.
- Always verify the contents of an archive with tar -tvfbefore extracting to avoid surprises.
- Keep backups of important files before performing archive operations.

Leave a Reply