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

1. Use the ‘find’ command:

`find / -name `

2. Use the ‘locate’ command:

`locate `

3. Use the ‘grep’ command:

`grep -R /`

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

How do I find a file on Raspberry Pi?
Find, locate and grep are three commands very useful to search for a file on Raspberry Pi.
They accept 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 in the command line, I recommend checking my tutorial on how the Raspberry Pi file system works.
All the commands listed here can be use 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.

Find

Introduction

Find is the command that most people will give you when you ask where is my file.
It’s powerful and will save you most of the time, as you can add many criteria to filter the results you are interested in.
Unfortunately, many criteria means many options to remember when you try to use it.

I will try to keep this simple, and give you the most useful options 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 file), but most of the time you will use both.
Also, when looking for a file in the whole file system, don’t forget to use sudo to get access to the system files.

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

This is perfect if you know the exact name of a file and want to locate it, but it’s not working if you only have a part of the name.
If you also want files containing the word you provide in parameter, you can use stars as wildcard, for example:
sudo find /var -name *syslog
sudo find /var -name syslog*
sudo find /var -name *syslog*

The first one is for file ending with “syslog”, the second starting with “syslog” and the last one containing “syslog”.

And a last alternative for a basic search, is to use the “-iname” option rather than “-name”.
It will display the results, for insensitive case match.
If a file is name “SYSlog” somewhere, it will find it with iname, not with name.

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 first one is to find files depending on their creation, access or modification date.

It works all the same, 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 this option, you can specify a threshold, and say if it should be above or that threshold.
A few examples will be clearer ?

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

It’s very useful when you want to clean your system, or find a file you recently created or modified but don’t remember where.

Size option

The last option 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 at my beginning on Linux, as I never reminded 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.

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:
    locate -i SysLog
  • Only filenames containing the string, exclude 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 /usr/bin
My “locate” command will give me too many results, but if I’m only interested in the binary file, I can filter the results that contains “/usr/bin”.

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.

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/pi/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 (from “buster” to the “bullseye” or whatever version name).
You can do a search on the whole system with:
sudo grep --color -nHr 'buster' *

So the files I’m looking for are in /etc/apt, and I have 4 files to edit to make this change.
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
  • ‘buster’: 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.

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

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.

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? 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.
Download the e-book.

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

Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

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

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

If you’re a Raspberry Pi user, you know that finding a file can be a challenge. Fortunately, there are several commands you can use to search for a file on your Raspberry Pi. Here are three of the most useful commands to help you find the file you’re looking for.

1. Find

The find command is one of the most powerful and versatile commands for searching for files on your Raspberry Pi. It can search for files by name, type, size, and other criteria. To use the find command, open a terminal window and type find [options] [path] [expression]. For example, to search for a file named “example.txt” in the current directory, you would type find . -name example.txt.

2. Locate

The locate command is another useful tool for searching for files on your Raspberry Pi. It searches through a database of all the files on your system, so it’s much faster than the find command. To use the locate command, open a terminal window and type locate [options] [expression]. For example, to search for a file named “example.txt”, you would type locate example.txt.

3. Grep

The grep command is a powerful tool for searching for text within files. It can search for text within a single file, or it can search through multiple files at once. To use the grep command, open a terminal window and type grep [options] [expression] [files]. For example, to search for the text “example” in all files in the current directory, you would type grep example *.

These three commands are some of the most useful tools for searching for files on your Raspberry Pi. With these commands, you’ll be able to quickly and easily find the file you’re looking for.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?