Efficient File Search in Linux/Unix: A Guide to Using ‘find’ by Name

The find command is a powerful tool for searching files and directories based on various criteria, including the filename. Here are some examples of how to use find to search for files by name:

1. Find Files by Exact Name

To find a file named example.txt in the current directory and its subdirectories:

find . -name "example.txt"
find by name

2. Find Files by Wildcard

To find files that match a pattern, such as all .txt files:

find . -name "*.txt"

3. Find Files with a Specific Extension

To find all files with a .jpg extension:

find . -name "*.jpg"

4. Find Files with a Specific Prefix

To find all files that start with the prefix out:

find . -name "out*"

5. Find Files with a Specific Suffix

To find all files that end with _log:

find . -name "*_log"

6. Find Files with a Specific Substring

To find all files that contain the substring report:

find . -name "*report*"

7. Find Files with a Specific Case-Sensitive Name

To find a file named Example.txt (case-sensitive):

find . -name "Example.txt"

8. Find Files with a Specific Case-Insensitive Name

To find a file named example.txt ignoring case:

find . -iname "example.txt"

9. Find Files with a Specific Name in a Specific Directory

To find a file named example.txt in the /home/user/documents directory and its subdirectories:

find /home/user/documents -name "example.txt"

10. Find Files with Multiple Names

To find files named example.txt or example.log:

find . \( -name "example.txt" -o -name "example.log" \)

11. Exclude Files with Specific Names

To find all files except those named example.txt:

find . ! -name "example.txt"

12. Exclude Files with Specific Patterns

To find all files except those that start with test:

find . ! -name "test*"

13. Exclude Files with Multiple Patterns

To find all files except those named example.txt or example.log:

find . \( -name "example.txt" -o -name "example.log" \) -prune -o -print

14. Find Files with Specific Names in Multiple Directories

To find files named example.txt in both /home/user/documents and /var/log:

find /home/user/documents /var/log -name "example.txt"

15. Find Files with Specific Names and Perform an Action

To find files named example.txt and delete them:

find . -name "example.txt" -exec rm -i {} \;

Example Usage

Let’s say you want to find all .txt files in the /home/user/documents directory and its subdirectories:

find /home/user/documents -name "*.txt"

Conclusion

These examples demonstrate how to use the find command to search for files by name using various patterns and criteria. You can combine these options with other criteria, such as file size, modification time, and file type, to create more complex queries. If you have specific requirements or need more advanced functionality, feel free to ask, and I can provide more tailored examples.

This entry was posted in Find Command, File & Directory and tagged , , , . Bookmark the permalink.