Home

 › 

Articles

 › 

How-to

 › 

Software

 › 

How to Use the Find Command in Linux with Examples

linux distros

How to Use the Find Command in Linux with Examples

You’ll use the “find” command most frequently when working with Linux or any other Unix-like operating system. It’s self-explanatory in what it does; it simply finds things. But little do you know this basic command can find what you need in multiple ways. 

The find command can search for a file based on filename, permissions, users, type, size, and date, among other criteria. Knowing how to use the find command in Linux with different conditions can help you retrieve files quickly. Some commands are more advanced, so you probably won’t use them that frequently. If you’re a beginner, you can work your way up by first understanding the simpler examples of the find command. 

Find Command in Linux Syntax

Here’s the general syntax of the find command in Linux:

$ find [where to begin the search] [-options] [what to find]

In the above syntax, [-options] is the criterion for the search, which essentially defines the command and tells the operating system what to do. 

Here are some of the -options you’ll use:

  • -exec CMD
  • -ok CMD 
  • -inum N 
  • -links N 
  • -name demo
  • -newer file 
  • -perm octal 
  • -print
  • -empty 
  • -size +N/-N 
  • -user name 

How to Use Find Command in Linux Examples

So there are tens of different ways you can use the find command in Linux to search for a file. You can also use the same find command not just to locate the file but also to carry out other actions to, for example, delete the file. 

To help you better understand how this type of command works in Linux, here are several examples, from basic to advanced. 

Example 1: Search for a File With Its Name

This command is straightforward as it searches for the file using its name in your defined directory. If you know the name of the file and its directory, you can use this command to pull it out easily. 

Use find command in Linux 1
Returns “sample.txt” files

This command will look for files named “sample.txt” in the home directory. 

Example 2: Search for All Files With the Same Extension

Suppose you don’t remember the name of the file, but you do remember the type. You can retrieve all files with a specific extension or type using this command. 

Use find command in Linux 2
Returns .txt files in your home directory.

This will return all .txt file types in the home directory. 

Example 3: Search for a File With the Name in the Current Directory

If the file you’re looking for is in the current directory, you don’t need to mention the directory in the command. Here’s how it goes:

Use find command in Linux 3
Returns “sample.txt” file in your current directory.

This will search for the file titled “sample.txt” in the current directory. 

Example 4: Search for a Directory

You can also use the find command in Linux to search for directories. Here’s an example of a find command to search for a directory named HC.

Use find command in Linux 4
Move to HC directory.

This command will take you to the directory named HC. 

Example 5: Search for a File With Its Name, But Without Case Sensitivity

Linux commands are case-sensitive, so you have to be careful with the file and directory names. But there’s a workaround find command for finding files based on name, regardless of their case. Again, this can be helpful if you’re unsure about the case of the filename. 

Use find command in Linux 5
Returns sample.txt or Sample.txt file.

This command will return “sample.txt”, even if its actual name is “Sample.txt”. 

Example 6: Search by Type

In instances where you may have a directory and file having the same name, you can make your search accurate by specifying the type of object you’re looking for. For instance, you can specify that you’re searching for a file with the -type option. 

Use find command in Linux 6
Return sample.txt file

This command will return only files titled “sample”, which are text files. 

Example 7: Search for Text in Files

Suppose you want the contents of the file, like a string in a text file. You can use find to retrieve the text directly. The find command can also be tweaked to search for text within one or multiple files. You’ll need to provide the string you’re looking for. 

Use find command in Linux 7
Returns all lines in .txt files that include the words “History-Computer”.

This command will print text lines with the string “History-Computer” in all .txt files in all the directories. 

Example 8: Search for All Empty Files

If you’re looking to clean your directory, you’ll want to delete any empty files. But how do you know which files are empty? You can use the find command to look for empty files.

Use find command in Linux 8
Returns empty files in your home directory.

This command will return all the empty files in the home directory. 

Example 9: Search for All Empty Directories

