متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 10System · Linux

How to find files by name, size, date or content

Where is that config file? Which log changed in the last hour? What fills the disk? Which files contain a password? find and grep answer all of these. This how-to gives the commands you use every day, with safe ways to act on the results (and to delete only what you meant).

  • 5 الدروس
  • 8 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

10.1

The quick answer

find / -name "nginx.conf" 2>/dev/null
find /var/log -type f -mmin -60
find / -xdev -type f -size +1G -exec ls -lh {} +
grep -rIl "listen 8080" /etc
locate sshd_config

الأوامر في هذا الدرس

10.2

Step by step

  1. By name — quote the pattern, -iname ignores upper/lower case:
find /etc -name "*.conf"
find / -iname "*backup*" -type f 2>/dev/null
find /home -name ".env" -type f
  1. 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
TestMeaning
-type f / -type d / -type lFile / folder / link.
-mtime -1 / -mtime +7Changed in the last day / more than 7 days ago (-mmin = minutes).
-size +500MBigger than 500 MB (k, M, G).
-xdevStay on this file system (do not walk into /proc or other disks).
-user alice / -perm -4000Owned by alice / has the SUID bit.
  1. 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.

  1. 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.

  1. Very fast name search with the locate database (package mlocate/plocate):
dnf install -y plocate
updatedb
locate -i nginx.conf

The database is updated daily — new files appear after updatedb.

10.3

Check that it worked

Count and confirm before you change anything:

find /tmp -type f -mtime +7 | wc -l
find /tmp -type f -mtime +7 | head

10.4

Common problems

ProblemCauseFix
Many Permission denied linesNormal user in system folders.Use sudo, or hide errors with 2>/dev/null.
find: paths must precede expressionThe 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 slowIt walks the whole system, network mounts too.Start in a narrower folder; add -xdev.
locate does not find a new fileThe database is old.updatedb.
xargs breaks on names with spacesPlain xargs.Use -print0 | xargs -0, or -exec … {} +.

الأوامر في هذا الدرس

10.5

Go further

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.

كل الأوامر في هذا الفصل