Using the find command in Linux with examples

This find command guide is a follow-up of my previous 90 Linux Commands frequently used by Linux Sysadmins article. Every week, or as time allows, I will publish articles on the ~ 90 commands geared toward Linux sysadmins and Linux power users. Let’s continue this series with the find command.

The find command is part of findutils which includes essential directory searching utilities for Linux and Unix-like operating systems. It allows for varied search criteria, formatted output, and custom commands to be run on the search results. I’ve also included some cheat sheets and other helpful reading at the end of this article.

Find command examples

Linux find - like a dog digging holes beach searching

To find directories matching a given name, in case-insensitive mode, use:

find path/ -type d -iname '*Dir_name*'

To find files by extension, use:

find path/ -name '*.ext'

To find files matching a path pattern, use:

find path/ -path '**/lib/**/*.ext'

To find files matching multiple patterns, use:

find path/ -name '*pattern1*' -or -name '*pattern2*'

To find files matching a given pattern, excluding a specific directory, use:

find path/ -name '*.py' -not -path '*/exclude_dir/*'

To find file larger than 500k use:

find path/ -size +500k

To find files sized between 500K and 10M, use:

find path/ -size +500k -size -10M

To find files modified in the last 7 days.

find path/ -name "*.txt" -mtime -60 -print

To find files modified in the last 7 days and delete them, use: find path/ -mtime -7 -delete

 

Linux find command – useful reading

Related commands

  • grep – a disk utility for Unix systems.
  • locate – also part of findutils.
  • whereis – Find the correct path to an executable file.

 

Conclusion

We’ve covered the find command used to search for files in a directory hierarchy. However, findutils also includes the locate command, which scans one or more databases of filenames and displays any matches, the updatedb command, which updates the file name database used by the locate command, and lastly the xargs command, which builds and executes command lines by gathering together arguments it reads on the standard input. Most often, these arguments are lists of file names generated by using find. Note, if no files to search are specified, the current directory (.) is used.

Tags: , ,

Discussion

  1. Welcome to the community!



Top ↑