Introduction
GNU tar
is an archiving program on Linux designed to store multiple files in a single file (an archive), and to manipulate such archives. The archive can be either a regular file or a device (e.g. a tape drive, hence the name of the program, which stands for tape archiver), which can be located either on the local or on a remote machine.
tar Extracting Files usefull examples
To Unpack or Extract a tar File
tar -xvf zipped_file.tar
This command extracts files from a gzip
-compressed .tar.gz
file.
Extract single file
tar -xvf zipped_file.tar file1.txt
You can extract specific files or directories by specifying their names.
Extracting Files to a Specific Directory
tar -xvf zipped_file.tar -C /var/www/html
To extract files to a directory other than the current one, use the -C
option.
Viewing the Contents of an Archive Before Extracting
tar -tf archive.tar
Explanation:
-t
: Lists the contents of the archive.
This is useful for checking the files in the archive before extraction.
Preserving Permissions During Extraction
To ensure that file permissions and ownership are preserved during extraction, use the --preserve-permissions
or --same-permissions
option.
sudo tar -xf archive.tar --preserve-permissions
Explanation:
- This ensures that extracted files retain their original permissions and ownership, which is especially useful when extracting system backups.
Using zmore
command
The zmore
command on Ubuntu is used to view compressed text files (usually with the .gz
extension) directly in the terminal.
View Compressed File
zmore file1.gz
Search in the file Without Extracting
zmore file1.gz | grep "date"
Check How Many Lines is in the File
zmore file1.gz | wc -l