aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Computing
  • Data
  • Programming

Find Files And Directories On Linux With The Find Command

  • Ackley Wyndam
  • September 25, 2021
  • 4 minute read

There are many reasons to learn find, so download our free find cheat sheet to help you learn more about the command.

Regardless of how organized I resolve to be, it seems there are always times when I just can’t locate a file. Sometimes it’s because I can’t remember the name of the file in the first place. Other times, I know the name, but I can’t recall where I decided to save it. There are even times when I need a file that I didn’t create in the first place. No matter what the quandary, though, I know that on a POSIX system, I always have the find command.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

 

Installing find

The find command is defined by the POSIX specification, which creates the open standard by which POSIX systems (including Linux, BSD, and macOS) are measured. Simply put, you already have find installed as long as you’re running Linux, BSD, or macOS.

However, not all find commands are exactly alike. The GNU find command, for instance, has features that the BSD or Busybox or Solaris find command might not have or does have but implements differently. This article uses GNU find from the findutils package because it’s readily available and pretty popular. Most commands demonstrated in this article work with other implementations of find, but should you try a command on a platform other than Linux and get unexpected results, try downloading and installing the GNU version.

 

Find a file by name

You can locate a file by its filename by providing the full file name or parts of the file name using regular expressions. The find command requires the path to the directory you want to search in, options to specify what attribute you’re searching (for instance, –name for a case-sensitive file name), and then the search string. By default, your search string is treated literally: The find command searches for a filename that is exactly the string you enter between quotes unless you use regular expression syntax.

Read More  Red Hat Enterprise Linux Arrives In Oracle’s Cloud

Assume your Documents directory contains four files: Foo, foo, foobar.txt, and foo.xml. Here’s a literal search for a file with the name “foo”:

$ find ~ -name “foo”
/home/tux/Documents/examples/foo

You can broaden your search by making it case-insensitive with the -iname option:

$ find ~ -iname “foo”
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo

Wildcards

You can use basic shell wildcard characters to broaden your search. For instance, the asterisk (*) represents any number of characters:

$ find ~ -iname “foo*”
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

A question mark (?) represents a single character:

$ find ~ -iname “foo*.???”
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

This isn’t regular expression syntax, so the dot (.) represents a literal dot in this example.

 

Regular expressions

You can also use regular expressions. As with -iname and -name, there’s both a case-sensitive and a case-insensitive option. Unlike the -name and -iname options, though, a -regex and -iregex search is applied to the whole path, not just the file name. That means if you search for foo, you get no results because foo doesn’t match /home/tux/Documents/foo. Instead, you must either search for the entire path, or else use a wildcard sequence at the beginning of your string:

$ find ~ -iregex “.*foo”
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo

Find a file modified within the last week

To find a file you last modified last week, use the -mtime option along with a (negative) number of days in the past:

$ find ~ -mtime -7
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

Find a file modified within a range of days

You can combine -mtime options to locate a file within a range of days. For the first -mtime argument, provide the most recent number of days you could have modified the file, and for the second, give the greatest number of days. For instance, this search looks for files with modification times more than one day in the past, but no more than seven:

<span class="co4">$ </span><span class="kw2">find</span> ~ <span class="re5">-mtime</span> +<span class="nu0">1</span> <span class="re5">-mtime</span> <span class="re5">-7</span> 

Limit a search by file type

It’s common to optimize the results of find by specifying the file type you’re looking for. You shouldn’t use this option if you’re not sure what you’re looking for, but if you know you’re looking for a file and not a directory, or a directory but not a file, then this can be a great filter to use. The option is -type, and its arguments are a letter code representing a few different kinds of data. The most common are:

  • d – directory
  • f – file
  • l – symbolic link
  • s – socket
  • p – named pipe (used for FIFO)
  • b – block special (usually a hard drive designation)
Read More  Convert Your Windows Install Into A VM On Linux

Here are some examples:

$ find ~ -type d -name “Doc*”
/home/tux/Documents
$ find ~ -type f -name “Doc*”
/home/tux/Downloads/10th-Doctor.gif
$ find /dev -type b -name “sda*”
/dev/sda
/dev/sda1

Adjust scope

The find command is recursive by default, meaning that it searches for results in the directories of directories contained in directories (and so on). This can get overwhelming in a large filesystem, but you can use the -maxdepth option to control how deep into your folder structure you want find to descend:

$ find /usr -iname “*xml” | wc -l
15588
$ find /usr -maxdepth 2 -iname “*xml” | wc -l
15

You can alternately set the minimum depth of recursion with -mindepth:

$ find /usr -mindepth 8 -iname “*xml” | wc -l
9255

Download the cheat sheet

This article only covers the basic functions of find. It’s a great tool for searching through your system, but it’s also a really useful front-end for the powerful Parallel command. There are many reasons to learn find, so download our free find cheat sheet to help you learn more about the command.

 

This article is republished from opensource.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

Ackley Wyndam

Related Topics
  • Computing
  • Find Command
  • Linux
  • Programming
  • Wildcards
You May Also Like
View Post
  • Computing
  • Multi-Cloud
  • Technology

Host a static website on AWS with Amazon S3 and Route 53

  • June 27, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Prioritize security from the edge to the cloud

  • June 25, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

6 edge monitoring best practices in the cloud

  • June 25, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Pure Accelerate 2025: All the news and updates live from Las Vegas

  • June 18, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management

  • June 18, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

What is cloud bursting?

  • June 18, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it

  • June 17, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

What is confidential computing?

  • June 17, 2025

Stay Connected!
LATEST
  • Camping 1
    The Summer Adventures : Camping Essentials
    • June 27, 2025
  • Host a static website on AWS with Amazon S3 and Route 53
    • June 27, 2025
  • Prioritize security from the edge to the cloud
    • June 25, 2025
  • 6 edge monitoring best practices in the cloud
    • June 25, 2025
  • Genome 5
    AlphaGenome: AI for better understanding the genome
    • June 25, 2025
  • 6
    Pure Accelerate 2025: All the news and updates live from Las Vegas
    • June 18, 2025
  • 7
    ‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management
    • June 18, 2025
  • What is cloud bursting?
    • June 18, 2025
  • 9
    There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it
    • June 17, 2025
  • What is confidential computing?
    • June 17, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Oracle adds xAI Grok models to OCI
    • June 17, 2025
  • Fine-tune your storage-as-a-service approach
    • June 16, 2025
  • 3
    Advanced audio dialog and generation with Gemini 2.5
    • June 15, 2025
  • Google Cloud, Cloudflare struck by widespread outages
    • June 12, 2025
  • 5
    Global cloud spending might be booming, but AWS is trailing Microsoft and Google
    • June 13, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.