How to Search Files Effectively in the Linux Terminal (2023)

Have you ever felt frustrated searching for files manually on your computer? If you’re a developer or DevOps engineer working on GUI-less Linux servers, it'll be hard navigating back and forth to find files.

Many people are unaware of the power of Linux terminals. Linux has an incredibly powerful command line that allows you to search files and directories in a fraction of a second.

Whether you’re a beginner or an expert, and if you're looking to take your file management skills to the next level, you've arrived at the right spot. This article will help you understand the basics of the most commonly used find command in Linux.

What is the find Command in Linux?

The find command allows you to search for files and directories on your computer. It adds the flexibility to search for files in a specific directory or recursively through all sub-directories.

Let’s explore the power of the find command

How to Search a File by Name

Let’s say you saved a file called hello_world.html somewhere and you don’t even remember the directory name. But your boss is asking you to send them the file immediately.

Usually, if you forgot where you stored a file, you'd begin by going through folder after folder and checking if the file exists.

This is when the find command does a great job. Instead of searching the file manually on your computer, you can use the find command to automate the process.

By passing the name of the file using the -name flag, the find command searches and returns the location of the file.

find -name <file_name>
How to Search Files Effectively in the Linux Terminal (1)

But remember the -name flag performs a case-sensitive search. If you are looking to do a case-insensitive search, you can use the -iname flag instead.

find -iname <file_name>
How to Search Files Effectively in the Linux Terminal (2)

You can also use the find command as an alternative to the ls command in some places. Let's assume you need to find all the files ending with the .txt extension. You can do so with the find command using the regex pattern (*.txt).

(Video) 3 Best Ways To Find Files And Folders With The Linux Terminal

find /path/to/search -name "*.txt"
How to Search Files Effectively in the Linux Terminal (3)

This command will list all the .txt files in your current directory and its sub-directories.

To find .txt files in a particular directory and sub-directory, you can replace the /path/to/search with the path of your directory.

How to Find a Directory in Linux

Searching for a directory is possible by passing the d to the -type parameter in the find command.

find /path/to/search -type d
How to Search Files Effectively in the Linux Terminal (4)

In the above screenshot, we're finding a directory named zip from our current directory.

Similarly the -type option accepts other parameter options to simplify our finding process.

  • f finds the list of regular files
  • b finds the list of block devices
  • c finds the list of character devices
  • l finds the list of symbolic links
  • s finds the list of sockets
  • p finds the named pipes

How to Search a File by Size in Linux

Adding the -size option along with the find command helps you find files based on size. Prepend a + or - to the size to represent greater than and less than, respectively.

find /path/to/search -size <size_of_the_file>
How to Search Files Effectively in the Linux Terminal (5)

In the above screenshot, we're finding all the files that have a size greater than 1 GB.

You can also search for all files that fall within a specific size range.

For example, if you want to find all the files that are above 50 MB and below 100 MB, you can run the following command:

find /path/to/search -size +50M -size -100M
How to Search Files Effectively in the Linux Terminal (6)

You can specify the size in your preferred notation. A few of the available notations are:

(Video) How to Find Files in Linux | Learning Terminal

  1. K represents KB
  2. M represents MB
  3. G represents GB
  4. b represents bytes
  5. c represents blocks

How to Search a File Based on Time Modified

Every file has a created and last updated time associated with it. Let's assume you have thousands of files in your directory. You edited a file in the past couple of days and forgot its name. You're sure that you have edited only a couple of files after that.

In such cases, you can find all the files that were modified within the past 7 days. This limits your search from 1000+ files to a more manageable amount. You'll be able to find the file you edited in seconds after running the command.

This is possible by passing -mtime parameter with the find command.

find /path/to/search -mtime <-number_of_days_ago>
How to Search Files Effectively in the Linux Terminal (7)

