The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.
find command uses are:
• Search based on modification time (e.g., files edited last week).
• Locate files with specific permissions or content.
• Automate tasks like deleting or executing commands on found files.
Syntax of find Command in Linux
find [path...] [option] [expression]
find . -name filename
• Path: Where to start searching (e.g., ~/Documents).
• Options: Refine your search (e.g., -type for files/directories).
• Expression: Criteria like filenames or sizes.
case 1. how to search based on their size
M for mb
k for kb
G for gb
c for byte
find /path/ -size 50M
case 2. how to find only file or only directory in a given path?
f for files
d for directory
l for symbolic link
b for block device
s for socket
find /path -type f
case 3. how to search a file based on it's name
find /path -name <dilename> # exect name mention
case 4. how to ignore upper & lower case in file name while searching files ?
find /path -iname <filename>
Case 5. How to search files for a given user only ?
find /path –user <username>
Case 6. How to search a file based on the inode number ?
find /path inum <inode_no._of_file>
ls -li # check inode number
Case 7. How to search a based on the no. of links?
find /path -links <no._of_link>
Case 8. How to search a based on their permission
find /path -perm /u=r
find /path -perm 777
Case 9. How to search all the files which start with letter a?
find /path -iname “a*”
Case 10. How to search all the files which are modified/created after last.txt file?
find /path -newer last.txt
Case 11. How to search all the empty files in a given directory?
find /path -empty
Case 12. How to search all the empty files in a given directory and at the same time delete them?
find /path -empty -exec rm {} \;
Case 13. How to search all the files whose size are between 1-50MB ?
find /path -size +1M -size -50M
Case 14. How to search 15 days old files
find /path -mtime 15
Top comments (0)