الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

file

Find out what kind of data a file contains (text, script, program, image, archive …), whatever its name.

المستوى
لينكس اليومي
المجموعة
الملفات والمجلدات
يحتاج root
لا
الحزمة
file

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

ماذا يفعل

Linux does not need file endings: report can be a PDF and photo.jpg can be a script. file looks at the content (the first bytes, "magic numbers") and tells you the real type.

Use it before you open an unknown file, when you investigate a suspicious upload, or to see if a program is 32-bit, 64-bit or for ARM.

طريقة الكتابة

file [OPTIONS] FILE...
file -C [-m MAGICFILES]

الخيارات 30

الأكثر استخدامًا

-b, --brief
Print only the type, without the file name.
-i, --mime
Print the MIME type and charset, like text/plain; charset=utf-8.
--mime-type, --mime-encoding
Print only the MIME type / only the charset.
-z, --uncompress
Look inside compressed files.
-Z, --uncompress-noreport
Look inside compressed files, report only the content.
-L, --dereference
Follow symbolic links.
-h, --no-dereference
Do not follow links (the default).
-s, --special-files
Read block and character devices too (like a disk /dev/vda1).
-k, --keep-going
Do not stop at the first match: show all matching types.
-f, --files-from file
Read the file names from a file (one per line).
-F, --separator sep
Use another separator than : after the name.
-N, --no-pad
Do not pad the names to line up the output.
-0, --print0
Print a NUL after the name (for safe scripts).
-p, --preserve-date
Do not change the access time of the file.
-r, --raw
Do not translate unprintable characters.
-E
On errors (file not found), print an error and stop with a failure code.
-e, --exclude test
Skip one test: apptype, ascii, encoding, tokens, cdf, compress, csv, elf, json, soft, tar.
--exclude-quiet
Like --exclude, but no error for unknown tests.
--extension
Print the usual file endings for this type.
-l, --list
List the strength of each magic pattern.
-m, --magic-file list
Use these magic files instead of the default.
-C, --compile
Compile a magic file into a .mgc file.
-c, --checking-printout
Show a parsed form of the magic file (debug).
-P, --parameter name=value
Set limits of the parser (like bytes to read).
-n, --no-buffer
Flush the output after each file.
-S, --no-sandbox
Turn off the system call sandbox.
-d
Print debug information.
--apple
Print the Apple creator/type.
-v, --version
Show the version.
--help
Show help.

أمثلة 11

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

  1. 1What is this file?

    file mystery
    mystery: PDF document, version 1.7

    The name had no ending; the content says PDF.

  2. 2Check a program's architecture

    file /usr/bin/ls
    /usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, dynamically linked …

    A download for the wrong CPU (ARM vs x86-64) gives "Exec format error"; file shows why.

  3. 3Get the MIME type for a script

    file -b --mime-type upload.jpg
    image/jpeg

    Web apps should check the real type of uploads, not the file name.

  4. 4Find a disguised script in an upload folder

    file /var/www/uploads/* | grep -iv image

    A "picture" that is really PHP script is a typical web shell.

  5. 5Check the text encoding

    file -i data.csv
    data.csv: text/plain; charset=iso-8859-1

    Convert to UTF-8 with iconv before an import.

  6. 6Look inside a compressed file

    file -z backup.sql.gz

    Shows the type of the data inside the gzip file.

  7. 7Check what is on a disk partition

    file -s /dev/vdb1
    /dev/vdb1: SGI XFS filesystem data …

    Find the file system type of a partition (or "data" when it is empty).

  8. 8Check Windows line endings in a script

    file deploy.sh
    deploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

    CRLF breaks bash scripts ("bad interpreter"). Fix it with sed -i 's/\r$//' deploy.sh.

  9. 9Check many files from a list

    find /tmp -type f -newer /tmp/marker > list.txt; file -f list.txt

    Classify every new file in one run (incident response).

  10. 10See if a binary is static or dynamic

    file /usr/local/bin/kubectl

    "statically linked" binaries do not need system libraries.

  11. 11Only the type, no name

    file -b /etc/passwd
    ASCII text

    Easy to use in if tests in scripts.

انتبه

  • file guesses from patterns; very short or special files can be reported wrongly.
  • Running file on hostile files is mostly safe (it uses a sandbox), but do not open them in other programs to "check".

الملفات

/usr/share/misc/magic.mgc
The compiled database of file patterns.
/etc/magic
Your own extra patterns.

نصائح للحفظ

  • The name lies, the content does not: use file.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man file