Let's assume another scenario, where today's date is February 10, 2023. You modified a file before Feb 3, 2023. After Feb 3, 2023, you modified a lot of files. You have to find the file which you modified before Feb 3, 2023. So, basically, you need the files that were modified before Feb 3, 2023.

Strange scenario, right?

But, you can also run this query using the find command. You can achieve this by exchanging the negative sign (-) with the positive sign (+).

Here's the modified command for you:

find /path/to/search -mtime +7

How to Execute a Command on Files Filtered from the find Command

This question may confuse you. Before revealing the answer, let's understand the question clearly with a real scenario.

Let's assume you have 1000 files in a directory, and running the find command returns 20 matching files. You want to move these 20 files into a different directory. How can you achieve that?

Simply put, we have to run a command over each of the filtered files.

(Video) How to use grep command to find a file in Linux | Linux in a Minute

You can do this by passing the -exec option with the find command.

The -exec option executes a command on each file that is found in the search. The -exec option is followed by a command and its arguments, with the {} symbols representing the path of the file being processed.

To represent the end of the -exec command, we have to add \; (a backward slash and a semi-colon).

Here's the syntax:

find /path/to/search -name -exec {} \;

Let’s try to move the filtered files from the 5minslearn directory to the zip directory.

Here’s the command:

find ./5minslearn -name "*.zip" -exec mv {} ./5minslearn/zip \;
How to Search Files Effectively in the Linux Terminal (8)

This command searches for all files ending with a .zip in the ./5minslearn directory and then moves each file to the ./5minslearn/zip directory.

The -exec option allows you to perform a wide range of operations on the files that are found. You can replace the move command from the above example by copying, deleting, or even changing the file permission command.

How to Execute a Command on Files Filtered with a Confirmation

Most people will prefer to use this if they're not sure about whether to apply the operation on each file.

The -ok option is similar to the -exec option except that it will ask for confirmation before executing the operation on each file. This command is super helpful to review files that will be affected before executing the specific operation. You also have the option to decline if you're not sure or don't wish to apply the command.

(Video) Linux/Mac Terminal Tutorial: The Grep Command - Search Files and Directories for Patterns of Text

For example, this time let's try to move the .txt files to the other directory.

find /path/to/search -name "*.txt" -ok mv {} /path/to/destination \;
How to Search Files Effectively in the Linux Terminal (9)

The above command searches for all files with a .txt extension in the ./5minslearn directory and then prompts the user to confirm before moving each file to the ./5minslearn/text_files directory.

To approve the operation, enter yes and no to decline the operation and skip to the next file.

The -ok option is useful when you want to be cautious about the files you are modifying, as it allows you to inspect each file and its location before executing the specified command.

How to Find a File with Detailed Information

The -ls option in the find command is used to display information about the files found in the search, in the format of the ls command. This option provides detailed information about the files, such as their permissions, owner, size, and last modified time.

find /path/to/search -name "*.<file-extension>" -ls
How to Search Files Effectively in the Linux Terminal (10)

How to Find and Remove Files

Have you ever needed to find files and remove them from your computer? The -delete option in the find command does this for you. It allows you to delete files that match the specified criteria.

find . -name "*.<extension>" -delete
How to Search Files Effectively in the Linux Terminal (11)

In the above example, you can see that the find command deleted the files with the .html extension

Note: This operation is irreversible. Be 100% sure when you run the delete operation.

I would advise running the find command without the -delete flag at first and ensure that only the files that need to be deleted are shown. Once you're sure, you can execute the same command appending -delete flag.

Conclusion

In this article, you learned how to search files effectively using your Linux terminal.

(Video) Find and sort files by extension in the Linux command line

These are very basic options in the find command that I think every developer should know. I believe mastering the fundamentals is the first step to becoming more advanced with Linux. I've been covering basics in all my blogs to help you create a solid foundation.

To learn more about Linux, subscribe to my email newsletter on my site and follow me on social media.

FAQs

How to Search Files Effectively in the Linux Terminal? ›