Like empty files, you can also use the find command in Linux to search for empty directories. 

Use find command in Linux 9
Returns all empty directories and sub-directories in your current directory.

This command will return all the empty directories or sub-directories. 

Example 10: Search for All Hidden Files

The find command is also useful for finding hidden files in a directory. The command is pretty straightforward. 

Use find command in Linux 10
Returns all hidden files in your home directory.

This command returns all the hidden files in the home directory. 

Example 11: Search for Files With Particular Permissions

When working with Linux operating system with multiple users, you may want to ensure your files have the right permissions, so not everyone can access them. You can use the find command to filter the search by the given permissions. For instance, you want to find files in the current directory whose permissions are 664. Here’s how you’ll do that:

Use find command in Linux 11
Returns all files in your home directory with 664 permissions.

This command will return all the files in the home directory with permission set to 664. 

Example 12: Search for Files Without Particular Permissions

Just as you can use the find command to find a file with specific permissions, you can also find files without permissions. Here’s an example for finding files whose permissions are not 664:

Use find command in Linux 12
Returns files that don’t have 664 permissions.

This shall return only files whose permissions do not include 664. 

Example 13: Search for All SUID Files

Set Owner User ID is a permission bit for executable files in Linux. An alternate user can run this file with the same permissions as the owner. Here’s how you can find all the SUID files. 

Use find command in Linux 13
Returns all SUID files.

This command returns all the SUID files. 

Example 14: Search for All SGID Files

SGID is similar to SUID but for groups. Here’s how you can search for all SGID files. 

Use find command in Linux 14
Returns all SGID files

This command returns all the SGID files.

Example 15: Search for All Executable Files

Looking for an executable file but don’t know the name? You can also use the -perm option to search for all the executable files in current or all directories. 

Use find command in Linux 15
Returns all executable files.

This command will return executable files in the home directory. 

Example 16: Search for Read-Only Files

You can use the find command if you want read-only files in the current directory or any directory. This can be helpful to ensure that certain important files have read-only permission. Here’s how you do it for all the directories. 

Use find command in Linux 16
Returns all read-only files.

This will return read-only files in the home directory. Of course, you can also specify the directory and/or file type. 

Example 17: Search and Delete a File

If you want to delete a file, you can do it with the find command. Here’s one example of such a command:

Use find command in Linux 17
Searches for and deletes “sample.txt” file.

Example 18: Search and Delete Multiple Files

You can also delete two or more files using a find command with some tweaks. Here’s what it should look like. 

Use find command in Linux 18
Searches for and deletes all .txt files.

This command will delete all .txt files in the home directory. You can also specify another directory or go with all directories. 

Example 19: Search for a File Based on User

The option “-user” allows you to search files for a specific user on the operating system. This can be used to further narrow the search and ensure only the right file returns. If you want to find a single file for a specific user, here’s an example of a command:

Use find command in Linux 19
Returns “sample.txt” file for user “historycom”.

This find command in Linux will return the specific file for this particular user. 

Example 20: Search for All Files for a User

Perhaps you want to check which files belong to a certain user. You can search for all files for a specific user using a similar find command. 

Use find command in Linux 20
Returns all files for user “historycom”.

In the above command, “historycom” is the username. This will return all the files for this specific user. 

Example 21: Search for Specific Files for a User

You can use the find command to search only for a specific type of user files. Here’s an example:

Use find command in Linux 21
Returns all .txt files for user “historycom”.

This command will return all .txt files for the user historycom. 

Example 22: Search for All Files for a Group

As Linux also has groups, files can be shared. One way to check which files belong to a particular group, is to search for files for a single group. Here’s what that would look like:

Use find command in Linux 22
Returns all files for the group “historycom”.

In the above command, “historycom” is the group’s name. This command will return all the files for this particular group. 

Example 23: Search for All Files Changed or Modified in the Last Hour

