how to find a file on raspberry pi

3 Commands to Search For a File on Raspberry Pi (and find it!)

On Raspberry Pi OS (especially the Lite version), there is no fancy tool to find a file on your SD card. And yet, it would be quite useful!
No worries, you can do everything with a few commands that I will introduce in this tutorial.

Find, locate and grep are three useful commands to search for a file on Raspberry Pi.
They have interesting options, to quickly locate any file on the Raspberry Pi file system.

In this post, I’ll explain the command syntax for each one, and give you a few examples, so you can use them correctly. I will also give you a bonus tip at the end 🙂

Before going further, if you are really new to the command line, I recommend checking my tutorial on how the Raspberry Pi file system works. All the commands listed here can be used on most Linux operating systems. Just connect to your Raspberry Pi with SSH, open a session if on a minimal system, or start a terminal on Desktop.

If you’re looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

Find

Introduction

Find is the command that most people will suggest when you ask them how to find a file on Linux.

It’s powerful and can save you a lot of time because you can add many criteria to filter the results you’re interested in. Unfortunately, many criteria mean many options to remember when you try to use it.

I will try to keep this simple, and give you the most useful parameters only.

Basic example

The find syntax is generally formatted as:
find [path] [options]

You can remove the path to search in the current directory (recursively), or the options (to display all files), but most of the time you will use both.

If you are searching for a file in the entire file system, don’t forget to use sudo to get access to everything.

Here is a simple example to clarify:
sudo find /var -name journal
And the result is:

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

This is perfect if you know the exact name of a file and want to find it, but it won’t work if you only have part of the name.

If you also want files that contain the word you specify in the parameter, you can use asterisks as a wildcard, for example:
sudo find /var -name *journal
sudo find /var -name journal*
sudo find /var -name *journal*

The first command is for files ending with “journal”, the second for the ones starting with “journal” and the last one containing “journal” anywhere in their path.

Quick tip: you can use the “-iname” option rather than “-name”.
sudo find /var -iname *JouRnAL*
It will display the results, for insensitive case match.
If a file is named “JouRnAL” somewhere, it will find it with iname, not with name.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.

Date option

Ok, so the name/iname option is cool, but what if I don’t know the name of the files I’m looking for? Well, find has many options to help you in this case.

The “find” command allows you to search for files based on their creation, last access, or modification date.

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

All these options work the same way, so I’ll give you all the options at once:

  • -cmin: Difference in minutes from the creation date.
  • -ctime: Difference in days from the creation date.
  • -mmin: Same thing for the modification minutes.
  • -mtime: And modification days.
  • -amin: This one is for access time elapsed in minutes.
  • -atime: Or days.

For each of these options, you can specify a threshold, and say if it should be above or that threshold. A few examples will be clearer 🙂

  • Find files in your home directory created less than 60 minutes ago:
    find /home/pat -cmin -60
  • Look for files in /var/log that have not been modified in the last 3 days:
    sudo find /var/log -mtime +3

This is very useful when you want to clean up your system or find a file you recently created or modified but can’t remember where you put it.

Size option

Another option you can try is to look for specific file sizes. It’s also useful when you want to free disk space on your SD card.

It works almost the same as the previous one, so I’ll directly give you two examples:

  • Find files bigger than 100MB on your system:
    sudo find / -size +100M
  • Or smaller than 100M:
    sudo find / -size -100M
  • Or even empty files:
    sudo find / -size 0

Excellent command to find the biggest files on a system or share, and remove them 🙂

Going further

As I told you at the beginning, the idea here is not to master the find command, as there is so many things to say.
But with these options, you can already find files in most cases.
I will add two tips to these before switching to the next tool.

  • You can combine several options in the files command. For example:
    sudo find /var/log -size +100M -mtime +30
    Will list all log files over 100M and not modified in the last month.
  • You can look at the manual page if you are interested in other options:
    man find
    For example, you can execute a command on the results, display files only, disable recursive mode, etc.

Locate

Let’s check the “locate” command now.

Introduction

Locate is a command that is easier to use for beginners.
I remember using it all the time in my early days on Linux, as I never remembered the find command options.

The difference with find, is that locate will create a temporary database regularly, to find files quickly (instead of looking for all files with each new request).

Installation

Now, on recent system versions, it’s not longer included in the system, so you have to start by installing it.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

