CodeNewbie Community 🌱

Cover image for Unlock the power of the find command: A beginner's guide with examples
k1lgor
k1lgor

Posted on • Updated on

Unlock the power of the find command: A beginner's guide with examples


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"
Enter fullscreen mode Exit fullscreen mode

image-1

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
Enter fullscreen mode Exit fullscreen mode

image-2

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
Enter fullscreen mode Exit fullscreen mode

image-3

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
Enter fullscreen mode Exit fullscreen mode

image-4

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
Enter fullscreen mode Exit fullscreen mode

image-5

Another example:

find . -name "example.txt" -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode

image-6

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. The rm command will be executed on each file found and the file will be passed as an argument to rm 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"
Enter fullscreen mode Exit fullscreen mode

image-7

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 {} \;
Enter fullscreen mode Exit fullscreen mode

image-8

Thank you for reading πŸ§‘β€πŸ’»

Stay tuned for more πŸš€

✌️ and logout

Buy Me A Coffee

Top comments (0)