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  Microsoft Build 2019 | Intro to Blockchain

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  Active Directory Diagnosis Tool For Cloud SQL

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
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • Computing
  • Public Cloud
  • Technology

United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services

  • April 15, 2025
Microsoft’s Majorana 1 chip carves new path for quantum computing
View Post
  • Computing
  • Technology

Microsoft’s Majorana 1 chip carves new path for quantum computing

  • February 19, 2025
View Post
  • Computing
  • Engineering

Why a decades old architecture decision is impeding the power of AI computing

  • February 19, 2025
CES 2025: Intel Shows Off Its AI Tech
View Post
  • Computing
  • Technology

CES 2025: Intel Shows Off Its AI Tech

  • January 23, 2025
View Post
  • Data
  • Engineering

Hiding in Plain Site: Attackers Sneaking Malware into Images on Websites

  • January 16, 2025
View Post
  • Computing
  • Design
  • Engineering
  • Technology

Here’s why it’s important to build long-term cryptographic resilience

  • December 24, 2024
Cloud platforms among the clouds
View Post
  • Computing
  • Learning
  • Public Cloud

Best Cloud Platforms Offering Free Trials for Cloud Mastery

  • December 23, 2024

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • 3
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 4
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 5
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 6
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 7
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 8
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 9
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 10
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 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
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 2
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 3
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 4
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • Nintendo Switch Deals on Amazon 5
    10 Physical Nintendo Switch Game Deals on MAR10 Day!
    • March 9, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.