0
0
Linux-cliHow-ToBeginner · 3 min read

How to Find a File in Linux: Simple Commands Explained

Use the find command in Linux to search for files by name, type, or other attributes. For example, find /path -name "filename" searches for a file named filename starting from /path.
📐

Syntax

The basic syntax of the find command is:

  • find [path] [options] [expression]

Here, path is where to start searching (like / for root or . for current directory).

options and expression define what to look for, such as file name, type, or size.

bash
find [path] [options] [expression]
💻

Example

This example searches for a file named notes.txt starting from the current directory:

bash
find . -name "notes.txt"
Output
./documents/notes.txt ./backup/notes.txt
⚠️

Common Pitfalls

Common mistakes include:

  • Not specifying the correct path, which may cause slow searches or no results.
  • Using -name without quotes when the filename contains special characters or spaces.
  • Confusing -name (case-sensitive) with -iname (case-insensitive).

Always quote the filename pattern to avoid shell expansion issues.

bash
Wrong: find . -name notes.txt
Right: find . -name "notes.txt"
📊

Quick Reference

OptionDescriptionExample
-nameSearch by file name (case-sensitive)find / -name "file.txt"
-inameSearch by file name (case-insensitive)find . -iname "file.txt"
-type fSearch for files onlyfind . -type f -name "file.txt"
-type dSearch for directories onlyfind /home -type d -name "docs"
-size +10MFind files larger than 10 megabytesfind . -size +10M
-mtime -7Find files modified in last 7 daysfind /var/log -mtime -7

Key Takeaways

Use the find command with a starting path and search criteria to locate files in Linux.
Always quote file names in the find command to avoid shell interpretation errors.
Use -iname for case-insensitive name searches and -type to filter by file or directory.
Specify the correct path to speed up searches and get accurate results.
Combine options like -size and -mtime to refine your file search.