A method to find files on a Linux system is with the well named find command, find allows for certain parameters to be passed allowing for a more specialized and refined search.
find can also be used to execute commands on the returned files.
Simply finding a file by its case sensitive name:
find -name index.php
For the search to not be case sensitive:
find -iname readme.txt
Type
Searching by type;
- c: character devices
- d: directory
- f: regular file
- l: symbolic link
Find files that are of a certain extension:
find / -type f -name "*.log"
Size and time
Search filtering can be done by size too;
- c: bytes
- k: Kilobytes
- M: Megabytes
- G: Gigabytes
All MP4 files greater than 1 Gigabyte:
find / -type f -name "*.mp4" +1G
All MP4 files less than 20 Megabytes:
find / -type f -name "*.mp4" -20M
Time filtering is based around 3 methods:
- Access Time: When the file was read or written to last.
- Change Time: Last time the file’s meta-data was changed.
- Modification Time: Last time the file was modified.
Finding files accessed less than a week ago:
find / -atime -7
Finding with modification time of less than 1 day:
find / -mtime -1
All these commands and filters can be combined to help narrow down or even widen your search.
An example for doing something with the “found” files is this:
find /tmp -type f -name "*.log" -mtime +7 -exec rm -f {} \;
This will delete all the .log files in /tmp that were created over 1 week (7 days) ago.
View the official find docs here or a comprehensive guide here.