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

The find command is used for searching for files and directories in the Linux command line.

Find is one of the most powerful and frequently used commands. It is also one of the most extensive commands with over 50 options and this makes it a bit confusing, specially when it is paired with the exec or xargs command.

It is impossible for a sysadmin or software developer to avoid the find command while working in the command line. Instead of being afraid of it, you should embrace its power.

I am going to discuss some of the most common examples of the find command that you are likely to use. But before that, let me show you its syntax and how to use it.

Find command in Linux

The general syntax for the find command is:

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

Everything in brackets [] are optional. It means that you can run find command without any options and arguments. It will just dump all the files and directories in the current location. That's not very useful, right?

Let's look at it in more detail:

  • directory to search is basically the location from where you want to start your search. By default, the search is recursive and starts from your current location.
  • options specify the type of search, be it by name, by type, by modified time etc. There are more than 50 options possible here.
  • expression allows you to specify the search term. If you want to find a file by its name, expression is the file name. If you want to find files with name matching a pattern, expression in the pattern.

Let me take a simple example:

find . -type f -name myfile

This command will run a search in the current directory and its subdirectories to find a file (not directory) named myfile. The option -type f asks it to look for files only. The single dot . means the current directory.

Let's see some practical examples of the find command.

Find files and directories by name

You can search for files and directories by its name:

find . -name SEARCH_NAME

Since there is no file type mentioned, it searches for both files and directories with the given name.

The below example finds both file and directories named mystuff:

[emailprotected]:~/Examples$ find -name mystuff./new/mystuff./mystuff
Find Files by Name in Linux [5 Frequent Use Cases]Finding files by their name is one of the most common scenarios of finding files in Linux. Here are a few examples to help.Linux HandbookTeam LHB

Find only files or only directories

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

find . -type f -name SEARCH_NAME

The order of type and name does not matter. Take the previous example and find for files only:

[emailprotected]:~/Examples$ find -type f -name mystuff./mystuff

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

find . -type d -name SEARCH_NAME

In the previous file, look for directories only:

[emailprotected]:~/Examples$ find -type d -name mystuff./new/mystuff

Run a case-insensitive search

By default, the find command is case sensitive. You can run a case-insensitive search with the given name by using -iname instead of -name.

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

find . -type f -iname SEARCH_NAME

You can use it with type d as well.

[emailprotected]:~/Examples$ find -iname mystuff./new/mystuff./MyStuff./mystuff

Screenshot of above three examples:

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

Search files by their extension (important)

One of the most common use of the find command is to find files of a specific type or should I say a specific extension.

For example, let's say, you want to search for all the C++ files in the current directories. The C++ files end with extension .cpp, so you can search it like this:

find . -type f -name "*.cpp"

This way, you tell the find command to look for type file and with names that end with .cpp.

[emailprotected]:~$ find . -type f -name "*.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 put your search expression in double quotes when using the find command.

Why do I recommend using double quotes or single quotes around your search term? Because if you do not do that, the shell will expand the wildcard.

If you do not wrap your search term in quotes:

find . -type f -name *.cpp

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

This could work if there is only one file but if there are more than one, your shell will complain of incorrect syntax.

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

In the above example, there is only one cpp file and hence when the command expands to find . -type f -name file.cpp, it works because it file.cpp still works as search term.

But there are two .txt files in the same directory and hence when the command expands to find . -type f -name another.txt new.txt, it complains because there is more than one search term now.

This is why you should always wrap your search term in double quotes.

Search for multiple files with multiple extensions (or condition)

The above command searched for files with a given extension. What if you want to look for files with different extensions?

Instead of running the find command multiple times, run it once by using the -o option that works as logical OR condition:

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

Here's an example:

[emailprotected]:~/Examples$ find . -type f -name "*.txt" -o -name "*.cpp"./new.txt./file.cpp./new/new.txt./new/dir2/another.txt./new/dir1/new.txt./another.txt

Look for files in specific directory

So far, all the examples performed search in the current directory because you specified . in the examples.

The dot can be replaced with an absolute or relative path of a directory so that you can look for files in the specified directory without leaving your current location.

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

[emailprotected]:~/Examples$ find ./new -name mystuff ./new/mystuff

