k1lgor
Posted on January 31, 2023
Introduction
The find
command is a powerful tool in Linux that allows you to search for files and directories in a specified location. With find
you can search for files based on their name, type, size, and many other criteria. In this blog post, we will explore some examples of how to use the find
command, as well as some tips and tricks for getting the most out of it.
Searching for files by name
The most basic use of find
is to search for files by name. For example, to search for all files in the current directory with the name "example.txt", you would use the command:
find . -name "example.txt"
Searching for files by type
You can also search for files by type using the find
command. For example, to search for all directories in the current directory, you would use the command:
find . -type d
Searching for files by size
Another useful option for find
is the ability to search for files by size. For example, to search for all files larger than 100MB in the current directory, you would use:
find . -size +100M
Using logical operators
You can use logical operators like -and
, -or
, and -not
to combine different search criteria. For example, to search for all files with the name "example.zip" that are larger than 100MB, you would use the command:
find . -name "example.zip" -and -size +100M
Executing commands on found files
One of the most powerful features of find
is the ability to execute commands on the files that it finds. For example, to delete all files in the current directory with the name "example.txt", you would use the command:
find . -name "example.txt" -delete
Another example:
find . -name "example.txt" -exec rm {} \;
The command find . -name "example.txt" -exec rm {} \;
is used to search for files named "example.txt" starting from the current directory (.
) and execute the rm
command on each file found.
The
find
command is used to search for files or directories that match certain criteria.The
.
specifies the directory to start the search from, in this case, it's the current directory.The
-name "example.txt"
option specifies the name pattern to search for, in this case, it's "example.txt".The
-exec rm {} \;
option specifies the action to be taken on each file found. Therm
command will be executed on each file found and the file will be passed as an argument torm
using{}
. The\;
is used to terminate the command.
So, this command will search the current directory and all its subdirectories for files named "example.txt" and remove each file that it finds.
Tips and Tricks
find
command is also capable of searching in a specific directory by specifying the path of the directory to search.
find ~/Public -name "example.txt"
Conclusion
The find
command is a powerful tool for searching for files and directories in Linux. With find
, you can search for files based on their name, type, size, and many other criteria, and even execute commands on the files that it finds.
find . -name "*.txt" -exec cat {} \;
Thank you for reading 🧑💻
Stay tuned for more 🚀
✌️ and logout
Posted on January 31, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
January 28, 2023