Files Operation: Extracting Files
Linux Files Operation: tar and zmore Extracting Files is a routine task for Linux users, whether you’re backing up files, transferring data, or inspecting system logs. The tar
utility is a cornerstone for managing these archives, allowing you to create, extract, and modify .tar
files with ease. Complementing it, tools like zmore
enable users to view compressed archive contents without the need to extract them fully, saving both time and space.
In this guide, we’ll walk you through extracting files from archives using tar
and previewing compressed data with zmore
. With practical examples and tips, you’ll master these essential tools for managing files and archives efficiently.
Let’s get started!
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 Compress Useful Examples
The tar
command is a powerful tool in Linux for archiving and compressing files. It is commonly used to package multiple files into a single archive, making it easier to store, transfer, or back up data. By combining tar
with compression options like gzip
(.gz
) or xz
(.xz
), you can significantly reduce file sizes while maintaining their structure. In this article, we will explore useful examples of how to compress files and directories efficiently using tar
.
Compress a Single File / Compress Multiple Files
Useful examples:
tar -czvf archive.tar.gz file.txt
-c
→ Create an archive-z
→ Use gzip compression-v
→ Verbose mode (shows files being processed)-f
→ Specify archive file name
tar -czvf archive.tar.gz file1.txt file2.txt file3.txt
This will compress file1.txt
, file2.txt
, and file3.txt
into archive.tar.gz
.
Compress a Directory
tar -czvf archive.tar.gz my_directory/
This will recursively compress all files and subdirectories inside my_directory
.
Exclude Specific Files or Directories
tar --exclude="cache/*" -czvf archive.tar.gz my_directory/
This will compress my_directory
but exclude anything inside the cache/
subdirectory.
List Contents of a Compressed Archive
tar -tzvf archive.tar.gz
This will list all files inside archive.tar.gz
without extracting them.
tar Extracting Files Useful Examples
The tar
command is widely used in Linux for archiving files, but it is equally important for extracting them. Whether you need to unpack a backup, retrieve specific files from an archive, or extract compressed data, tar
provides efficient options to handle different scenarios. In this article, we will go through useful examples of how to extract files and directories from tar
archives, including handling .tar.gz
, .tar.xz
, and selective extraction.
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
Please check other commands example on my blog.