Search for Files with the find Command on Raspberry Pi
Learn how to use find on Raspberry Pi OS to search directory trees by filename, size, and file owner.
The Linux find command recursively searches a directory tree and prints paths that meet conditions you specify. On Raspberry Pi OS, it can help you locate entries by filename, size, permissions, or ownership.
If you need a refresher on opening a command-line session, see Terminal in Raspbian. This lesson assumes you understand basic Linux paths, directories, shell wildcards, and user accounts.
How find searches
A directory tree is a starting directory and the directories beneath it. When find starts at a path, it examines that location recursively, including its subdirectories.
The general structure is:
find PATH EXPRESSION
- PATH is the directory or filesystem location where searching begins.
- An expression is a test or condition, such as
-name,-size, or-user. - Matching paths are printed as the command's results.
The starting path and the expression have different jobs. The path controls which part of the filesystem is examined. The expression controls which entries are selected from that part.
| Expression | Search criterion | Example use |
|---|---|---|
-name | Filename pattern | find /home/pi/files/ -name "new*" |
-size | File or entry size | find /home/pi/files/ -size +60c |
-user | Recorded file owner | find /home/pi/files/ -user pi |
Choose the search path
For these examples, use /home/pi/files/ as the starting location. The trailing slash is optional, but it makes the directory path easy to recognize:
find /home/pi/files/ -name "new*"
This searches the files directory itself and everything below it. It does not search unrelated locations elsewhere in /home/pi/ or the rest of the filesystem.
A different starting path changes the search scope. For example:
find /home/pi/files/* -name "new*"
Here, the shell expands * before find runs. The immediate contents of /home/pi/files/ become starting paths. A matching subdirectory can still be searched recursively, but the files directory itself is not passed as a starting path. Shell expansion can also omit hidden entries, so using the directory path directly is usually clearer and more predictable.
Search by filename with -name
The -name expression compares an entry's name with a filename pattern:
find /home/pi/files/ -name "new*"
This command searches beneath /home/pi/files/ and selects names beginning with new. For example, it could print paths such as:
/home/pi/files/new_notes.txt
/home/pi/files/projects/new-project
/home/pi/files/projects/new-data.csv
A wildcard is a pattern character that represents variable text. In a filename pattern, * means any sequence of characters, including no characters. Therefore, new* matches new, new.txt, and new_backup.csv.
Quote a pattern containing *:
find /home/pi/files/ -name "new*"
The quotes create a quoted pattern: they tell the shell to pass new* unchanged to find. Without quotes, the shell may expand * using names in the current working directory before find receives the command. That can produce unexpected results or an error.
The -name test normally treats uppercase and lowercase letters as different. A pattern such as new* does not generally match NewNotes.txt.
Search by file size with -size
Use -size to match entries according to their size. A size value consists of an optional comparison prefix, a number, and a unit suffix:
find PATH -size +60c
The leading + means “larger than.” The command below finds entries larger than 60 bytes beneath the chosen directory:
find /home/pi/files/ -size +60c
Read the expression as follows:
+selects entries greater than the stated size.60is the threshold.csets the unit to bytes.
The returned list contains paths that exceed the threshold. Inspect each path to determine which files are large enough for your purpose. Without a comparison prefix, a size expression uses the stated size as its matching condition rather than explicitly requesting “larger than.”
| Suffix | Meaning | Example |
|---|---|---|
c | Bytes | -size +60c means larger than 60 bytes |
k | Kilobytes | -size +100k means larger than 100 kilobytes |
M | Megabytes | -size +10M means larger than 10 megabytes |
G | Gigabytes | -size +1G means larger than 1 gigabyte |
Use the suffix that matches the question you are asking. A byte threshold is useful for tiny files; megabyte or gigabyte thresholds are more practical when looking for large media, backups, or disk images.
Search by file owner with -user
The -user expression matches entries whose recorded owner is a named account:
find /home/pi/files/ -user pi
This locates entries under /home/pi/files/ owned by the pi user. In Raspberry Pi OS examples, pi is the conventional default account name, although an installation may use a different account.
Ownership is separate from the account running the command. For example, you can run find while logged in as one user and search for files owned by pi. The expression checks the owner recorded in the filesystem; it does not mean “files created by the current command user.”
What results should you expect?
By default, find can report both files and directories that satisfy an expression. The examples use the term “files” in the everyday sense, but the command searches filesystem entries. Results depend on the actual contents of the Raspberry Pi's storage and on the permissions available to the account running the command.
For example, this command may return a directory named new-project as well as regular files whose names begin with new. If you need only regular files, Linux systems commonly add the -type f test:
find /home/pi/files/ -type f -name "new*"
The required examples without -type f are useful for learning the basic expressions and for locating any matching filesystem entry.
Troubleshooting find commands
A wildcard search gives unexpected results
The pattern may not have been quoted. Put double quotes around patterns containing *:
find /home/pi/files/ -name "new*"
This prevents the shell from expanding the wildcard before find sees it.
No files are found
Check the starting path, spelling, filename pattern, size threshold, and owner name. Begin with a simple search, then add a condition:
find /home/pi/files/
If this prints paths, progressively test -name, -size, or -user. The filesystem may simply contain no entries matching the complete expression.
The size result is not what you expected
Check both the unit suffix and comparison prefix. Use c for bytes, k for kilobytes, M for megabytes, and G for gigabytes. Add + when the requirement is “larger than”:
find /home/pi/files/ -size +60c
Permission-denied messages appear
The current account cannot read one or more directories in the search path. Search a directory that the account can access, such as a suitable location beneath /home/pi/, or use an appropriately authorized account when you have permission to do so. Permission messages do not necessarily mean that every other part of the search failed; readable locations may still produce results.
Quick reference
# Names beginning with new
find /home/pi/files/ -name "new*"
# Entries larger than 60 bytes
find /home/pi/files/ -size +60c
# Entries owned by pi
find /home/pi/files/ -user pi
Remember the central pattern: choose the correct PATH first, then add an expression that describes the entries you want. For more command-line practice, see Useful Terminal Commands, Directory Management, and File Management.