Search for files in multiple directories

If you think your desired file(s) could be located in several locations, you don't have to run find command multiple times. Just specify all the directory paths to search in the find command:

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

Find empty files and directories

The -empty option enables you to look for empty files and directories with the find command.

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

find . -empty

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

find . -empty -type f

You may also combine it with the filename search:

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

Find big files or small (Search based on file size)

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

You use the -size option with +N for size greater than N and -N for size smaller than N.

Find files of exactly 50 KB in size:

find . -size 50k

To search for files bigger than 1 GB in the current directory:

find . -size +1G

To find smaller than 20 bytes:

find . -size -20c

To find files bigger than 100 MB but smaller than 2 GB in size:

find . -size +100M -size -2G

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

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

To recall:

  • c : bytes
  • k: kilobytes
  • M: Megabytes
  • G: Gigabytes

Find recently modified files (Search based on modify or creation time)

You know the concept of mtime, atime and ctime, right?

  • mtime: last modification time of file
  • ctime: creation time of the file
  • atime: last access time of the file

You'll often find yourself in situations where you want to find all the recently modified files. The search by modified time helps in such cases.

To find all the files modified within 3 days (3*24H), use:

find . -type f -mtime -3

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

find . -type f -ctime +5

I know 24 hours is a huge time frame. What if you want to search for files that were modified only a few minutes ago? For that, you can use mmin, amin and cmin.

(Video) You Must Know 20 Important Linux Commands

To find all the files that were modified in the last 5 minutes, use:

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

You can specify upper and lower limits along with the search name. The command below will search for all the .java files that have been modified between last 20 to 30 minutes.

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

Find files with specific file permissions

I hope you are familiar with the file permission concept in Linux.

The find command allows you to search for files with specific file permission and access mode.

find -perm mode

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

find . -perm 777

