15 Super Useful Examples of Find Command in Linux (2023)

The find command is used to find files and directories on the Linux command line.

Find is one of the most powerful and most used commands. It's also one of the larger commands with over 50 options and that makes it a bit confusing, especially when combined with the exec or xargs command.

It is impossible for a system administrator or software developer to avoid using the search command while working on the command line. Instead of fearing it, embrace its power.

I'm going to discuss some of the most common search command examples you're likely to use. But first I want to show you its syntax and usage.

find command in linux

The general syntax of the Find command is:

find [directory to search] [options] [expression]

Everything in parentheses [] is optional. That means you can walk.MeetCommand without options or arguments. All files and directories are simply stored in the current storage location. That's not very useful, is it?

Let's take a closer look:

  • search directorythis is basically where you want to start your search. By default, the search is recursive and starts at your current location.
  • optionsSpecify the type of search, either by name, by type, by modification time, etc. Here more than 50 options are possible.
  • Expressionlets you specify the search term. If you want to search for a file by its name, the expression is the file name. If you want to search for files whose name matches a pattern, enter an expression in the pattern.

Let me give you a simple example:

encontrado -type f -myfilename

This command performs a search in the current directory and its subdirectories to find a file (not directory) with the namemy file. option to die-Type ftells it to only search for files. the only point.means the current directory.

Let's look at some practical examples of the search command.

Search files and directories by name

You maySearch files and directories by their names:

Meet . -name SEARCH_NAME

Since no file type is specified, it looks for files and directories with the given name.

The following example finds files and directories named mystuff:

[Email protected]:~/Examples$ find -name mystuff./new/mstuff./mstuff
Finding files by name in Linux [5 common use cases] Finding files by name is one of the most common scenarios for finding files in Linux. Here are some examples to help you: Linux Handbook Team LHB

Search only files or only directories

If you just want to search for files, specify file type -f :

Encontrar . -type f -name SEARCH_NAME

The order of type and name does not matter. Take the example above and just search for files:

[Email protected]:~/Ejemplos$ find -type f -name miscosas./miscosas

If you only want to search directories, type -d:

Encontrar . -type d -name SEARCH_NAME

In the above file, just look for the directories:

[Email protected]:~/Ejemplos$ find -type d -name mystuff./new/mystuff

Perform a case-insensitive search

By default, the Find command is case sensitive. You can perform a case-insensitive search on the specified name using-arcorather than-Name.

(Video) Linux Command-Line Tips & Tricks: Over 15 Examples!

Meet . - Type f -iname SEARCH_NAME

you can use it withtype duntil.

[Email protected]:~/Examples$ find -iname mystuff./new/mystuff./mystuff./mystuff

Screenshot of the three examples above:

15 Super Useful Examples of Find Command in Linux (3)

Find files by their extension (important)

One of the most common uses of the Find command is to look for files of a specific type or, should I say, a specific extension.

Suppose you want to find all C++ files in current directories. C++ files end with a .cpp extension, so you can search for them like this:

encontrado -type f -name "*.cpp"

This tells the find command to look forenter fileand with names ending in.cpp.

[Email protected]:~$ encontrado. -escriba f -nombre "*.cpp"./file.cpp./.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-1.1.3/src/zlib/contrib/iostream2/zstream_test.cpp. /.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-1.1.3/src/zlib/contrib/iostream/test.cpp./.cargo/registry/src/github.com-1ecc6299db9ec823/libz- sys-1.1.3/src/zlib/contrib/iostream/zfstream.cpp

Always enclose your search phrase in double quotes when using the search command.

why do i recommendwith single or double quotesaround your search term? Because if you don't, the shell will expand the wildcard.

If you don't enclose the search term in quotes:

found -type f -name *.cpp

Your shell will expand *.cpp and replace it with all files in the current directory with names ending in .cpp.

This might work if there's only one file, but if there's more than one your shell will complain about incorrect syntax.

15 Super Useful Examples of Find Command in Linux (4)

In the example above, there is only one cpp file, so the command expands toencontrado -type f -filename.cpp, it works becauseData.cppstill works as a search term.

But there are two .txt files in the same directory, so when the command is expandedfound -type f -name other.txt new.txt, he complains that there is now more than one search term.

For this reason, you should always enclose your search term in double quotes.

Search multiple files with multiple extensions (or conditions)

