Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find and tar Files on Linux
One of the most powerful features of the Linux operating system is the ability to find and manipulate files quickly and easily from the command line. This can be especially useful when working with large numbers of files or when you need to automate certain tasks. In this article, we will explore two essential command-line tools for finding and compressing files on Linux: the find command and the tar command.
Finding Files with the find Command
The find command is a powerful tool that allows you to search for files on your Linux system based on various criteria such as name, size, modification time, and permissions.
Common find Command Examples
To find all files in the current directory and its subdirectories that have the .txt file extension:
find . -name "*.txt"
To find all files in the /home/user directory that have been modified in the last 30 days:
find /home/user -mtime -30
To find all files in the /home/user directory that are larger than 1GB:
find /home/user -size +1G
Compressing Files with the tar Command
The tar command is used to create, extract, and manipulate archive files on Linux. tar stands for "Tape Archive" and was originally designed for tape drives. Today, it is commonly used to create archive files for sharing or transferring data.
Basic tar Operations
To create a new archive file called myfiles.tar containing all files in the /home/user directory:
tar -cf myfiles.tar /home/user
To extract all files from the myfiles.tar archive:
tar -xf myfiles.tar
To add a new file called file.txt to an existing archive:
tar -uf myfiles.tar file.txt
By default, tar does not compress files. However, it can work with compression tools like gzip. To create a compressed archive:
tar -czf myfiles.tar.gz /home/user
Combining find and tar Commands
The real power comes from combining find and tar to locate specific files and compress them efficiently.
Finding Specific File Types
To find all image files (jpg, png, gif) in the /home/user directory:
find /home/user -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif"
To find all log files in the /var/log directory:
find /var/log -name "*.log"
Creating Archives from find Results
To create a compressed archive of all image files found:
tar -czf images.tar.gz $(find /home/user -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif")
To create a compressed archive of all log files:
tar -czf logfiles.tar.gz $(find /var/log -name "*.log")
Common tar Options
| Option | Description | Example |
|---|---|---|
| -c | Create archive | tar -cf archive.tar files/ |
| -x | Extract archive | tar -xf archive.tar |
| -z | Compress with gzip | tar -czf archive.tar.gz files/ |
| -v | Verbose output | tar -cvf archive.tar files/ |
| -f | Specify archive filename | tar -cf myarchive.tar files/ |
Conclusion
The find and tar commands are essential tools for Linux file management and automation. By combining these commands, you can efficiently locate specific files based on various criteria and create compressed archives for backup or distribution. Mastering these tools will significantly improve your productivity when working with large file collections on Linux systems.
