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  Introducing AWS Amplify DataStore, A Persistent Storage Engine That Synchronizes Data Between Apps And The 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  How To Make Small Steps Go A Long Way

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

How to create an AWS free tier account

  • July 10, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

How to configure multiple AWS CLI authentication credentials

  • July 10, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

What is database as a service (DBaaS)?

  • July 7, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

The cloud’s role in PQC migration

  • July 7, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Hybrid cloud has hit the mainstream – but firms are still confused about costs

  • July 7, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Turns out OpenAI is the customer behind Oracle’s mysterious $30 billion cloud deal

  • July 3, 2025
aster-cloud-sms-pexels-tim-samuel-6697306
View Post
  • Programming
  • Software

Send SMS texts with Amazon’s SNS simple notification service

  • July 1, 2025
aster-cloud-website-pexels-goumbik-574069
View Post
  • Programming
  • Software

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

  • June 27, 2025

Stay Connected!
LATEST
  • How to create an AWS free tier account
    • July 10, 2025
  • How to configure multiple AWS CLI authentication credentials
    • July 10, 2025
  • 3
    Formula E accelerates its work with Google Cloud Storage and Google Workspace
    • July 9, 2025
  • What is database as a service (DBaaS)?
    • July 7, 2025
  • The cloud’s role in PQC migration
    • July 7, 2025
  • 6
    Hybrid cloud has hit the mainstream – but firms are still confused about costs
    • July 7, 2025
  • 7
    Building secure, scalable AI in the cloud with Microsoft Azure
    • July 5, 2025
  • 8
    Turns out OpenAI is the customer behind Oracle’s mysterious $30 billion cloud deal
    • July 3, 2025
  • aster-cloud-erp-bill_of_materials_2 9
    What is an SBOM (software bill of materials)?
    • July 2, 2025
  • aster-cloud-sms-pexels-tim-samuel-6697306 10
    Send SMS texts with Amazon’s SNS simple notification service
    • July 1, 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
    A looming hyperscaler exodus? UK IT leaders are thinking of ditching US cloud providers – here’s why
    • June 26, 2025
  • Genome 2
    AlphaGenome: AI for better understanding the genome
    • June 25, 2025
  • aster-cloud-website-pexels-goumbik-574069 3
    Host a static website on AWS with Amazon S3 and Route 53
    • June 27, 2025
  • Camping 4
    The Summer Adventures : Camping Essentials
    • June 27, 2025
  • 6 edge monitoring best practices in the cloud
    • June 25, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.