DEV Community

Gur
Gur

Posted on

How to Find a File in Linux Using Find Command

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

case 3. how to search a file based on it's name

        find /path -name <dilename> # exect name mention
Enter fullscreen mode Exit fullscreen mode

case 4. how to ignore upper & lower case in file name while searching files ?

         find /path -iname <filename>
Enter fullscreen mode Exit fullscreen mode

Case 5. How to search files for a given user only ?

find /path –user <username>
Enter fullscreen mode Exit fullscreen mode

Case 6. How to search a file based on the inode number ?

find /path inum <inode_no._of_file>
    ls -li # check inode number

Enter fullscreen mode Exit fullscreen mode

Case 7. How to search a based on the no. of links?

    find /path -links <no._of_link>
Enter fullscreen mode Exit fullscreen mode

Case 8. How to search a based on their permission

find /path -perm /u=r
    find /path -perm 777

Enter fullscreen mode Exit fullscreen mode

Case 9. How to search all the files which start with letter a?

    find /path -iname  “a*”
Enter fullscreen mode Exit fullscreen mode

Case 10. How to search all the files which are modified/created after last.txt file?

    find /path -newer last.txt 
Enter fullscreen mode Exit fullscreen mode

Case 11. How to search all the empty files in a given directory?

    find /path -empty
Enter fullscreen mode Exit fullscreen mode

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 {} \;
Enter fullscreen mode Exit fullscreen mode

Case 13. How to search all the files whose size are between 1-50MB ?

    find /path  -size +1M -size -50M
Enter fullscreen mode Exit fullscreen mode

Case 14. How to search 15 days old files

    find /path -mtime 15
Enter fullscreen mode Exit fullscreen mode

Top comments (0)