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) 60 useful Linux commands in 15 minutes

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) You Must Know 20 Important Linux Commands

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 are the use cases of 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? ›

1. Find describes locating a specified text, file, document, or other objects on a computer, in a file, or on the Internet. For example, you can press the keyboard shortcut Ctrl + F now to open a find window and search for any text on this page.

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 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

How do I find a file in Linux using find command? ›

Basic Examples
  1. find . - name thisfile.txt. If you need to know how to find a file in Linux called thisfile. ...
  2. find /home -name *.jpg. Look for all . jpg files in the /home and directories below it.
  3. find . - type f -empty. Look for an empty file inside the current directory.
  4. find /home -user randomperson-mtime 6 -iname ".db"
Apr 5, 2021

Can we use * In find command in Linux? ›

Find text within multiple files

Consider the below command: find ./Newdirectory -type f -name "*. txt" -exec grep 'demo' {} \;

How do I find a file using find command? ›

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

What is find and locate in Linux? ›

The find command will search for the specified files in all of your computer's directories. Meanwhile, the locate command will look for files only on your Linux database.

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 is F in 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.

How do you use find in a sentence? ›

[M] [T] You'll find our house at the end of the next street. [M] [T] As soon as I find it, I'll bring it over to your place. [M] [T] He woke up to find himself lying on a bench in the park. [M] [T] We'll have to camp out if we can't find a place to stay.

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.

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.

How do I find empty files in Linux? ›

Understanding find command options

-type f : Search and list files only. -type d : Find and list empty directories only. -empty : Only list empty files or folders on Linux or Unix. -ls : Show current file in ls -dils format on your screen.

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 do you use find and grep? ›

Use grep to select lines from text files that match simple patterns. Use find to find files and directories whose names match simple patterns. Use the output of one command as the command-line argument(s) to another command.

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

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 with 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 folder 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.

What is the alternative for find in Linux? ›

The fd command offers a simple, intuitive way to search your Linux filesystem. fd is a super fast, Rust-based alternative to the Unix/Linux find command.

Which is faster locate or find? ›

The reason locate is faster than find is because it relies on a database that lists all the files on the filesystem. This database is usually updated once a day with a cron script, but you can update it manually with the updatedb command.

What does find do in bash? ›

The Bash find Command 101

The find command allows you to define those criteria to narrow down the exact file(s) you'd like to find. The find command finds or searches also for symbolic links (symlink). A symbolic link is a Linux shortcut file that points to another file or a folder on your computer.

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 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 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? ›

Find the Largest Top 10 Files and Directories On a Linux
  1. du command : It estimates file space usage.
  2. sort command : Sort lines of text files or given input data.
  3. head command : Output the first part of files i.e. to display first 10 largest file.
  4. find command : It Searches file on Linux machine.
Jan 20, 2020

What does ctrl Z mean in Linux? ›

ctrl+z stops the process and returns you to the current shell. You can now type fg to continue process, or type bg to continue the process in the background.

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 does R means in Linux? ›

r allows users to list files in the directory; w means that users may delete files from the directory or move files into it; x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files.

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 the difference between find and grep command? ›

While grep finds lines in files, the find command finds files themselves. Again, it has a lot of options; to show how the simplest ones work, we'll use the shell-lesson-data/exercise-data directory tree shown below. The exercise-data directory contains one file, numbers.

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.

Can you use regex in find? ›

Regular expressions (shortened as regex) are powerful tools described by character sequences that specify a search pattern. This is why their use together with find yields a more refined search with a more reduced command.

When to use find and locate? ›

Although both commands have the same function, they work differently. The find command will search for the specified files in all of your computer's directories. Meanwhile, the locate command will look for files only on your Linux database.

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 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 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 a file in Linux terminal? ›

You can use the find command with -name option followed by the file name that you want to search. You can use the following option if you want to search for a specific file type: f – regular file. d – directory.


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

Author: Saturnina Altenwerth DVM

Last Updated: 01/23/2023

Views: 6310

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.