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  Save Big By Temporarily Suspending Unneeded Compute Engine VMs—Now GA

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  What Scrum Masters Can Learn From Dancing

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

Global cloud spending might be booming, but AWS is trailing Microsoft and Google

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

Google Cloud, Cloudflare struck by widespread outages

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

What is PC as a service (PCaaS)?

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

By the numbers: Use AI to fill the IT skills gap

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

Crayon targets mid-market gains with expanded Google Cloud partnership

  • June 10, 2025
Robot giving light bulb to businessman. Man sitting with laptop on money coins flat vector illustration. Finance, help of artificial intelligence concept for banner, website design or landing web page
View Post
  • Computing
  • Multi-Cloud
  • Technology

FinOps X 2025: IT cost management evolves for AI, cloud

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

AI security and compliance concerns are driving a private cloud boom

  • June 9, 2025
cookies-food-photographer-jennifer-pallian-OfdDiqx8Cz8-unsplash
View Post
  • Computing
  • Multi-Cloud
  • Technology

What is a cookie?

  • June 6, 2025

Stay Connected!
LATEST
  • 1
    Advanced audio dialog and generation with Gemini 2.5
    • June 15, 2025
  • 2
    A Father’s Day Gift for Every Pop and Papa
    • June 13, 2025
  • 3
    Global cloud spending might be booming, but AWS is trailing Microsoft and Google
    • June 13, 2025
  • Google Cloud, Cloudflare struck by widespread outages
    • June 12, 2025
  • What is PC as a service (PCaaS)?
    • June 12, 2025
  • 6
    Apple services deliver powerful features and intelligent updates to users this autumn
    • June 11, 2025
  • By the numbers: Use AI to fill the IT skills gap
    • June 11, 2025
  • 8
    Crayon targets mid-market gains with expanded Google Cloud partnership
    • June 10, 2025
  • Apple-WWDC25-Apple-Intelligence-hero-250609 9
    Apple Intelligence gets even more powerful with new capabilities across Apple devices
    • June 9, 2025
  • Apple-WWDC25-Liquid-Glass-hero-250609_big.jpg.large_2x 10
    Apple introduces a delightful and elegant new software design
    • June 9, 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
  • 1
    Apple supercharges its tools and technologies for developers to foster creativity, innovation, and design
    • June 9, 2025
  • Robot giving light bulb to businessman. Man sitting with laptop on money coins flat vector illustration. Finance, help of artificial intelligence concept for banner, website design or landing web page 2
    FinOps X 2025: IT cost management evolves for AI, cloud
    • June 9, 2025
  • 3
    AI security and compliance concerns are driving a private cloud boom
    • June 9, 2025
  • 4
    It’s time to stop debating whether AI is genuinely intelligent and focus on making it work for society
    • June 8, 2025
  • person-working-html-computer 5
    8 benefits of AI as a service
    • June 6, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.