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 take a look at two of the most commonly used 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 a variety of criteria. Here are a few examples of how you can use the find command −

To find all files in the current directory and its subdirectories that have the .txt file extension, you can use the following command 7minus;

find . -name "*.txt"


To find all files in the /home/user directory that have been modified in the last 30 days, you can use the following command −

find /home/user -mtime -30


To find all files in the /home/user directory that are larger than 1GB, you can use the following command −

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 it was originally used to write data to tape drives. Today, it is commonly used to create archive files that can be easily shared or transferred over the internet. Here are a few examples of how you can use the tar command −

To create a new archive file called myfiles.tar that contains all files in the /home/user directory, you can use the following command −

tar -cf myfiles.tar /home/user


To extract all files from the myfiles.tar archive file, you can use the following command −

tar -xf myfiles.tar


To add a new file called file.txt to an existing archive file called myfiles.tar, you can use the following command −

tar -uf myfiles.tar file.txt


It is important to note that the tar command does not compress the files by default. However, it can be used in conjunction with other command such as gzip to compress the files while creating the archive. For example, to create a compressed archive file called myfiles.tar.gz that contains all files in the /home/user directory, you can use the following command −

tar -czf myfiles.tar.gz /home/user


By using the find and tar commands together, you can easily find and manipulate large numbers of files on your Linux system. With a little practice and experimentation, you will soon become proficient at automating tasks and managing files with these powerful command-line tools.

Finding and Compressing Specific Types of Files

In addition to searching for files based on specific criteria such as file size or modification date, the find and tar commands can also be used to search for and compress specific types of files. For example, you may only want to find and compress all image files in a certain directory or all files with the .log extension.

Here are a few examples of how you can use the find command to search for specific types of files −

To find all image files (with extensions such as .jpg, .png, and .gif) in the /home/user directory, you can use the following command −

find /home/user -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif"


To find all files with the .log extension in the /var/log directory, you can use the following command −

find /var/log -name "*.log"


You can then use the tar command to compress the files that were found by the find command. Here are a few examples of how you can use the tar command to compress specific types of files −

To create a compressed archive file called images.tar.gz that contains all image files in the /home/user directory, you can use the following command −

tar -czf images.tar.gz $(find /home/user -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif")  To create a compressed archive file called logfiles.tar.gz that contains all files with the .log extension in the /var/log directory, you can use the following command − tar -czf logfiles.tar.gz$(find /var/log -name "*.log")


Conclusion

The find and tar commands are powerful tools that can be used to find and compress large numbers of files on a Linux system. By using specific search criteria and command options, you can easily find and compress specific types of files such as image files or log files. With a little practice and experimentation, you will soon become proficient at automating tasks and managing files with these powerful command-line tools.