The tar command in Linux/Unix is used to create, extract, and manage archive files. It’s commonly used to combine multiple files or directories into a single file, often for backup or distribution purposes.
Key Concepts:
• Tar archives are often compressed to save space (e.g., .tar.gz or .tgz files).
• Think of it like “zipping” and “unzipping” files, but specifically for Unix/Linux systems.
Basic Syntax:
tar [options] [archive_name] [file_or_directory]
• options: What you want to do (e.g., create, extract, list, etc.).
• archive_name: The name of the archive file (e.g., backup.tar).
• file_or_directory: The files or directories to include in the archive.
Common Examples:
1. Create a tar archive:
tar -cvf archive.tar file1 file2 directory/
• -c: Create an archive.
• -v: Verbose (show what’s happening).
• -f: Specify the file name of the archive (archive.tar).
2. Extract a tar archive:
tar -xvf archive.tar
• -x: Extract files from an archive.
• -v: Verbose (list files while extracting).
• -f: Specify the archive file name.
3. Compress a tar archive (gzip):
tar -cvzf archive.tar.gz file1 file2 directory/
• -z: Compress the archive using gzip.
• Creates a .tar.gz file.
4. Extract a compressed tar archive:
tar -xvzf archive.tar.gz
• -z: Decompress the gzip-compressed archive.
5. List contents of a tar archive:
tar -tvf archive.tar
• -t: List the files in the archive.
6. Add files to an existing tar archive:
tar -rvf archive.tar newfile
• -r: Append files to an archive.
Real-World Examples:
• Backup a directory:
tar -cvzf backup.tar.gz /home/user/documents
This creates a compressed backup of the documents directory.
• Extract a downloaded file:
tar -xvzf software.tar.gz
Extracts the contents of a compressed .tar.gz file.
• Transfer files as one package:
Combine files into a single tar file, send it over, and extract it on another machine.
Quick Reference for Options:
Option Description
-c Create an archive
-x Extract files from an archive
-v Verbose (show progress)
-f Specify archive file name
-z Compress or decompress using gzip
-t List contents of an archive
-r Append files to an existing archive
In short, tar is a versatile tool for packing, unpacking, and compressing files!
No comments:
Post a Comment