The above command looked for files with a specific extension. What if you want to search for files with different extensions?

Instead of running the find command multiple times, run it once with theOption that works as a logical OR condition:

encontrado -type f -name "*.cpp" -o -name "*.txt"

Here is an example:

[Email protected]:~/Beispiele$ finden. -type f -name "*.txt" -o -name "*.cpp"./new.txt./file.cpp./new/new.txt./new/dir2/other.txt./new/dir1 /novo.txt./outro.txt

Find files in a specific directory

So far, all examples have searched the current directory because you specified it. in the examples.

The dot can be replaced by aabsolute or relative route of a directoryso that you can search for files in the specified directory without leaving your current location.

(Video) 15 Useful Linux Commands Every Linux User Needs | Learning Terminal Part 1

[Email protected]:~/Examples$ find ./new -mystuffname ./new/mystuff

Find files in multiple directories

If you think that the files you want may be in multiple locations, you don't need to run the find command multiple times. Just specify all directory paths to search in the search command:

find ./location1 /second/location -type f -name "padrão"

Find empty files and directories

Die-office hourYou can use the search option to look for empty files and directories.

To find all empty files and directories in the current directory, use:

Meet . -empty

You can specify the file type to search only empty files or directories:

Encontrar . -blank -type f

You can also combine it with filename search:

Encontrar . -blank -type f -name "*.cpp"
15 Super Useful Examples of Find Command in Linux (5)

Find large or small files (search by file size)

It can find large or small files based on the search performed by the size parameter. This only works with files, not directories.

you use the-SizeOption with +N for size greater than N and -N for size smaller than N.

Find files exactly 50 KB in size:

Meet . -50k size

To find files larger than 1GB in the current directory:

Meet . -Size +1G

To find less than 20 bytes:

Meet . -size -20c

To find files larger than 100 MB but smaller than 2 GB:

Meet . -Size +100M -Size -2G

You can also combine the size search with the name search. For example, to find all files with names ending in .log but larger than 500MB in the root directory, you could use:

find / -size +500M -name "*.log"

Call back:

  • C: bytes
  • k: kilobytes
  • METRO: megabyte
  • GRAMS: Gigabyte

Find recently modified files (search based on modified or created time)

you know themConcept of mtime, atime and ctime, On the right?

  • mtime: time the file was last modified
  • ctime: file creation time
  • atime: time the file was last accessed

You will often find yourself in situations where you want to find all recently changed files. In such cases, the modified time search helps.

To find all files changed in 3 days (3*24H), use:

Meet . -type f -mtime -3

To find all files created at least 5 days ago (5*24H), use:

Meet . -type f -ctime +5

I know that 24 hours is a huge amount of time. What if you want to find files that were changed just a few minutes ago? For that you can usemillimeter,noycm.

(Video) Top 15 Linux Commands

To find all files changed in the last 5 minutes, use:

Meet . -Type f -mmin -5
15 Super Useful Examples of Find Command in Linux (6)

You can specify the upper and lower limits along with the search name. The following command finds all .java files that have been modified within the last 20-30 minutes.

Encontrar . -type f -mmin +20 -mmin -30 -name "*.java"

Find files with specific file permissions

Hope you know your wayConcept of File Permissions in Linux.

You can use the find command to find files with specific file permissions and access modes.

find -perm modus

For example, to find all files with access mode 777 in the current directory;

Meet . - Permanent 777

To find all files that have read and write access to everyone (exact match, will not match if the file has execute permissions for everyone):

found -permanent a=r+w

Find files owned by a user

You can also search files based on ownership.

For example, to find files owned by user John in the current directory, use:

Meet . -Type f -User John

You can also combine it with other options like size, time and name:

encontrado -type f -user John -name "*.cpp"

Don't search recursively, just search the current directory

By default, the find command searches all subdirectories of your current location. If you don't want this, you can set the search depth to 1. This limits the search to the current directory only and excludes all subdirectories.

found - max. depth 1 -type f -name "*.txt"
15 Super Useful Examples of Find Command in Linux (7)

Exclude a directory from the search

If you want to exclude a directory from the search, you can do so by combining path, removal, and logical or .

found -path "./exclude_directory/*" -prune -o -name SEARCH_NAME

Be careful with the * in the directory path,-Plumby the way andafter pruning.

Basically, the prune command asks not to use the value specified by the path. Plum is always includedto ensure that the right side of the terms is only evaluated for directories that have not been cleaned up.

Act on the result of search commands (exec and xargs)

By now you've seen different ways to find files based on different criteria. It's good. However, you can improve it by performing certain actions on the search command result.

For example, how about finding files that match a certain naming pattern and renaming them all at once, or finding empty files and deleting them?

You know itpipe bypasscan be used to combine the output of one command with the input of another command. However, this doesn't work with the output of the find command, at least not directly.

You have two options if you want to edit the Find command output:

  • use executive
  • use xargs

Using find and execute

Suppose you want to include search files in a long list using the search command (ls -l). This is what you use:

(Video) 60 useful Linux commands in 15 minutes

encontrado -type f -name "*.txt" -exec ls -l {} +

Here is the output:

[Email protected]:~/Beispiele$ finden. -type f -name "*.txt" -exec ls -l {} +-rw-rw-r-- 1 abhishek abhishek 39 13 de outubro 19:30 ./another.txt-rw-rw-r-- 1 abhishek abhishek 35 13 out 15:36 ./new/dir1/new.txt-rw-rw-r-- 1 abhishek abhishek 35 13 out 15:36 ./new/dir2/another.txt-rw-rw-r- - 1 Abhishek Abhishek 35 13 out. 18:16 ./nuevo.txt

Many people forget to add this{} +at the end of the exec command. You must use it and pay attention to the space between {} and +.

The {} refers to the result of the search command. You can think of it like this: {file 1, file 2, file 3}. The+The ampersand is used to end the exec command.

There is also another convention with exec:

encontrado -type f -name *.txt" -exec ls -l {} \;

On here, ; is used instead of the + sign. The extra \before ; is used to represent the special character; to mask.

the benefit of{} +is that it executes fewer commands thanls -l Fechai1 Fechai2 Fechai3while{} \;will be executedls -l Fechai1,ls -l Fechai2etc.

But,{} \;have the advantage of using{}more than once in the same executive declaration. For example, the following command renames all found files with an .old extension.

encontrado -type f -name *.txt" -exec mv {} {}.old \;
Find Exec Command on Linux: 9 Useful Examples Find works by finding files based on various criteria. The exec command lets you work with these results. Here are some examples of the find exec command combination.Linux HandbookSagar Sharma

using xargs

Many Linux users are getting used to channel redirection. This exec command with the ending{} +looks intimidating.

This is where xargs helps. Just parse the output of the Find command inxargs commandno tube

found -type f -name *.txt" | xargs ls -l
15 Super Useful Examples of Find Command in Linux (10)

The syntax seems much simpler, doesn't it? The Xargs command is also very powerful. You can read about it here.

How to use Xargs command in Linux [explained with examples] xargs is one of the most powerful Linux commands. In this tutorial you will learn how to use xargs command with some practical and useful examples Linux Handbook Abhishek Prakash

combination of find and grep

Now you knowCombination of find with execand xargs, you can use it to combine find and grep.

For any system administrator or software developer, find and grep are one of the most common and useful combinations.

You use find to search for filename patterns and then use grep to search the contents of those files.

For example, you want to find all .txt files that contain the term Alice. You combine find and grep like this:

encontrado -type f -name "*.txt" -exec grep -i alice {} +

The same can also be achieved with xargs:

found -type f -name "*.txt" | xargs grep -i alice
15 Super Useful Examples of Find Command in Linux (13)

Of course, this is the simplest example, but if you're familiar with itgrep command, you can use it according to your wishes and needs.

There is so much more to find with...

And it's not possible to list all the Find command options and examples. The possibilities are endless, but once you become familiar with the Find command, you can use it in a variety of situations. It's up to you how to combine the logic here.

I hope these examples of search commands are helpful. If you still have questions or suggestions to improve this article, let us know in the comments section.

(Video) How to use the FIND command in Linux | The FIND command


1. 60 Linux Commands you NEED to know (in 10 minutes)
2. The 50 Most Popular Linux & Terminal Commands - Full Course for Beginners
3. Using the Find Command! Linux Terminal 201 - HakTip 162
4. Linux commands : Clear your Linux Basics in 25 min for beginners (Hindi)
(Saksham the computer guy)
5. Important Linux Commands for Beginners: Linux Tutorial
6. Important search commands in Linux ( Tamil )
(HSN 24x7)


Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated: 11/09/2023

Views: 6151

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.