Find command in linux

In this post, we will see about find command in linux.
Find is an important and one of most widely used command in Unix and Linux operating systems. It is used to search and locate list of files and directories based on conditions you specify for files that match the arguments.

Find command lets you search used in variety of conditions like you can search files by permissions, users, groups, file type, date, size and other possible criteria. Searching by Find command is recursive in a sense that it will search subdirectories as well.


Syntax

$ find [path] [search criteria] [action]

all these options following the Find command are optional, let’s see how

If you type $ find and press enter it will display the pathnames of all files in the current directory and all subdirectories. Same will happen if you type $ find . and press enter.

If you want to search for file named arpit in current directory, use this

$ find / -name arpit

You can also do this by using

$ find . -name arpit

find command by default is case sensitive, if you want to search without case sensitivity

$ find / -iname arpit

this will search for files that match arpit, ARPIT, Arpit and all other combination in current directory.


Searching for Files and Directories Only

You can also search files or directories only using -type option. To find files with name arpit use

$ find -type f -name arpit

To find directories with name arpit use

$ find -type d -name arpit

This will search for only files with name arpit in current and sub-directories removing case sensitivity filter.

$ find / -type f -iname arpit

You can also use wild cards with find command. For instance

$ find / -type f -iname “*.txt”

this will search current and sub-directories for files with .txt extension.


Search based on File Permission

With find command you can also search files based on specific permission. For instance

$ find -perm 777

This will display all files with permission 777 in your current directory.


Search for files that are modified at some specific time/day

If you want to search for files that are modified in last one day using mtime

$ find . -mtime +1

If you want to search for files that are modified in last one min

$find . -mmin +1

if you want to search for files that are modified more then 2 and less than 5 min

$ find . -mmin +2 -mmin -5

Search based on User

If you want to search for files of user arpit in the current and sub-directories, use this

$ find / -user arpit -iname “*.txt”

Search based on File size

To find all files of size 10MB , use.

$ find / -size 10M

You can also find and delete 50MB files and delete them in one single command

$ find / -size +50M -exec rm -rf {} \;

To learn more about Find command, type $ man find on your Linux terminal

That’s all about Find command in linux.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *