هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- By name — quote the pattern,
-inameignores upper/lower case:
find /etc -name "*.conf"
find / -iname "*backup*" -type f 2>/dev/null
find /home -name ".env" -type f- By type, time and size:
find /srv -type d -empty
find /var/log -type f -mtime -1
find /tmp -type f -mtime +7
find / -xdev -type f -size +500M
find /etc -newer /etc/passwd -type f| Test | Meaning |
|---|---|
-type f / -type d / -type l | File / folder / link. |
-mtime -1 / -mtime +7 | Changed in the last day / more than 7 days ago (-mmin = minutes). |
-size +500M | Bigger than 500 MB (k, M, G). |
-xdev | Stay on this file system (do not walk into /proc or other disks). |
-user alice / -perm -4000 | Owned by alice / has the SUID bit. |
- By content with
grep:
grep -rn "PermitRootLogin" /etc/ssh/
grep -rIl --include="*.yml" "password:" /srv/ansible
grep -rn --exclude-dir=.git -E "AKIA[0-9A-Z]{16}" ~/projects-r recursive, -n line numbers, -l file names only, -I skip binary files.
- Act on the results — safely. First list, then act:
find /tmp -type f -mtime +7 -print
find /tmp -type f -mtime +7 -delete
find /srv/www -type f -name "*.php" -exec chmod 640 {} +
find /var/log -name "*.log" -print0 | xargs -0 grep -l "OutOfMemory"Always run the command with -print first and read the list; -delete must be the last option.
- Very fast name search with the
locatedatabase (packagemlocate/plocate):
dnf install -y plocate
updatedb
locate -i nginx.confThe database is updated daily — new files appear after updatedb.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
Many Permission denied lines | Normal user in system folders. | Use sudo, or hide errors with 2>/dev/null. |
find: paths must precede expression | The pattern was not quoted and the shell expanded it. | Quote it: -name "*.conf". |
-delete removed more than expected | -delete was placed before the tests. | Always put -delete last, and test with -print first. |
| Very slow | It walks the whole system, network mounts too. | Start in a narrower folder; add -xdev. |
locate does not find a new file | The database is old. | updatedb. |
xargs breaks on names with spaces | Plain xargs. | Use -print0 | xargs -0, or -exec … {} +. |
Go further
- Next how-tos: Change file permissions and owner, Create and extract tar.gz archives, Check disk space, Search text in many files.
- Command pages: find, grep, xargs, stat.
- Related how-to: Check disk space and find what fills the disk.
- Learn it for RHCSA: RHCSA — Files, RHCSA — Text. Practise: Lab: files.
1How do you find files changed in the last hour under /var/log?
2Why test with -print before -delete?
To read exactly which files match before anything is removed.