Finding Files
Using the Find & Locate Command Effectively

Growing in DevOps, together! 🤝 | Associate Software Engineer at Tech Mahindra | Enthusiastic about automation, cloud solutions, and efficient software delivery. | Let's connect, collaborate, and learn from each other!
Find
Find Files by Name: (.) represents the current directory
find . -name <file-name>
Find Files by Extension
find -type f -name "*.extention"
Search for directories
find -type d -name <directory-name>
Find Files by Size
Files of size less than [size] use -
Files of size greater than [size] use +
find . -size +[size]

12 Cases of find command
1. How to search a file based on their size?
find /path/ -size 50M
M for MB, k for KB, G for GB, c for bytes
Note: The ls command is displaying the sizes of the files in bytes by default
2. How to find only a file or directory in a given path?
find /path/ -type f
f for file, d for directory, I for symbolic link, b for block device, s for socket
3. How to search a file based on its name?
It is case sensitive
find /path/ -name <file_name>
4. How to ignore upper & lower case in file name while searching?
find /path/ -iname <file_name>
5. How to search files for a given user only?
Use case: Sometimes the directories are shared among different users and they create files in the same directory. So in that case we can use this command to find the files that belong to a particular user.
find /path/ -user root
6. How to search a file based on the inode no .?
An "inode," short for "index node," is a data structure used to store metadata about a file or directory. Each file and directory on a Linux file system is associated with an inode.
Inodes are a fundamental concept in Unix-like file systems and play a crucial role in maintaining the file system structure and tracking information about files.
Use case: In hardlink, same inode is shared among different files.
Note: We will explore inodes in more detail when we dive deeper into links.
Command to find inode
ls -li
find /path/ -inum <inode-no-of-file>
7. How to search a file based on the no. of links?
find /path/ -links <no-of-link>
8. How to search a file based on their permissions?
find /path/ -perm /u=r
find /path/ -perm 777
-perm /u=x: This option is used to search for files that have the execute (x) permission set for the owner (u).

9. How to search all the files which start with letter a?
find /path/ -iname "a*"

Note: The shell is interpreting find . -iname file* as two separate commands: find . is treated as one command. -iname file* is treated as another command.

10. How to search all the files which are created after the last.txt file?
find /path/ -newer last.txt
11. How to search all the empty files in a given directory?
find /path/ -empty
12. How to search all the empty files and delete them? (IQ V Imp)
For files:
find . -empty -delete
For deleting only empty directories:
find . -type d -empty -delete
13. How to search all files whose size are between 1-50 MB
find /path/ -size +1M -size -50M
Note: ls -lh : h: This option is used to make file sizes more human-readable. It will display file sizes in a format that is easier to understand, using units like "K" for kilobytes, "M" for megabytes, and "G" for gigabytes, as needed.
14. How to search 15 days old files
find /path/ -mtime 15
Mtime - modified time
Locate
The locate command in Linux is used to quickly find files and directories by searching a pre-built database, mdb. Unlike find, which searches in real-time, locate retrieves results faster by using an indexed database of file paths.
- Updating the Database
If "locate" command does not output any result, then as a root user run updatedb. Since locate relies on an index, you must update it periodically.
sudo updatedb
- Searches for all occurrences of filename across the system.
locate filename
Difference between Locate and Find.
| Feature | locate | find |
| Speed | Very fast (uses a pre-built index) | Slower (searches in real-time) |
| Database Dependency | Requires an updated database (updatedb) | Searches the file system directly |
| Search Scope | Searches globally in indexed locations | Searches in a specific directory |
| Can Search by File Properties? | No | Yes (-size, -mtime, -type, etc.) |
Yes (-size, -mtime, -type, etc.) | No, unless the database is updated | Yes (real-time search) |
| Best Used For | Fast lookups of known files | Deep searches with detailed filters |




