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

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)Terminal command to search a file by name

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)Terminal command to do case-sensitive search

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

find /path/to/search -name "*.txt"

How to Search Files Effectively in the Linux Terminal (3)Terminal command displaying file search by matching a pattern

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)Terminal command to search a directory using find command

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)Terminal command to search files by size

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)Terminal command to search files within a range

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

  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)Terminal command to search file based on modified time

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.

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.

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)Terminal command to move the filtered files with a confirmation

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)Terminal command to list files in ls command format

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.

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.

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

FAQs

How to search for files in Linux terminal? ›

Let me show you how easy it is to find a file in Linux.
  1. The basic use of the find command. Open a terminal window from your desktop menu. With the terminal app open, type the syntax for the basic find command: find -name FILE. ...
  2. A trick and a trap. Let's say you're looking for MyFile.txt. You could simply run the command:
Jan 23, 2024

What is the fastest way to find a file in Linux? ›

The locate command in Linux is a utility for swiftly finding files on the file system. It does this by creating an index of the file system and keeping a record of the file locations. This makes it faster for the locate command to search for files, as compared to find, which scans the file system in real time.

How do you search content in a file 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 to search words in Linux terminal? ›

The grep command is commonly used to search lines containing a specific keyword in a file. It is also useful for filtering another utility's output, printing only ones with a particular pattern.

How to search in terminal? ›

By default, this command is set to Ctrl+Shift+F . // Press ctrl+shift+f to open the search box { "command": "find", "keys": "ctrl+shift+f" }, For example, you can change "ctrl+shift+f" to "ctrl+f" , so when typing Ctrl+F .

Which command can be used to search for files on a Linux system? ›

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.

How do I quick search in Linux? ›

Command Execution:
  1. The find command starts the search from the current directory, including all its subdirectories.
  2. For each file ( -type f ) found in the search, the -exec option executes the grep command.
  3. The grep command searches for the specified content (“pattern”) in each file.
Jul 5, 2024

How do I find files quickly? ›

The basics of finding files on Windows

You're able to search right from the taskbar. There's a search box or icon right there on the taskbar, just to the right of the Start menu button, which you can use to start your file search. Just type as much of the file name as you remember, and results appear as you type.

How do I search an entire file system in Linux? ›

Find Command Syntax
  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 search file contents from command line? ›

What is the command to search file content in CMD? You can use the findstr command to search file content using CMD. For example, findstr “text” filename. txt will search for the specified text within the file.

How do you search for files of a certain type in Linux? ›

Find command to search a file in Linux:
  1. find . - name ips.txt. ...
  2. find . - name "myFile*" ...
  3. find . - type d. ...
  4. find . - mtime -2. ...
  5. grep "database" configuration.php. ...
  6. grep -r -H "database" * ...
  7. grep -l "database" * ...
  8. ls -la | grep configuration.php.
Oct 27, 2020

How do I search files for content? ›

Search file contents

To search the text of the documents: In Windows 10, click on "Advanced options" -> "File Contents." In Windows 11, type the search words and click on the "Search options" -> "File Contents."

How to do a file search in 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.

How do I search for a file with certain words in Linux? ›

How to find files which contains defined text in Linux and...
  1. apt-get update && apt install grep. Command to install in RedHat-based OS (like Centos) is:
  2. yum install grep. To find all files with required string just run:
  3. grep -r <looked_for_text> <dir>
Feb 2, 2023

How to search a file in Unix? ›

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 you locate in files in Linux? ›

The 'locate' command in Linux is a powerful tool used to find files by their name. You can use it like this: locate [options] filename. txt . In this example, we use the 'locate' command to search for 'example.

How to find regular files in Linux? ›

Regular files in Linux are the most common type of files, containing data that can be read and modified. They can be identified using the file command, which displays the file type, or by using the ls command with the -l option, where regular files are represented by the "-" character at the beginning of the line.

What is the LS command in Linux? ›

The ls command is one of the more basic commands in Linux. It is designed to list the names and features of files and directories. It can be used for a single file or as many as all files and folders in a selected set of directories.

How do you search for a specific file type in Linux? ›

Find command to search a file in Linux:
  1. find . - name ips.txt. ...
  2. find . - name "myFile*" ...
  3. find . - type d. ...
  4. find . - mtime -2. ...
  5. grep "database" configuration.php. ...
  6. grep -r -H "database" * ...
  7. grep -l "database" * ...
  8. ls -la | grep configuration.php.
Oct 27, 2020

Top Articles
Our Story
Frequently asked questions
Chris Provost Daughter Addie
13 Easy Ways to Get Level 99 in Every Skill on RuneScape (F2P)
Seething Storm 5E
Craigslist Nj North Cars By Owner
Mr Tire Rockland Maine
7543460065
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
Student Rating Of Teaching Umn
Bme Flowchart Psu
Mens Standard 7 Inch Printed Chappy Swim Trunks, Sardines Peachy
Rhinotimes
Craigslist Farm And Garden Tallahassee Florida
Available Training - Acadis® Portal
iLuv Aud Click: Tragbarer Wi-Fi-Lautsprecher für Amazons Alexa - Portable Echo Alternative
Mail.zsthost Change Password
Log in or sign up to view
How Much Is Tay Ks Bail
Full Standard Operating Guideline Manual | Springfield, MO
zom 100 mangadex - WebNovel
Boscov's Bus Trips
Myhr North Memorial
Stoney's Pizza & Gaming Parlor Danville Menu
Pearson Correlation Coefficient
Like Some Annoyed Drivers Wsj Crossword
Glover Park Community Garden
Sunset Time November 5 2022
Dark Entreaty Ffxiv
Bay Area Craigslist Cars For Sale By Owner
Dei Ebill
The Boogeyman (Film, 2023) - MovieMeter.nl
Stubhub Elton John Dodger Stadium
Sam's Club Near Wisconsin Dells
Blush Bootcamp Olathe
How To Make Infinity On Calculator
Kltv Com Big Red Box
Sun Haven Pufferfish
Tributes flow for Soundgarden singer Chris Cornell as cause of death revealed
What Time Does Walmart Auto Center Open
Kstate Qualtrics
Spinning Gold Showtimes Near Emagine Birch Run
Oreillys Federal And Evans
Pillowtalk Podcast Interview Turns Into 3Some
Philadelphia Inquirer Obituaries This Week
Pepsi Collaboration
Jason Brewer Leaving Fox 25
Suffix With Pent Crossword Clue
Tacos Diego Hugoton Ks
Bf273-11K-Cl
Ret Paladin Phase 2 Bis Wotlk
Zalog Forum
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6694

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.