هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
find walks through every folder below a starting point and checks each file against an expression of tests. By default it prints the matching names; with actions it can list, delete or run any command on them.
The pattern is: find WHERE TESTS ACTIONS. Several tests are joined with AND; use -o for OR, ! (or -not) for NOT, and \( … \) for groups.
Numbers in tests: +N = more than N, -N = less than N, N = exactly N. Times are in days (-mtime) or minutes (-mmin).
It reads the real file system, so it is always correct (unlike locate, which uses a database), but it can be slow on big trees. Errors like "Permission denied" go to stderr: add 2>/dev/null.
طريقة الكتابة
find [-H] [-L] [-P] [-D DEBUG] [-O LEVEL] [STARTING-POINT...] [EXPRESSION]الخيارات 68
الأكثر استخدامًا
Before the starting point
Global options (put them first in the expression)
Tests: name and path
Tests: type, owner, permissions
Tests: size and content
Tests: time
Operators
Actions
-ls- Print details like
ls -dils.
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 17
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Find files by name
find /etc -name "*.conf"Quote the pattern, so the shell does not expand it first.
2Find all files of a user (exam classic)
find / -user student -type f 2>/dev/null2>/dev/nullhides the "Permission denied" lines.3Copy all files of a user to a folder
mkdir -p /root/found find / -user student -type f -exec cp -a --parents {} /root/found/ \; 2>/dev/null--parentskeeps the paths, so files with the same name do not overwrite each other.4Find big files that fill a disk
find / -xdev -type f -size +500M -exec ls -lh {} \; 2>/dev/null-xdevstays on one file system, for example only/.5Find files changed in the last 24 hours
find /etc -mtime -1 -type fAfter an incident or an update: what changed?
6Find SUID and SGID programs (security audit)
find / -xdev -type f \( -perm -4000 -o -perm -2000 \) -ls 2>/dev/nullCompare the list with a known-good list. A new SUID file can be a backdoor.
7Find world-writable files
find / -xdev -type f -perm -0002 ! -path "/proc/*" 2>/dev/nullFiles everyone can change are a risk, except on purpose in /tmp.
8Find files without an owner
find / -xdev \( -nouser -o -nogroup \) 2>/dev/nullLeft over from deleted users. Give them an owner or remove them.
9Delete old logs safely
find /var/log/myapp -name "*.log" -mtime +30 -print find /var/log/myapp -name "*.log" -mtime +30 -deleteRun with -print first, look at the list, then use -delete.
10Run a command on many files at once (fast)
find . -name "*.sh" -exec chmod 750 {} ++passes many names in one call.11Find text inside files
find /etc -type f -name "*.conf" -exec grep -l "PermitRootLogin" {} + 2>/dev/nullgrep -lprints only the names of files that contain the text.12Handle names with spaces safely
find /srv/uploads -type f -name "*.tmp" -print0 | xargs -0 rm -fNUL separators work with any file name.
13Search only the top level of a folder
find /var/www -maxdepth 1 -type dLists the sites, not every folder inside them.
14Skip a folder
find /srv -path /srv/backup -prune -o -name "*.php" -printEverything except what is under /srv/backup.
15Find files with a wrong SELinux label
find /var/www/html ! -context "*httpd_sys_content_t*" -lsFiles that Apache cannot read because of their label.
16Find broken symbolic links
find /etc -xtype lLinks whose target was removed.
17Print a custom report
find /home -type f -size +100M -printf "%s\t%u\t%p\n" | sort -nSize, owner and path; sorted by size.
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Order matters:
-deletebefore a test deletes everything. Put tests first, the action last. -deleteand-exec rmare permanent. Always run the same command with-printfirst.- Quote patterns (
"*.log"), or the shell expands them in the current folder first. -mtime +1means "more than 1 full day ago", which is at least 48 hours.
نصائح للحفظ
- find WHERE WHAT DO:
find /var -name "*.log" -size +100M -ls. -exec … {} \;= one call per file,-exec … {} += few calls (faster).