If, for any reason, you want to check which files were modified or changed in the last hour (for example, in the case of an attack), you can use the find command in Linux to search for recently changed or modified files quickly. The commands for changed and modified files are different. 

Here’s an example of a command to find all files changed in the last hour:

Use find command in Linux 23
Returns all files changed in the last 60 minutes.

Here’s an example of a command to find all files modified in the last hour:

Use find command in Linux 24
Returns all files modified in the last 60 minutes.

Example 24: Search for All the Files Accessed in the Last Hour

Another commonly used find command is searching for files accessed in a particular time frame. Here’s how you can find all the files accessed in the last hour:

Use find command in Linux 25
Returns all files accessed in the last 60 minutes.

This will give you all the files accessed in the last 60 minutes. You can specify a directory as well. 

Example 25: Search for All Files Accessed in the Last 10 Days

Like the above command, you can also search by the number of days for files accessed. Here’s what such a command will look like:

Use find command in Linux 26
Returns all files accessed in the last 10 days.

This will return all the files accessed in the last 10 days. 

Example 26: Search for All Files Modified or Changed in the Last 10 Days

If you changed or modified a file in the recent past and forgot which one it was, there’s a simple find command to locate such a file. The find commands for finding modified and changed files in the past are similar. Here’s the command for modified files:

Use find command in Linux 27
Returns all files modified in the last 10 days.

Here’s the command for changed files:

Use find command in Linux 28
Returns all files changed in the last 10 days.

Example 27: Search for All Files Accessed in a Timeframe

If you want to search for files accessed in a particular time frame, for example, within the last 20 to 10 days, you can specify the duration of the find command. Here’s an example:

Use find command in Linux 29
Returns all files accessed more than 10, but less than 20, days ago.

This will return all the files accessed more than 10 days back but less than 20 days back. 

Example 28: Search for Files by Size

Suppose you’re making space in a directory and want to delete any large files. You can also search for files by size. For instance, you want all files sized 100MB; here’s the command you’ll use:

Use find command in Linux 30
Returns all 100MB sized files.

This will return all the files sized 100MB from all the directories. 

Example 29: Search for Files by Size Range

Similar to a specific file size, you can use the find command with a size range. 

Use find command in Linux 31
Returns all files between 50 and 100MB in size.

This command will return all files ranging from 50MB to 100MB. 

Example 30: Search and Delete Files of a Particular Size

You can find and delete all files of a specific size. Here’s an example of such a command:

Use find command in Linux 32
Finds and deletes all .txt files that are larger than 50MB.

This will delete all .txt files of 50MB size from all directories. Be careful with this command because you don’t want to delete any file you didn’t aim to delete. So it’s best to specify the name of the file with this command.  

The following video by Corey Schafer provides extremely clear directions for using the find command.

Wrap Up

Now you know many ways to use the find command in Linux to search for files or folders. But that’s not all; you can also do other things like specify the kind of files you need. Most of the commands are straightforward, and you might use them daily. 

While the primary job of the find command is to search for files, you can also include other prompts in the same command, such as deleting files. Ensure you define the path carefully when deleting files using the find command, so you don’t accidentally delete other files.

Frequently Asked Questions

What is the use of find command in Linux?

Find command in Linux searches for directories and files. It comes in handy when you have hundreds or thousands of files. You can define parameters for the find command that can further narrow the search and get you the exact files you need.

How to find and list files in Linux?

You can use the find command to find particular or all files. Similarly, you can use ls command to list all the files in the current or another specified directory.

Can you use the find command to search for hidden files?

The find command can be used to look for hidden files in the current directory or another directory. You can also use the ls command to look for hidden files.

How to look for a file in all directories in Linux?

You can search for a file in all directories by using the find command with a slash (/), which searches the whole system for the specific file you want. You can specify file name and/or other attributes of it in your command like size or date accessed.

How do I search for a file in Unix?

The find command can be used in the Unix operating system to search for one or more files. This command initiates a search through the file hierarchy to look for the file that meets the command conditions, for example, name or file type.

To top