Are you searching through folders in Linux to find files? The “find command” is an efficient file detective that can accurately locate files based on conditions, making it easy for beginners to get started!
The basic logic of the find command is simple: find [search path] [matching condition] [action to execute]. You don’t need to memorize the syntax; just remember three common scenarios:
Finding files by name
Want to find all .txt files? Use
find /home -name "*.txt"
, where /home is the search range, and -name specifies the file name (case-sensitive). If you change it to -iname, it will be case-insensitive.
Finding by type (file / directory)
To find all subdirectories under the /var directory:
find /var -type d
(d stands for directory); to find regular files, use -type f, for example,
find /tmp -type f
.
Finding by modification time
Cleaning up old files is super useful!
find /data -mtime -7
can find files modified in the last 7 days, while -30 will find those modified in the last 30 days.