Here is the command to install it on Raspberry Pi OS:
sudo apt install mlocate

The first thing to do after the installation is to create the database.
Here is the command:
sudo updatedb

I think that this command is automatically done daily on your system, but you can schedule a task if you want to program it as you want.

Simple search

Once done, you can now use locate directly.
The main goal with locate is to quickly find a file based on its name.
There are a few options, but it’s really simpler than find.

So, the syntax is always something like:
locate [OPTION] [SEARCH]

You never need sudo with this command, as the database is already created and updated with it.

Here are three examples of what you can do:

  • Find all file name containing your search pattern:
    locate syslog.1
    There is no wildcard here, so this command:
    locate syslog

    will find any file name containing the word “syslog”
  • Ignore case in your search with:
    locate -i SysLog
  • Filter the results on filenames containing the string (excluding matches in path name):
    locate -b syslog

That’s the main options you’ll use (and probably only the first one).
As you can see, locate is perfect when you know the file name and want a quick result, but not so useful in other cases.

Filter with grep

Locate is limited in options, but you can still filter the results with other Linux commands, for example: grep.

I’ll give more details about grep in the next part, but for the moment, just remember that grep is a command that you can use to search for an expression. So we can use it after locate to display only the lines we want.

The syntax will be:
locate [FILE] | grep [EXPRESSION]

For example:
locate syslog | grep man
My “locate” command will give me too many results, but if I’m only interested in the files with “man” in their path or names, I can filter the results containing only that.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

Grep

The last command you can use to search for files is grep.
I already give you the general idea, but it can do much more than that.

I have a full introduction guide to the grep command on Linux on this website, that I encourage you to check out, but I’ll give you the short version here, focusing on file search.

Introduction

You already learn many ways to find a file since you’re reading this, but to be absolutely complete, there are a few things that are missing.
Grep is a powerful tool, to search strings in a text or in a file.

I already explained the basics of this command that you can combine with any other one to filter the results, but what is fascinating is to look inside files.

Use it to search in file content

Here is an example of why I use it so often.
Let’s say you have a folder with all your source code, maybe /home/pat/code.

You are looking for all files using a function you just changed (let’s say “send_email()”).
You can’t do this with find or locate, as they are just search in file names or attributes, not in the file content.

But grep can do this!
The syntax we’ll use is:
(sudo) grep [OPTIONS] [SEARCH] [PATH]

sudo can be added if you are looking in system folders.
Then there are many options you can add (check the man page), and finally the search string and path are similar to what we have seen previously.

Let’s take another example: I want to update my Raspberry Pi OS version, but I don’t know where are the apt files to edit to change the repository name.
I can do a search on the entire system with:
sudo grep --color -nHr 'bookworm' *

So the files I’m looking for are in /etc/apt, and I have their names, so I can edit them easily.
Here are the options I used:

  • –color: put file name and string matches in colors
  • -n: display the line number for each match
  • -H: display the file name for each match
  • -r: use a recursive search
  • ‘bookworm’: the string I’m looking for in any file
  • *: I’m searching in any file under my current path (“/”)

As always, “man <command>” will give you every option you might need in your case.
I often use -v (list files that doesn’t match the search expression), and -A/-B/-C to display lines around the match.

Want to learn more? How To Use ‘grep’: The Complete Linux Command Guide

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

Conclusion

That’s it, if you remember these 3 commands, you should be able to find any file on your Raspberry Pi.

In fact, there are common commands on any Linux system, so it may help you a lot in other situations. That’s really commands I’m using almost every day at work.

Also, I promise a bonus at the end, but I have three for you 🙂

  • Another command that can be useful in some cases is “whereis”.
    It will give you the location of a binary file.
    For example:
    whereis syslog
    Will only display the service location, not all the logs and documentations files.
  • If you are on desktop, there are also graphical tools that you can use as a search engine.
    For example, you can try Tracker or Recoll, both available in the default repository.
  • And finally, remember that you can mix these 3 commands with most other Raspberry Pi commands. If you are interested in this, read my selection here of the most useful commands on Raspberry Pi (and get a free cheat sheet to remember them ^^)

By the way, if you are on a traditional PC with Ubuntu, there are other great options you can use to find a file quickly. Check this article for more details.

Whenever you’re ready, here are other ways I can help you:

The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts