Linux Commands GuideLinux Tutorials

Find Command in Linux

The find command linux is an essential tool for locating files and directories by name, type, size, modification time and other attributes. This guide walks you from prerequisites and installation to practical examples, verification and troubleshooting so you can use find command linux confidently in daily tasks and scripts.

Prerequisites

Most distributions include the find utility from the GNU findutils package. Verify it and confirm the version before proceeding.

find --version
find (GNU findutils) 4.8.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Explanation: find –version reports the installed version. Knowing the version helps because some flags differ across releases.

Installation

If find is missing, install the GNU findutils package. Use sudo to run the package manager with elevated privileges — sudo temporarily runs the following command as the superuser.

sudo apt update && sudo apt install -y findutils
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
...
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  findutils
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 118 kB of archives.
After this operation, 322 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 findutils amd64 4.7.0-1ubuntu1 [118 kB]
Fetched 118 kB in 0s (400 kB/s)
Selecting previously unselected package findutils.
(Reading database ... 120000 files and directories currently installed.)
Preparing to unpack .../findutils_4.7.0-1ubuntu1_amd64.deb ...
Unpacking findutils (4.7.0-1ubuntu1) ...
Setting up findutils (4.7.0-1ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...

Explanation: The apt command installs packages on Debian/Ubuntu. Replace with yum or dnf on RHEL/CentOS/Fedora (example below).

sudo yum install -y findutils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.example.com
Resolving Dependencies
--> Running transaction check
---> Package findutils.x86_64 0:4.6.0-11.el7 will be installed
...
Installed:
  findutils.x86_64 0:4.6.0-11.el7

Setup and Basic Usage

Syntax recap: find [path] [expression]. If you leave [path] as . it starts from the current directory.

Finding a file by exact name

Always quote patterns to avoid shell expansion. Use -name for case-sensitive matches.

find . -name "example.txt"
./projects/example.txt

Explanation: This searches recursively from the current directory for files or directories named example.txt. Quoting prevents the shell from expanding wildcards before find runs.

Finding files with a pattern (case-insensitive)

find /home -iname "*.log"
/home/alice/system.LOG
/home/bob/app.log

Explanation: -iname performs a case-insensitive name match; /home is the starting path.

Find only directories

find . -type d -name "config"
./services/nginx/config
./projects/webapp/config

Explanation: -type d limits results to directories. Use -type f for regular files and -type l for symbolic links.

Limit search depth

find /etc -maxdepth 2 -name "*.conf"
/etc/ssh/sshd_config
/etc/hosts.conf

Explanation: -maxdepth 2 tells find not to recurse deeper than two levels from the start directory.

Find files by size

find / -type f -size +100M 2>/dev/null
/var/log/huge.log
/home/bob/videos/movie.mkv

Explanation: -size +100M finds files larger than 100 megabytes. Redirecting 2>/dev/null hides permission-denied messages from standard error.

Find files modified recently

find ~ -type f -mtime -1
/home/alice/.bash_history
/home/alice/notes/today.txt

Explanation: -mtime -1 finds files modified less than 1 day ago. Use +n for older than n days and n for exactly n days old.

Find files with a specific permission

find /srv -type f -perm 644
/srv/www/index.html
/srv/data/report.csv

Explanation: -perm 644 matches files whose permission bits are exactly 644. For matching any of the bits use -perm -644 (all specified bits set).

Find empty files and directories

find ./data -empty
./data/old_empty_file.txt
./data/archives/emptydir

Explanation: -empty matches zero-length regular files and empty directories.

Search text within files (combine find with grep)

find . -type f -name "*.txt" -exec grep -n "TODO" {} \;
./README.txt:42:TODO: add installation notes
./projects/todo.txt:1:TODO: refactor the module

Explanation: -exec grep -n "TODO" {} \; runs grep on each found file. The {} placeholder is replaced by the current filename. The command must end with \; to terminate -exec.

List matching files with details (combine with ls -lh)

find /var/log -type f -name "*.log" -size +50M -exec ls -lh {} \; 2>/dev/null
-rw-r--r-- 1 root root  210M Jun 10 10:02 /var/log/huge.log
-rw-r--r-- 1 syslog adm  120M May 28 07:15 /var/log/old.log

Explanation: Running ls -lh via -exec prints human-readable sizes for each match.

Delete matches safely (interactive)

find ./GFG -name "sample.txt" -exec rm -i {} \;
rm: remove regular file './GFG/sample.txt'? y
rm: remove regular file './GFG/sample.txt'? y

Explanation: Use -exec rm -i {} to prompt for confirmation before deleting each file. Avoid running non-interactive deletes on important paths. If you want to delete without prompting, -delete or -exec rm {} can be used (dangerous if used incorrectly).

Delete with -delete (careful)

find /tmp -type f -name "*.tmp" -mtime +7 -delete
(no output)

Explanation: -delete removes matched files. It prints no output by default; this command example deletes temporary files older than 7 days. Always run the same find without -delete first to preview results.

Exclude directories using -prune

find /var -path "/var/log" -prune -o -type f -name "*.conf" -print
/var/www/apache2.conf
/var/lib/someapp/config.conf

Explanation: -prune skips the specified path. The boolean operators -o (OR) and grouping are important — the expression after -o runs for non-pruned paths.

Find files owned by a user

find /home -user john -type f
/home/john/.profile
/home/john/projects/report.docx

Explanation: -user john finds files owned by the user john. Useful in multi-user environments for audits.

Verification

Confirm your commands work as expected by running them first in read-only mode (without -exec or -delete) and check sample output. Example: preview what would be deleted.

find /tmp -type f -name "*.tmp" -mtime +7
/tmp/session123.tmp
/tmp/archive_old.tmp

Explanation: Running the search first shows which files will be affected — a simple and critical verification step.

Troubleshooting

Permission denied messages

If you see many “Permission denied” lines, either restrict the starting path to locations you own or run with sudo if appropriate (be cautious):

sudo find / -name "mysecret.conf" 2>/dev/null
/etc/myapp/mysecret.conf

Explanation: sudo elevates the command to root to access protected paths. Redirecting stderr avoids clutter from permission messages.

Shell wildcard expansion issues

If you run find . -name *.txt without quotes and you have .txt files in the current shell, the shell expands the glob before find runs — causing unexpected behavior. Always quote patterns:

find . -name "*.txt"
./notes/todo.txt
./README.txt

Performance on large filesystems

Searching the entire filesystem can be slow. Use these approaches:

  • Limit start path (e.g., /home instead of /).
  • Use -maxdepth / -mindepth to limit recursion.
  • Exclude directories with -prune to avoid scanning heavy trees (e.g., exclude /proc, /sys, /dev).
find /home -maxdepth 3 -type f -name "*.jpg"
/home/bob/photos/vacation/IMG_001.jpg

Practical automation tips

When using find in scripts, always:

  • Quote patterns and variables to avoid unintended expansion.
  • Run the find expression first without -exec or with -print to verify results.
  • Prefer -exec ... + when possible to call the command with multiple arguments (more efficient):
find . -type f -name "*.log" -exec gzip {} +
./app.log.gz:  30% -- replaced with ./app.log.gz
./access.log.gz:  45% -- replaced with ./access.log.gz

Explanation: Using -exec ... + appends multiple filenames to a single command invocation (faster than one process per file).

Conclusion

Mastering the find command linux transforms how you manage and audit files across systems. Start with read-only searches, add filters like -type, -size, -mtime, and use -exec or -delete carefully. With these examples and troubleshooting tips you can build safe, efficient searches and automation scripts.

Further learning: combine find with tools such as grep, xargs, and shell scripts for powerful file management workflows.

Komentariši

Vaša email adresa neće biti objavljivana. Neophodna polja su označena sa *