Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Mastering `find`: Search by Name, Type, and Time

Recommended resources

Introduction

Welcome back! In the last lesson, you mastered the art of shell efficiency by using tab completion and command history. These skills are vital for working quickly on the command line. Now that you can recall and edit commands with ease, let's add another powerful tool to your arsenal: finding what you need.

As a developer and future system administrator, you'll constantly need to locate specific files—configuration files, log files, source code, or assets. Navigating manually with cd and ls is often not enough. This is where the find command comes in.

In this lesson, you will learn how to use the versatile find command to search for files and directories across your entire system based on specific criteria.

Your goal for this lesson is to learn how to:

  • Search for files by name, type, and modification time using find.

This is a fundamental skill that you will use daily, whether you're looking for a specific Django template file or troubleshooting a server issue by locating recent log entries.


The Power of find

The find command is a standard utility on all Linux systems that recursively searches a directory hierarchy for files and directories that match a given expression. Its basic syntax is:

find [starting-path] [expression]

  • [starting-path]: Where the search should begin (e.g., . for the current directory, / for the entire system, ~ for your home directory).
  • [expression]: The set of rules describing what you're looking for. This includes criteria like name, type, time, size, and permissions.

To get a comprehensive overview, let's start with a great video tutorial. We'll watch it in segments as we explore different features.

Linux/Mac Terminal Tutorial: How To Use The find Command

This video from Corey Schafer is an excellent, practical walkthrough of the find command. We'll begin with the basics to understand its core functionality.

Please watch from the beginning until 01:41. This will introduce the command and show you how to perform a basic recursive search from the current directory or a specified path.


Searching by Name

The most common use of find is to search for a file or directory by its name. This is done with the -name test.

  • Exact Match: find . -name "settings.py" will find files named exactly settings.py.
  • Wildcard Match: You can use wildcards (like *) to match patterns. For example, find . -name "*.py" will find all files ending with .py. It's good practice to wrap the pattern in quotes to prevent the shell from trying to interpret the * itself.
  • Case-Insensitive Match: If you're unsure about the capitalization, use -iname (insensitive name). find . -iname "readme.md" will match readme.md, README.md, Readme.Md, etc.

Now, let's see this in action.

Linux/Mac Terminal Tutorial: How To Use The find Command

Let's continue with Corey Schafer's video to see how to search by name and use wildcards.

Watch from 02:17 to 04:29. This section covers -name, using the wildcard * to find files with certain extensions, and using -iname for case-insensitive searches.

Do It Yourself: Find by Name

Time to practice. Open your terminal and try these commands.

  1. Find a system configuration file: Let's find the DNF configuration file we looked at in a previous lesson.

    find /etc -name "dnf.conf"
    

    This command searches the entire /etc directory tree for a file named exactly dnf.conf.

  2. Find all .conf files: How many configuration files are in /etc? Let's use a wildcard.

    find /etc -name "*.conf"
    

    You will likely see many "Permission denied" errors for directories your user cannot read. This is normal. To hide them and see only the results, you can redirect the standard error stream to /dev/null:

    find /etc -name "*.conf" 2>/dev/null
    
  3. Find your Python files: Navigate to your Django project directory (or any directory where you have code).

    
    
    
    # Make sure you are in your project directory
    cd path/to/your/project
    
    
    
    
    # Find all Python files in the current directory and subdirectories
    find . -name "*.py"
    

Searching by Type

Sometimes you want to find only directories, or only regular files, and exclude everything else. The -type test allows you to do this. The two most common types you'll use are:

  • f: a regular file
  • d: a directory
Using 'find' command to search by file type
This image clearly shows the difference between using `find -type f` to list only files and `find -type d` to list only directories.

Let's see a video demonstration.

Linux/Mac Terminal Tutorial: How To Use The find Command

The video segment covering -type is very short and clear.

Watch from 01:41 to 02:17. This part demonstrates how to use -type d for directories and -type f for files.

Do It Yourself: Find by Type

  1. Find all directories in /var/log:
    find /var/log -type d 2>/dev/null
    
  2. Combine with -name: Now, find all directories whose names start with aud.
    find /var/log -type d -name "aud*" 2>/dev/null
    
    Notice how we simply add another expression. By default, find treats multiple expressions as being joined by "AND". This command finds items that are both a directory and have a name starting with aud.

