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.
Table of Contents
Find command examples
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
- Find Cheatsheet – Github.
- Find Man page – IBM.
- Find Manual – gnu.org.
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.
find /home/you -name "*.txt" -mtime -60 -print
-60 is 60 days. You can find more info here::
https://www.cyberciti.biz/faq/howto-finding-files-by-date/
This should help. You can adjust it for your liking.
+1 @Ben .
Since it’s missing, I will add that example to the list of find command examples.
Welcome to the community!
Thank you. This works.