0
0
Linux CLIscripting~5 mins

find by name, type, and size in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to locate files on your computer based on their name, type, or size. The Linux find command helps you search for files that match these conditions quickly and easily.
When you want to find all text files named report.txt in your documents folder.
When you need to locate all directories named backup on your system.
When you want to find files larger than 10 megabytes to free up disk space.
When you want to list all executable files in a specific folder.
When you want to combine conditions, like finding all log files larger than 5MB.
Commands
This command searches in /home/user/documents for files named exactly report.txt.
Terminal
find /home/user/documents -name "report.txt"
Expected OutputExpected
/home/user/documents/project/report.txt /home/user/documents/old_reports/report.txt
-name - Search files by exact name pattern.
This command finds directories (-type d) named backup anywhere under /home/user.
Terminal
find /home/user -type d -name "backup"
Expected OutputExpected
/home/user/documents/backup /home/user/backup
-type d - Limit search to directories only.
-name - Match directory name.
This command finds files (-type f) larger than 10 megabytes (+10M) in /var/log.
Terminal
find /var/log -type f -size +10M
Expected OutputExpected
/var/log/large_log1.log /var/log/large_log2.log
-type f - Limit search to files only.
-size +10M - Find files larger than 10 megabytes.
This command lists all executable files in /usr/local/bin.
Terminal
find /usr/local/bin -type f -executable
Expected OutputExpected
/usr/local/bin/script1 /usr/local/bin/tool
-type f - Limit search to files.
-executable - Find files with execute permission.
This command finds all log files larger than 5 megabytes in /home/user/logs.
Terminal
find /home/user/logs -type f -name "*.log" -size +5M
Expected OutputExpected
/home/user/logs/error.log /home/user/logs/access.log
-type f - Search files only.
-name - Match files ending with .log.
-size +5M - Files larger than 5 megabytes.
Key Concept

If you remember nothing else from this pattern, remember: the find command lets you combine name, type, and size filters to pinpoint exactly the files or directories you need.

Common Mistakes
Using -name without quotes around the pattern
The shell may expand wildcards before find runs, causing unexpected results.
Always put the pattern in quotes, like -name "*.txt".
Using -size 10M instead of +10M or -10M
-size 10M finds files exactly 10 megabytes, which is rare and often not what you want.
Use +10M to find files larger than 10MB or -10M for smaller than 10MB.
Not specifying -type when searching by name
You might get both files and directories matching the name, which can be confusing.
Add -type f for files or -type d for directories to narrow results.
Summary
Use find with -name to search files or directories by their name.
Add -type f or -type d to limit results to files or directories.
Use -size with + or - and a size unit (c, k, M, G) to filter by file size.