In this tutorial on the way to use the ‘Find command’ in Linuxwe are going to discuss in short what’s find command & will discuss some examples for a similar.

Find command may be a pretty helpful command for simply locating files & folders in Linux. What makes it a robust command is that we will use variety of search criteria/options to refine the search.

It is found & used on the majorityLinux distros by default.

Find Command Syntax

The Basic Syntax of Find Command is

# find  location search-criteria  search-term

Now that we’ve some understanding of what the realize command is & a way to use the realize command in UNIX operating system. Let’s discuss some examples similarly,

Find Command Examples

To Find The Files In System

To find all the files in the ‘/’ folder i.e. root directory, use,

# find / -type f

To search for the file in a particular directory, use,

# find /etc/ -type f

Finding directories on the system

To find all the folders/directories in the ‘/’ folder i.e. root directory, use,

# find / -type d

To look for all the directories in a particular directory, use,

# find /etc/ -type d

Finding files based on the name

If you know the name of the file or folder you are looking for, then you can also use that to make search easy & fast with the following command,

# find /etc -iname “*.txt”

This shows all the files within the /etc folder with extension .txt. One factor to think about here is that it’ll ignore a case-sensitive file. it’ll show all the files ending with .txt however can ignore files ending with .TXT or .Txt.

To include all such files in additionwe are able to use ‘-name’ instead of ‘-iname’, as an example,

# find /etc -name “*.txt”

Invertive name search

Find command can also be used to exclude some files & only show the remaining files, use,

# find /etc -not -name “*.txt”

above command will list all the files & directories that do not have extension “.txt” at the end.

Finding files/directories with size

With the find command, we can also find files based on the file sizes. Use the following example as reference,

# find /etc -type f -size 2M

This will show all the files in the /etc folder with the size of 2 Megabytes.

Combining search criteria

We can also combine more than one search option to produce a more refined search,

# find /etc -name ‘test*’ ! -name ‘*.php’

here, it’ll notice all the files with the name ‘test’ at the beginning in ‘/etc’ folder that doesn’t have extension .php. “!” here is that the equivalent of AND operator.

Also, we are able to mix 2 search criteria & turn out results once any of the 2 search criteria are satisfied.

# find /etc -name ‘test*’ -o -name ‘*.txt’

Here “-o” is equivalent to OR operator.

Search based on file permissions

To find files based on their permissions, use, 

# find /etc -type f -perm 0400

This will show all the files in the /etc folder with the permission of 0644.

# find /etc -type f -perm /u=r

The result for the above command will show all files for a user with only read permissions.

Finding files with user & group ownership

Similar to how we can locate files with particular permissions, we can also use find command to locate files with a particular owner, 

# find / -user dan

Here, we are locating all the files that are created by user ‘dan’. Similarly, we can additionally explore for files or folders that are closely-held by a bunch by replacement -user with -group.

# find / -group dan

Finding files based on their modification time, Access time & Change time

# find / -mtime 10

It will find all the files that were changed within the last ten days. Replace mtime with -atime to search out all the files that were accessed within the last ten days.

# find / -cmin -60 

It will find all the files that were changed in the last 60 minutes.

# find / -mmin -60 

It will find all the files modified in the last 60 minutes.

# find / -amin -60 

It will find all the files accessed in the last 60 minutes.

Listing all the found files

To get all files and present them in order as ‘ls command’ would, use,

# find . -exec ls -ld {} \;

This will show all the files in output as would be shown by ls command.

Finding & deleting the found files

We additionally also can mix some choices to find files & then can also perform associate operation to delete them, beat one command,

# find /etc -type f -name *.txt -size -1M -exec rm -f {} \;

This command can realize all the files with .txt as AN extension with a size of but one computer memory unit & can execute the rm/delete command on found files.

Getting Help

Like with the other Linux command, we are able to conjointly take facilitate from the OS documentation to induce a lot of elaborate info concerning the command. Use,Grammar Check

# find –help

With this, we have a tendency to complete our tutorial on the way to use the find command in Linux. These are just some examples, certainly, there are many ways in which you’ll be able to use the realize command to induce what you would like. If you run into any problems or have any queries, please do send us using the comment box below.