簡體   English   中英

如何在目錄中查找二進制文件?

[英]How to find binary files in a directory?

我需要在目錄中找到二進制文件。 我想用文件來做這個,然后我會用 grep 檢查結果。 但我的問題是我不知道什么是二進制文件。 什么會給二進制文件的文件命令,或者我應該用 grep 檢查什么?

這將查找所有非基於文本的二進制文件和空文件。

編輯

只有grep解決方案(來自 Mehrdad 的評論):

grep -rIL .

原答案

除了findgrep這不需要任何其他工具:

find . -type f -exec grep -IL . "{}" \;

-I告訴 grep 假設二進制文件不匹配

-L只打印不匹配的文件

. 匹配其他任何東西


編輯 2

這將查找所有非空二進制文件:

find . -type f ! -size 0 -exec grep -IL . "{}" \;

只需要提及Perl對文本文件的-T測試,以及對二進制文件的相反-B測試。

$ find . -type f | perl -lne 'print if -B'

將打印出它看到的任何二進制文件。 如果您想要相反的內容,請使用-T :文本文件。

它並非完全萬無一失,因為它只顯示前 1,000 個字符左右,但它比此處建議的一些特別方法要好。 有關整個綱要,請參閱man perlfunc 這是一個總結:

“-T”和“-B”開關的工作方式如下。 檢查文件的第一個塊左右,看它是否是包含非 ASCII 字符的有效 UTF-8。 如果,那么它是一個“-T”文件。 否則,將檢查文件的同一部分是否有奇數字符,例如奇怪的控制代碼或設置了高位的字符。 如果超過三分之一的字符是奇怪的,那就是“-B”文件; 否則它是一個“-T”文件。 此外,任何在檢查部分包含零字節的文件都被視為二進制文件。

我對這個問題的第一個答案在這里使用find命令幾乎是內聯的。 我認為您的講師希望使用file命令讓您了解magic numbers的概念,該命令將它們分解為多種類型。

就我而言,它很簡單:

file * | grep executable

但它可以通過多種方式完成。

由於這是一項任務,如果我給你完整的解決方案,你可能會討厭我;-) 所以這里有一點提示:

grep命令默認會輸出一個二進制文件列表,如果你搜索像. 這將匹配任何非空文件:

grep . *

輸出:

[...]
Binary file c matches
Binary file e matches

您可以使用awk僅獲取文件名,使用ls打印權限。 請參閱相應的手冊頁( man grepman awkman ls )。

在這些現代(畢竟2020 年實際上是 21 世紀的第 3 個十年),我認為正確的問題是如何找到所有非 utf-8 文件 UTF-8 是現代文本文件的等價物。

具有非 ascii 代碼點的文本的 utf-8 編碼將引入非 ascii 字節(即設置了最高有效位的字節)。 現在,並非所有此類字節的序列都形成有效的 utf-8 序列。

moreutils包中的isutf8正是您所需要的。

$ isutf8 -l /bin/*
/bin/[
/bin/acyclic
/bin/addr2line
/bin/animate
/bin/applydeltarpm
/bin/apropos
⋮

快速檢查:

$ file $(isutf8 -l /bin/*)
/bin/[:             ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=4d70c2142fc672d8a69d033ecb6693ec15b1e6fb, for GNU/Linux 3.2.0, stripped
/bin/acyclic:       ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d428ea52eb0e8aaf7faf30914710d8fbabe6ca28, for GNU/Linux 3.2.0, stripped
/bin/addr2line:     ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=797f42bc4f8fb754a49b816b82d6b40804626567, for GNU/Linux 3.2.0, stripped
/bin/animate:       ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=36ab46e69c1bfea433382ffc9bbd9708365dac2b, for GNU/Linux 3.2.0, stripped
/bin/applydeltarpm: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=a1fddcbeec9266e698782596f2dfd1b4f3e0b974, for GNU/Linux 3.2.0, stripped
/bin/apropos:       symbolic link to whatis
⋮

您可能希望反轉測試並獲取所有文本文件。 使用-i

$ isutf8 -il /bin/*
/bin/alias
/bin/bashbug
/bin/bashbug-64
/bin/bg
⋮
$ file -L $(isutf8 -il /bin/*)
/bin/alias:      a /usr/bin/sh script, ASCII text executable
/bin/bashbug:    a /usr/bin/sh - script, ASCII text executable, with very long lines
/bin/bashbug-64: a /usr/bin/sh - script, ASCII text executable, with very long lines
/bin/bg:         a /usr/bin/sh script, ASCII text executable
⋮

是的,它讀取整個文件,但速度非常快,如果你想要准確性......

我需要在目錄中找到二進制文件。 我想使用文件執行此操作,然后我將使用grep檢查結果。 但是我的問題是我不知道什么是二進制文件。 什么將給二進制文件file命令,或者我應該使用grep檢查什么?

我認為確定文件性質的最佳工具是文件實用程序。 在我的一個目錄中,我只有一個文件被 nautilus 文件管理器識別為二進制文件。 僅對於此文件,命令ls | xargs 文件返回“數據”而沒有任何進一步的信息。

linux中二進制文件的格式為ELF

當您對二進制文件運行file命令時,輸出包含單詞ELF 你可以grep這個。

在命令行上:

file <binary_file_name>

所以,如果你想在一個目錄中找到二進制文件(例如在 linux 中),你可以這樣做:

ls | xargs file | grep ELF

您可以使用find和參數-executable這基本上就是您想要的。

手冊頁說:

   -executable
          Matches files which are executable and directories which are searchable (in a file name resolution sense).  This takes into  account  access control lists and other permissions artefacts which the -perm test ignores.  This test makes use of the access(2) system call, and so can be fooled by NFS servers which do UID mapping (or root-squashing), since many systems implement access(2) in the client's kernel and so  cannot make  use  of  the  UID mapping information held on the server.  Because this test is based only on the result of the access(2) system call, there is no guarantee that a file for which this test succeeds can actually be executed.

這是你想要的結果:

# find /bin  -executable -type f | grep 'dmesg'
/bin/dmesg

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM