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 am going to discuss some of the most common search command examples that you will probably 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 directoryis 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.
  • Expressionallows you to specify the search term. If you want to search for a file by its name, the expression is the name of the file. If you want to search for files whose name matches a pattern, enter an expression in the pattern.

Let me take a simple example:

found -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. die option-Type ftells it to only search for files. the single point.means the current directory.

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

Search files and directories by name

You canSearch files and directories by their names:

Meet . -name SEARCH_NAME

Since no file type is specified, it looks for both 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 out. Linux Handbook Team LHB

Search only files or only directories

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

Meet . -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:

Meet . -type d -name SEARCH_NAME

In the above file, just search for 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 with the specified name using-arcoinstead of-Name.

Meet . - Type f -iname SEARCH_NAME

you can use it withtype deven.

[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 search for files of a specific type, or should I say a specific extension.

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

found -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 expression in double quotes when using the search command.

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

If you don't put your 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 is only one file, but if there is 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, and therefore the command expands tofound -type f -filename.cpp, it works becauseData.cppit still works as a search term.

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

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

Search for multiple files with multiple extensions (or conditions)

The above command searched 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:

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

Here's 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 /new.txt./other.txt

Find files in a specific directory

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

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

[Email protected]:~/Examples$ find ./new -mycosasname ./new/mycosas

Find files in multiple directories

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

find ./location1 /second/location -type f -name "pattern"

Find empty files and directories

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

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

Meet . -empty

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

Meet . -blank -type f

You can also combine it with filename lookup:

Meet . -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 less than N.

Find files exactly 50KB in size:

Meet . -50k size

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

Meet . -Size +1G

To find less than 20 bytes:

Meet . -size -20c

To find files larger than 100 MB but less 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 500 MB 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 modification or creation 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, modified time search helps.

To find all files changed within 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 time frame. What if you want to find files that were only changed a few minutes ago? For this you can usemillimeter,inycm.

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 in the last 20-30 minutes.

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

Find files with specific file permissions

I 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 with read and write access to everyone (exact match, will not match if the file has execute permission to everyone):

found -permanent a=r+w

Find files owned by a user

You can also search for 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 such as size, time and name:

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

Don't search recursively, just search in the current directory

By default, the find command searches all subdirectories of your current location. If you don't want that, 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, prune, and logical or .

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

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

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

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

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

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 output of the Find command:

  • use executive
  • use xargs

Using find and run

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

found -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 octubre 19:30 ./another.txt-rw-rw-r-- 1 abhishek abhishek 35 13 de octubre 15:36 ./new/dir1/new.txt-rw-rw-r-- 1 abhishek abhishek 35 13 de octubre 15:36 ./new/dir2/another.txt-rw-rw- r-- 1 abhishek abhishek 35 13 de oct. 18:16 ./nuevo.txt

A lot of people forget to add that{} +at the end of the exec command. You should 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:

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

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,{} \;has the advantage of using{}more than once in the same executive statement. 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 locating files based on a number of criteria. The exec command allows you to work on these results. Here are some examples of the find exec command combination.Linux HandbookSagar Sharma

using xargs

Many Linux users are getting used to pipe redirection. This exec command with the ending{} +it seems intimidating.

This is where xargs helps. Just analyze the output of the Find command in thexargs commandon 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 the 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 the 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 is one of the most common and useful combinations.

You use find to search for filename patterns, and then you use grep to search for 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:

found -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 of examples, but if you are familiar with itgrep command, you can use it according to your wishes and needs.

There is much more to find with...

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

I hope you find these search command examples helpful. If you still have questions or suggestions to improve this article, please let me know in the comment section.

Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated: 04/05/2023

Views: 5975

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.