Linux Find Command: 10 Easy Examples and Mistakes to Avoid
The linux find command searches a directory tree for files that match tests you give it, and can run an action on each match. The old version of this post listed the options without output, and a few of its examples would surprise you in real use. This rewrite runs every example on a small generated tree with GNU findutils 4.10.0 (Debian 13) and shows what the linux find command really prints, including the mistakes. The full option list is in the find(1) man page.
Contents
- The test tree for the linux find command
- Example 1: find by name with the linux find command
- Example 2: exclude a directory in the linux find command
- Example 3: symbolic links
- Example 4: size and modification time with the linux find command
- Example 5: permissions are exact unless you add a sign
- Example 6: -exec, + and names with spaces
- Example 7: -delete, and why the order of tests matters
- Example 8: quote your patterns
- Example 9: -o needs parentheses
- Example 10: depth, output format and newer files
- Common linux find command mistakes
- FAQ
The test tree for the linux find command
All outputs below come from this tree: src/main.c (mode 755), src/lib/util.c, a hidden .backup/old/main.c.bak, docs/README.TXT, docs/notes.txt (mode 775, modified 30 hours ago), a 1.5 MB docs/big.txt, a 200 KB docs/medium.bin, three files in logs/ (one 10 days old), dir with space/My File.txt, a.tmp, and two symbolic links, link_to_main and link_to_docs.
Example 1: find by name with the linux find command
$ find . -name "main.c"
./src/main.c
$ find . -name "readme.txt"
$ find . -iname "readme.txt"
./docs/README.TXT
-name is case sensitive and matches the base name only, so the lower-case search found nothing. Use -iname to ignore case. Always quote the pattern: Example 8 shows why.
Example 2: exclude a directory in the linux find command
The old post used -not -path "*/.backup*". It works, but find still walks into the excluded directory. -prune stops the walk:
$ find . -path ./.backup -prune -o -type d -print | sort
.
./dir with space
./docs
./logs
./src
./src/lib
The -not -path version printed 6 directories where a plain -type d listed 8, so both hid .backup and .backup/old. On a big tree such as a home folder with a cache, -prune is much cheaper.
Example 3: symbolic links
$ find ./ -type l -exec ls -l {} \;
lrwxrwxrwx. ./link_to_main -> src/main.c
lrwxrwxrwx. ./link_to_docs -> docs
$ find . -type f -name notes.txt | wc -l
1
$ find -L . -type f -name notes.txt | wc -l
2
By default find does not follow symbolic links. With -L it walked into link_to_docs, and the same file was found twice. Use -L only when you want links followed, and expect duplicates.
Example 4: size and modification time with the linux find command
$ find . -type f -name "*.txt" -and -size +1M
./docs/big.txt
$ find . -type f -size +100k
./docs/big.txt
./docs/medium.bin
$ find logs -type f -mtime -7
logs/app2.log
logs/app3.log
$ find logs -type f -mtime +7
logs/app1.log
$ find docs -type f -mtime -1
docs/README.TXT
docs/big.txt
docs/medium.bin
Two details matter. A bare number in -size counts 512-byte blocks, so use a suffix such as k or M. And -mtime -1 means less than one 24-hour period: docs/notes.txt, modified 30 hours ago, was not listed. For hours or minutes, use -mmin.
Example 5: permissions are exact unless you add a sign
Permissions are where the linux find command surprises people most. The old FAQ said find -perm 755 finds files "with 755 permissions". Without a sign it matches exactly that mode, and nothing else:
$ find . -type f -perm 755
./src/main.c
$ find . -type f -perm -755
./src/main.c
./docs/notes.txt
$ find . -type f -perm /111
./src/main.c
./docs/notes.txt
-perm -755 means "at least these bits", so the 775 file matches too. -perm /111 means "any execute bit". Choose the form that matches your question before you act on the result.
Example 6: -exec, + and names with spaces
$ find . -type f -name "*.log" -exec echo {} \; | wc -l
3
$ find . -type f -name "*.log" -exec echo {} + | wc -l
1
With \;, find starts one command per file: 3 runs for 3 logs. With + it passes many files to one command, 1 run, which is faster for big lists. Piping to xargs is where spaces bite:
$ find . -type f -name "*.txt" | xargs ls -l
ls: cannot access './dir': No such file or directory
ls: cannot access 'with': No such file or directory
ls: cannot access 'space/My': No such file or directory
$ find . -type f -name "*.txt" -print0 | xargs -0 ls -l | wc -l
3
Use -print0 with xargs -0, or stay with -exec ... +. Names with spaces are normal on real systems.

Example 7: -delete, and why the order of tests matters
The linux find command can delete as well as search, so treat it with care.
$ find . -name "*.tmp" -type f -delete
$ ls a.tmp
ls: cannot access 'a.tmp': No such file or directory
That is safe. The danger is putting -delete first. On a copy of the tree, find . -delete -name "*.nomatch" deleted everything, even though no file matches that name: find evaluates tests left to right, and -delete is true for every file it reaches, so it ran before the name test. Preview with -print or -exec echo first, and put -delete last.
Example 8: quote your patterns
$ cd src; find . -name *.c
./main.c
$ cd ..; touch main.c; find . -name *.c
./main.c
./src/main.c
This is a shell problem, not a bug in the linux find command. The shell expands an unquoted *.c before find sees it. In src it became main.c, so util.c in the subfolder was never found, and the command did not report an error. Write -name "*.c".
Example 9: -o needs parentheses
$ find . -type f -name "*.log" -o -name "*.tmp" -print
$ find . -type f \( -name "*.log" -o -name "*.c" \)
./logs/app1.log
./logs/app2.log
./logs/app3.log
./src/lib/util.c
./src/main.c
Boolean logic in the linux find command is easy to get wrong. Without parentheses, -print belongs only to the last branch, and the first command printed nothing (the only .tmp file had already been deleted in example 7). Group alternatives with \( ... \).
Example 10: depth, output format and newer files
$ find . -maxdepth 1 -type f
$ find . -type f -name "*.txt" -printf "%s %p\n" | sort -rn | head -2
1500000 ./docs/big.txt
2 ./docs/notes.txt
$ find . -type f -newer logs/app1.log | wc -l
10
-maxdepth 1 lists only the top level (no regular files remained there in this tree). -printf prints size and path without a second command, and -newer FILE selects files modified after FILE.
Common linux find command mistakes
- Unquoted patterns, so the shell changes the search.
-deletebefore the tests, or without a preview.-perm 755when you meant "at least" (-perm -755).| xargswithout-print0on names with spaces.-owithout parentheses.- Following links with
-Land counting files twice.
For the next step after finding files, the tar listing post shows how to inspect archives without extracting, and the wc post shows how to count the results correctly.
FAQ
Is the linux find command part of every distribution? The lab used GNU findutils 4.10.0. Other implementations (BSD, busybox) differ in details such as -printf, which this post did not test.
How do I search file contents? The linux find command filters by name and attributes. Combine it with grep, for example -exec grep -l text {} +, which was not run in this lab.
Why is find slow on a big tree? It walks every directory. Use -prune for folders you do not need, and -maxdepth to limit the walk.
Comments
Post a Comment