Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the Find Command in Linux?
3.
Syntax of Find Command in Linux
3.1.
Where to start searching from
3.2.
What to look for
3.3.
Action to take
4.
Options Available in Find Command in Linux
4.1.
Name Search
4.2.
Type Search
4.3.
Size Search
4.4.
Modified Time
4.5.
User Ownership
4.6.
Permissions
5.
How to Find a File in Linux from the Command Line
5.1.
Open the Terminal
5.2.
Enter the Find Command
5.3.
Specify What You're Looking For
6.
Examples of Find Command in Linux
6.1.
Finding Files by Name
6.2.
Finding Directories
6.3.
Finding Files by Size
6.4.
Finding Files Modified in the Last 7 Days
6.5.
Finding Files Owned by a User
7.
Frequently Asked Questions
7.1.
Can I search for specific content inside files with the find command?
7.2.
How do I skip a directory when using the find command?
7.3.
Is it possible to delete files using the find command?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Find Command in Linux

Author Sinki Kumari
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

The find command in Linux is a tool that lets you search for files and directories within the filesystem based on specific criteria. It can locate files by name, type, size, and even by the time they were last modified, making it incredibly handy for managing and organizing your files efficiently. 

Find Command in Linux

In this article, we're going to look into everything you need to know about the find command, including its syntax, the options you can use to refine your search, and practical examples to get you started. 

What is the Find Command in Linux?

The find command in Linux is like a search tool specifically for your computer's files and folders. Imagine you have a big box filled with different things and you're looking for one specific toy. In the digital world of your computer, the find command helps you locate your "toy" - which could be any file or folder you need.

This command works by typing it into the terminal, which is a place where you can enter text commands on Linux, instead of clicking around with a mouse. When you use the find command, you tell your computer to look through all the files and folders starting from a certain point (like the top of the box) and check each one to see if it matches what you're looking for.

You can search for files by their name, when they were last changed, their size, and even what type they are (like if it's a document, a picture, or a music file). This is super useful when you have lots of files and need to find something specific without going through each one by hand.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of Find Command in Linux

When we talk about the syntax of the find command, we're discussing the way you need to write it so your computer understands what you're asking for. Think of it like the specific way you ask a question; if you don't use the right words in the right order, you might not get the answer you're looking for.

In its most basic form, the find command looks like this:

find [where to start searching from] [what to look for] [action to take]

Where to start searching from

This is the directory (like a folder) where you want the search to begin. If you don't specify this, the find command will start looking from the current directory you're in, which could be your user folder, for example.

What to look for

Here, you specify the criteria for what you're searching for. This could be the name of the file, its size, or when it was last modified. There are a lot of options here, and you can even combine them to narrow down your search.

Action to take

This part is optional. After finding the files, you can choose to do something with them, like viewing their details or even deleting them. If you don't specify an action, the find command will just list the files that match your criteria.

Here's an example to make it clear:

find /home/user/Documents -name "report.txt"


In this example, /home/user/Documents is where the search starts, -name "report.txt" tells the command to look for a file named report.txt, and since we didn't specify an action, it will just show us where that file is located.

Options Available in Find Command in Linux

When you're using the find command in Linux, think of it as having a set of tools or options that can help you narrow down your search, making it easier and faster to find what you need. These options are like filters you can apply to focus only on the files or directories that match your specific criteria.

Name Search

If you know the name of the file or folder you're looking for, you can use the -name option. This tells the find command to look for files that have the exact name you specify.

Type Search

Sometimes, you might want to find only files or only directories. The -type option lets you do this by specifying f for files or d for directories.

Size Search

If you're looking for files of a certain size, the -size option can be really handy. You can specify the size in kilobytes (k), megabytes (M), or even gigabytes (G) to find files that are exactly the size you need.

Modified Time

Files that were changed or modified within a certain time frame can be found using the -mtime option. You can specify the number of days ago that the files were last modified.

User Ownership

If you want to find files that belong to a specific user, the -user option allows you to search for files owned by that user.

Permissions

The -perm option is used to find files with specific permissions. This is useful if you're looking for files that have certain access rights.

By combining these options with the find command, you can make your search as broad or as specific as you need. This flexibility makes the find command a powerful tool for managing files and directories in Linux.

How to Find a File in Linux from the Command Line

Finding a file in Linux using the command line is straightforward. You just need to open the terminal, which is like the text-based control center of Linux. Here’s how you do it step by step:

Open the Terminal

This can usually be done by searching for 'Terminal' in your apps or pressing a shortcut, often Ctrl+Alt+T.

Enter the Find Command

Type in find followed by the location where you want to start your search. If you're not sure where the file might be, you can use / to start from the root directory, which covers everywhere.

Specify What You're Looking For

Use the -name option followed by the name of the file you want to find. Make sure to put the file name in quotes if it contains spaces or special characters.

For example, if you want to find a file named example.txt, you would type:

find / -name "example.txt"


This command tells your computer to look through every directory, starting from the root, for a file named example.txt.

Remember, searching from the root can take time because it’s checking everywhere. If you have an idea of where the file might be, you can replace / with that directory's path to speed things up.

Examples of Find Command in Linux

Let's look at some practical examples of how you can use the find command in Linux to make your life easier. These examples will help you understand how to apply the options we discussed earlier.

Finding Files by Name

If you want to find a file named notes.txt, you can use the following command:

find /home -name "notes.txt"


This command searches through the /home directory and all its subdirectories for a file named notes.txt.

Finding Directories

To find a directory named projects, you can use:

find / -type d -name "projects"


This tells Linux to search from the root directory for any directory (-type d) named projects.

Finding Files by Size

If you're looking for files larger than 5 Megabytes, the command would be:

find / -size +5M


This command searches the entire system for files that are more than 5 Megabytes in size.

Finding Files Modified in the Last 7 Days

To find files that were modified in the last week, you can use:

find /home -mtime -7


This command looks in the /home directory for files modified in the last 7 days.

Finding Files Owned by a User

If you need to find files owned by the user alex, the command is:

find / -user alex


This searches the entire filesystem for files owned by alex.

Frequently Asked Questions

Can I search for specific content inside files with the find command?

Yes, the find command can be teamed up with tools like grep to look inside files for specific words or phrases.

How do I skip a directory when using the find command?

You can instruct the find command to ignore certain directories by using specific options that tell it to exclude those paths.

Is it possible to delete files using the find command?

Yes, the find command has an option to delete files that match your search criteria, but use it carefully to avoid accidental deletions.

Conclusion

In this article, we have learned about the find command in Linux, a powerful tool for searching files and directories based on various criteria like name, type, size, modification date, and ownership. We've covered its basic syntax, useful options for refining searches, and practical examples to demonstrate its versatility. Whether you're a beginner or an experienced Linux user, learning the find command can significantly enhance your efficiency in managing files on your system.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Linux Reboot Command
Next article
Linux Mount Command
Live masterclass