By passing the name of the file using the -name flag, the find command searches and returns the location of the file. But remember the -name flag performs a case-sensitive search. If you are looking to do a case-insensitive search, you can use the -iname flag instead.

What is the fastest way to search files in Linux? ›

Use fd to Find Files on Linux

It returns the results almost instantly. fd accepts arguments after the pattern. Some useful arguments are: -H: Search hidden files and directories.

How do you search through files 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.

How to search a file in Linux? ›

You'll typically use the find command with the syntax find /path -type f -iname filename . You can use a variation of this command to find any file or directory on your Linux machine. We'll break down the command into simple parts. /path/to/file is the directory in which you want to search for the file.

How do I search files faster? ›

You can take steps to speed up Windows search, such as restarting the Windows Search Service, restarting Windows Explorer, or restarting your entire computer, but the fundamental cause of slow search times is simply that indexing files take time. The more files you have, the longer it takes to index them.

How do I search files quickly? ›

Search File Explorer: Open File Explorer from the taskbar or select and hold the Start menu (or right-click), select File Explorer , then select a search location: To quickly find relevant files from your PC and the cloud, search from Home. To find files stored inside a folder, search from a folder like Downloads.

How to search files in Linux using grep? ›

grep command in Unix/Linux
  1. grep [options] pattern [files]
  2. Options Description -c : This prints only a count of the lines that match a pattern -h : Display the matched lines, but do not display the filenames. ...
  3. $cat > geekfile.txt.
  4. $grep -i "UNix" geekfile.txt.
  5. $grep -c "unix" geekfile.txt.
Nov 15, 2022

How do I search for text in all files in Linux? ›

Method 1: grep command. grep command in Linux that is used to search for files containing a specific text or string. By default, it shows us the lines in the files that contain the particular text. If we append the -l option to it, the command will show us all the files that contain the particular text.

How do you search for a text in all files in Linux? ›

You can search for a text string all files under each directory, recursively with -r option:
  1. $ grep -r "redeem reward" /home/tom/ OR.
  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 is the best Linux command to find a file? ›

1. Find Command. find command is a powerful, widely used CLI tool for searching and locating files whose names match simple patterns, in a directory hierarchy. Using find is simple, all you need to do is provide a starting point (top of the directory heirarchy) where the search beings.

How do I locate a file? ›

Find & open files
  1. Open your device's Files app . Learn where to find your apps.
  2. Your file categoriess will show.
  3. To sort by name, date, or size, tap a file category More. Sort by.
  4. To open a file, tap it.

How do I search for a file in Unix? ›

Searches a given path for a file or folder. The syntax is: find [path...] [expression...] Examples: On some of the latest Unix-like OS's, the -print option is a default and can be omitted.

What is the best command to find a file in Linux? ›

Use find to search for files based on name, type, time, size, ownership and permissions, in addition to some other useful options. Install and use Linux locate command to perform faster system-wide searches for files. It also allows you to filter out by name, case-sensitive, folder, and so on.

How do I search all files in a directory in Linux? ›

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How do I find more than 100 MB files in Linux? ›

How To Find Large Files on Linux
  1. To list the directory contents in descending file size order, use the ls command along with the -IS argument. ...
  2. Use find to search for any file larger than 100MB in the current directory. ...
  3. Use the same command, but this time specify a path to search. ...
  4. Open a terminal.
Jun 13, 2022

Videos

1. 60 Linux Commands you NEED to know (in 10 minutes)
(NetworkChuck)
2. How to find a file in Linux - Ubuntu | Linux in a Minute
(ITProTV)
3. Using Linux Terminal Tools for Finding Files
(cm tech)
4. Filtering .log files, with cat, grep, cut, sort, and uniq
(Hackpens)
5. Linux/Mac Terminal Tutorial: How To Use The find Command
(Corey Schafer)
6. Linux Crash Course - The find command
(Learn Linux TV)
Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated: 07/08/2023

Views: 6445

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.