Searching by Modification Time

This is an incredibly useful feature for system administration and development. You can find files that were changed recently (or not so recently).

  • -mmin N: File's data was last modified N minutes ago.
  • -mtime N: File's data was last modified N*24 hours ago (i.e., N days).

For both options, you can prefix the number N with + or -:

  • -N: less than N (e.g., -mtime -7 means modified within the last 7 days).
  • +N: more than N (e.g., -mtime +30 means modified more than 30 days ago).
  • N (no sign): exactly N.

Linux/Mac Terminal Tutorial: How To Use The find Command

Let's watch how to use the time-based search options.

Watch from 04:29 to 06:54. Pay close attention to the difference between -mmin (minutes) and -mtime (days), and how the + and - symbols work.

Do It Yourself: Find by Time

  1. Create a test file:
    touch ~/test_find_file.txt
    
  2. Find recent files: Now find all files in your home directory that were modified in the last 5 minutes. Your new file should appear in the list.
    find ~ -mmin -5
    
  3. Find older config files: Let's find configuration files in /etc that have not been modified in over a year (365 days).
    find /etc -name "*.conf" -mtime +365 2>/dev/null
    
    This combines a name search with a time search. It's looking for items that are named *.conf AND were modified more than 365 days ago.

Putting It All Together: A Practical Scenario

This is where your preference for 80% practical work comes into play. Let's simulate a common task for a web developer.

Imagine you have a project directory for a blog. Let's create a simplified version of it. Run these commands in your terminal:




# Create the main project directory in your home folder
mkdir -p ~/my_blog/{posts,static/css,templates}




# Create some files
touch ~/my_blog/manage.py
touch ~/my_blog/posts/post1.md
touch ~/my_blog/posts/post2.md
touch ~/my_blog/static/css/style.css
touch ~/my_blog/templates/base.html
touch ~/my_blog/templates/post_detail.html




# Let's wait a moment and then create a backup file to simulate an older file
echo "This is a backup" > ~/my_blog/old_backup.txt

Now, navigate into your project and use find to answer the following questions.

cd ~/my_blog

Your Tasks:

  1. Find all files with a .html extension.
  2. Find all directories named posts.
  3. Find all markdown files (.md) inside the posts directory that were modified in the last 10 minutes.
  4. Find any file that is NOT a directory (i.e., find all files).
  5. Find any .py file.

Solutions (try to solve them yourself before looking!)

Click to see the solutions
  1. find . -type f -name "*.html"
  2. find . -type d -name "posts"
  3. find ./posts -type f -name "*.md" -mmin -10
  4. find . -not -type d or more simply find . -type f
  5. find . -type f -name "*.py"

For a quick textual reference on everything we've covered, this article is excellent.

Find Command in Linux (Find Files and Directories) | Linuxize

The article "Find Command in Linux" from Linuxize is a great cheat sheet. It clearly lays out the options for finding by name, type, and modification date.

Skim through the sections Find Files by Name, Find Files by Type, and Find Files by Modification Date. You don't need to read it word-for-word, but notice how the examples confirm what you've just practiced. It's a good page to bookmark for future reference.


Conclusion

Great job! You've just added one of the most powerful command-line tools to your skillset. The find command is your go-to utility for locating files and directories when you know something about them, but not necessarily where they are.

Key Takeaways:

  • The basic structure is find [path] [expressions].
  • Search by name with -name "pattern" and by case-insensitive name with -iname "pattern". Remember to use quotes around wildcards like *.
  • Filter by type using -type f for files and -type d for directories.
  • Search by modification time with -mtime (days) and -mmin (minutes). Use +N for "more than N ago" and -N for "less than N ago".
  • You can chain expressions together to create complex and powerful searches (e.g., find . -type f -name "*.log" -mtime -7).
Linux Find Command Examples
This image summarizes several practical uses of the `find` command, combining criteria like name, type, size, and modification time.

In our next and final lesson of this module, we will address a common administrative task: analyzing disk space. You'll learn how to use df and du to see how much space is being used by your filesystems and directories. This often goes hand-in-hand with find, as you might use find to locate large files and then du to confirm their size.