To find all files with access of read and write for all (exact match, it won't match if the file has execute permission for all):

find . -perm 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 the user John in the current directory, use:

find . -type f -user John

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

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

Don't find recursively, search only in current directory

By default, the find command searches in all the subdirectories of your current location. If you don't want that, you can specify the depth of your search to 1. This will restrict the search to only the current directory and excludes any subdirectories.

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

Exclude a directory from search

if you want to exclude a directory from the search, you can do that by combining path, prune and logical or.

find . -path "./directory_exclude/*" -prune -o -name SEARCH_NAME

Be careful with the * in the path of the directory, -prune after path and -o after prune.

Basically, the prune command asks to not use the value specified by path. Prune is always used with -o to ensure that right side of the terms are evaluated only for directories that were not pruned.

Take action on the result of find commands (exec and xargs)

So far, you have learned various ways to find files based on various criteria. That's good. But you can make it better by taking certain actions on the result of the find command.

For example, how about finding files matching certain name pattern and renaming them all at once or finding empty files and deleting them?

You know that pipe redirection can be used to combine the output of one command with the input of another command. But this won't work with the output of find command, at least not directly.

You have two options if you want to take an action on the result of find command:

  • Use exec
  • Use xargs

Using find and exec

Suppose you want to long list (ls -l) the search files with the find command. Here's what you use:

(Video) 60 useful Linux commands in 15 minutes

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

Here's the output:

[emailprotected]:~/Examples$ find . -type f -name "*.txt" -exec ls -l {} +-rw-rw-r-- 1 abhishek abhishek 39 Oct 13 19:30 ./another.txt-rw-rw-r-- 1 abhishek abhishek 35 Oct 13 15:36 ./new/dir1/new.txt-rw-rw-r-- 1 abhishek abhishek 35 Oct 13 15:36 ./new/dir2/another.txt-rw-rw-r-- 1 abhishek abhishek 35 Oct 13 18:51 ./new/mystuff/new.txt-rwxrwxrwx 1 abhishek abhishek 35 Oct 13 15:37 ./new/new.txt-rw-rw-r-- 1 abhishek abhishek 35 Oct 13 18:16 ./new.txt

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

The {} is what references the result of the find command. You can imagine it to be like {file 1, file 2, file 3}. The + sign is used to terminate the exec command.

There is also another convention with exec:

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

Here, ; is used instead of the + sign. The additional \ before ; is used to escape the special character ;.

The advantage of {} + is that it runs fewer commands as ls -l file1 file2 file3 whereas {} \; will run ls -l file1, ls -l file2 etc.

But, {} \; has the advantage of using {} more than once in the same exec statement. For example, the command below will rename all the found files with .old extension.

find . -type f -name *.txt" -exec mv {} {}.old \;
Find Exec Command in Linux: 9 Useful ExamplesFind works on searching files based on a number of criteria. The exec command gives you the ability to work on those results. Here are some examples of find exec command combination.Linux HandbookSagar Sharma

Using xargs

Many Linux users get used to of the pipe redirection. This exec command with the trailing {} + seems intimidating to them.

This is where xargs helps. You just parse the output of the find command to the xargs command via pipe.

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

The syntax seems a lot simpler, right? Xargs command is also very powerful. You may read about it here.

How to Use Xargs Command in Linux [Explained With Examples]xargs is one of the most powerful commands in Linux. In this tutorial, you’ll learn to use xargs command with some practical and useful examples.Linux HandbookAbhishek Prakash

Combining find and grep

Now that you know about combining find with exec and xargs command, you can use it to combine find and grep.

For any sysadmin or software developer, find and grep is one of the most common and yet most useful combination.

You search for file name patters with find and then use grep to search for the content inside those files.

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

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

The same can be achieved with xargs as well:

find . -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 the examples but if you are familiar with the grep command, you can use it to your liking and need.

There is a lot more with find ...

And it is not possible to list all the find command options and examples. The possibilities are endless but when you get familiar with the find command, you can start using it in a variety of situations. That is really up to you how you combine the logic here.

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

(Video) Using the Find Command! Linux Terminal 201 - HakTip 162


What is the use of find command in Linux? ›

The find command is one of the most useful Linux commands, especially when you're faced with the hundreds and thousands of files and folders on a modern computer. As its name implies, find helps you find things, and not just by filename.

What can we search using find command? ›

The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions.

Which search options can be used with the find command in Linux? ›

The find Command

It supports searching by file, folder, name, creation date, modification date, owner and permissions.

What are 5 main directory commands Linux? ›

Linux Directory Commands
  • pwd Command. The pwd command is used to display the location of the current working directory. ...
  • mkdir Command. The mkdir command is used to create a new directory under any directory. ...
  • rmdir Command. The rmdir command is used to delete a directory. ...
  • ls Command. ...
  • cd Command.

What is the use of find? ›

What is the FIND Function? The FIND Function[1] is categorized under Excel TEXT functions. The function will return the position of a specified character or sub-string within a supplied text string. In financial analysis, we can use this function to locate specific data.

What is find ()? ›

The find() method finds the first occurrence of the specified value. The find() method returns -1 if the value is not found. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found. ( See example below)

What actions does the find command perform? ›

The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments. find command can be used in a variety of conditions like you can find files by permissions, users, groups, file types, date, size, and other possible criteria.

How do I find a file in a directory in Linux? ›

You can use the find command to search for a file or directory on your file system. By using the -exec flag ( find -exec ), matches, which can be files, directories, symbolic links, system devices, etc., can be found and immediately processed within the same command.

How do I find hidden files in Linux? ›

By default, your file manager doesn't display all hidden files. Click on the Menu icon located in the upper-right corner and select Show Hidden Files. Your hidden files and folders will now be visible. Alternatively, you can use the keyboard shortcut Ctrl + H to view hidden files on Linux as well.

Can we use * In find command in Linux? ›

We can make another combination of the find command with the grep command to find the text from the various files. Consider the below command: find ./Newdirectory -type f -name "*. txt" -exec grep 'demo' {} \;

How to use find command in Unix? ›

Find Command in Unix
  1. -atime n: Returns true if the file was accessed n days ago.
  2. -ctime n: Returns true if the file's status was changed n days ago.
  3. -mtime n: Returns true if the file's contents were modified n days ago.
  4. -name pattern: Returns true if the file's name matches the provided shell pattern.
Jan 18, 2023

Which command used to find word in Linux? ›

The most common way to find text in a Linux system is using the command-line utility grep .

What are your top 3 Favourite Linux commands tools? ›

10 Cool Command Line Tools For Your Linux Terminal
  1. Wikit. Wikit is a command line utility to search Wikipedia in Linux. ...
  2. Googler. ...
  3. Browsh. ...
  4. Lolcat. ...
  5. Boxes. ...
  6. Figlet and Toilet. ...
  7. Trash-cli. ...
  8. No More Secrets.
Feb 2, 2019

What does F mean Linux? ›

Many Linux commands have an -f option, which stands for, you guessed it, force! Sometimes when you execute a command, it fails or prompts you for additional input. This may be an effort to protect the files you are trying to change or inform the user that a device is busy or a file already exists.

What is the advantage of find tool? ›

Simply finding where a word is used in a document is faster with the Find function.

What are the uses of find and Replace? ›

Find and replace basic text

, type the word or phrase that you want to find, and Word will highlight all instances of the word or phrase throughout the document. To replace found text: Select the magnifying glass, and then select Replace. In the Replace With box, type the replacement text.

What do we use find and replace for? ›

Word's Find and Replace function will search your documents for specific text, which can then be highlighted, replaced with different text or formatting, or left as-is.

What is to look or find? ›

Looking is the process and finding is the result. For example, "I was looking for my keys for an hour before I finally found them in my pocket." In other words, when I look for something, I search for it. I try to locate it. When I find it – I notice it, or have it.

What type of verb is find? ›

Irregular verb: To Find.

How do I use find command to list files? ›

Finding files (find command)
  1. To list all files in the file system with the name .profile, type the following: find / -name .profile. ...
  2. To list files having a specific permission code of 0600 in the current directory tree, type the following: find . -

How to find arguments in Linux? ›

-exec command ; Execute command on each matched file; returns true if 0 is returned as the exit status of command. All following arguments to find are taken to be arguments to the command until a semicolon (';') is encountered.

How to search Linux command? ›

There are two Linux commands to do this, which are find and locate.
Using the Find Command in Linux
  1. / (slash) — search the whole system.
  2. . (dot) — search from the folder you're currently working on (current directory).
  3. ~ (tilde) — to search from your home folder.

How to find string in Linux? ›

Use the grep command to search the specified file for the pattern specified by the Pattern parameter and writes each matching line to standard output. This displays all lines in the pgm. s file that begin with a letter.

How do I search all files in Linux? ›

Search All Files in Directory

To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.

How do I find the location of a file? ›

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I retrieve hidden data? ›

Select the Start button, then select Control Panel > Appearance and Personalization. Select Folder Options, then select the View tab. Under Advanced settings, select Show hidden files, folders, and drives, and then select OK.

How to use grep in Linux? ›

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How can I see hidden text in files? ›

Click File > Options > Display, and then under Always show these formatting marks on the screen, select the Hidden text check box. Click File > Options > Advanced, and then under Show document content, select the Show drawings and text boxes on screen check box.

What is * wildcard in Linux with examples? ›

The wildcard '*' means it will match any number of characters or a set of characters. For example, S**n will match anything between S and n.

What does Cp * do in Linux? ›

CP is the command used in Unix and Linux to copy your files or directories. Copies any file with the extension “. txt” to the directory “newdir” if the files do not already exist, or are newer than the files currently in the directory.

What does * do in grep? ›

Searching for Metacharacters
[^...]Any character not in the list or range
*Zero or more occurrences of the preceding character or regular expression
.*Zero or more occurrences of any single character
\The escape of special meaning of next character
4 more rows

How do I find a file name in Unix? ›

Replace "pattern" with a filename or matching expression, such as "*. txt" . (Leave the double quotes in.)
-atime nFile was accessed n days ago
-type cSpecifies file type: f=plain text, d=directory
-fstype typSpecifies file system type: 4.2 or nfs
6 more rows
Jun 18, 2019

How do I manually search in Linux? ›

Search in Current Manual Page for Matches

The three tricks to remember to search once you're within a man page are: / search string – find matches to “search string” in current man page” n – go to next match. shift + n – go to prior match.

How do I find a directory in Unix? ›

You need to use the find command, which is used finding files on Linux or Unix-like system. Another option is the the locate command to search through a prebuilt database of files generated by updatedb. However, the find command will search live file-system for files that match the search criteria.

How to find text in all files Linux? ›

You can search for a text string all files under each directory, recursively with -r option:
  1. $ grep -r "redeem reward" /home/tom/
  2. $ grep -R "redeem reward" /home/tom/ Look for all files containing cacheRoot text on Linux:
  3. $ grep -R cacheRoot /home/vivek/
Sep 19, 2022

What are the five 5 common file formats? ›

5 types of document files
  • Portable document format (PDF) A PDF file is a common file type in many work environments. ...
  • Word document (DOC and DOCX) ...
  • Hypertext markup language (HTML and HTM) ...
  • Microsoft excel spreadsheet file (XLS and XLSX) ...
  • Text file (TXT)
Jun 1, 2021

What are the 6 basic file operations? ›

Six basic file operations. The OS can provide system calls to create, write, read, reposition, delete, and truncate files.

What are 5 different file types? ›

  • JPEG (Joint Photographic Experts Group)
  • PNG (Portable Network Graphics)
  • GIF (Graphics Interchange Format)
  • PDF (Portable Document Format)
  • SVG (Scalable Vector Graphics)
  • MP4 (Moving Picture Experts Group)
Jul 26, 2018

How do I find Top 10 files in Linux? ›

The procedure to find largest files including directories in Linux is as follows:
  1. Open the terminal application.
  2. Login as root user using the sudo -i command.
  3. Type du -a /dir/ | sort -n -r | head -n 20.
  4. du will estimate file space usage.
  5. sort will sort out the output of du command.
Mar 27, 2022

What does Ctrl Z mean in Linux? ›

The ctrl-z sequence suspends the current process. You can bring it back to life with the fg (foreground) command or have the suspended process run in the background by using the bg command.

What is Ctrl Z used for in Linux? ›

Undo, redo, and other shortcut key functions
Command SHORTCUT KEYProcedure
Undo CTRL+ZTo reverse your last action, press CTRL+Z. You can reverse more than one action.
5 more rows

What is $# in shell script? ›

The special character $# stores the total number of arguments. We also have $@ and $* as wildcard characters which are used to denote all the arguments. We use $$ to find the process ID of the current shell script, while $? can be used to print the exit code for our script.

What is awk '- F option? ›

awk -F, ' program ' input-files. sets FS to the ' , ' character. Notice that the option uses an uppercase ' F ' instead of a lowercase ' f '. The latter option ( -f ) specifies a file containing an awk program.

What is Z in bash? ›

The -z flag checks for any NULL or uninitialized variables in BASH. The -z flag returns true if the variable passed is NULL or uninitialized. Hence, we can make use of basic If-else statements to detect the parameters passed.

What is find F in Linux? ›

Generally, the -f command stands for files with arguments. The command specifies the associated input to be taken from a file or output source from a file to execute a program. The f command uses both -f and -F (follow) to monitor files. In a shell script, -f is associated with the specified filename.

How do I use find and grep command in Linux? ›

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do I find a directory in Linux? ›

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

How to use find F in Linux? ›

Find Files by Type

In Linux, everything is a file. To search for files based on their type, use the -type option and one of the following descriptors to specify the file type: f : a regular file. d : directory.

How to find file type in Linux? ›

Displaying the file type (file command)
  1. To display the type of information the file named myfile contains, type the following: file myfile. ...
  2. To display the type of each file named in the filenames.lst file, which contains a list of file names, type the following: file -f filenames.lst.

How do I find all text files in Linux? ›

The most common way to find text in a Linux system is using the command-line utility grep . This utility was originally developed for the Unix operating system in the early 1970s.

How do I find a string in all files in Linux? ›

Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Using the grep command, we can recursively search all files for a string on a Linux.


1. How to use the FIND command in Linux | The FIND command
(Motasem Hamdan)
2. 60 Linux Commands you NEED to know (in 10 minutes)
3. The 50 Most Popular Linux & Terminal Commands - Full Course for Beginners
4. Top 15 Linux Commands
5. Important Linux Commands for Beginners: Linux Tutorial
6. 15+ Terminal Commands Every Developer Must Know
(Web Dev Simplified)
Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated: 04/08/2023

Views: 6393

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.