温馨提示×

如何批量用linux strings处理

小樊
47
2026-10-17 03:58:20
栏目: 智能运维

在 Linux 下,strings 常用于从二进制文件中提取可打印字符串。如果你要批量处理多个文件,通常结合 find、xargs 或 shell 循环即可。

下面按常见场景给你几种做法。


一、批量处理一个目录下的所有文件

方法 1:使用 find + -exec

find /path/to/dir -type f -exec strings {} \;

如果你只想处理某类文件(如 .bin):

find /path/to/dir -type f -name "*.bin" -exec strings {} \;

方法 2:使用 find + xargs(更高效)

find /path/to/dir -type f -print0 | xargs -0 strings

处理特定后缀:

find /path/to/dir -type f -name "*.so" -print0 | xargs -0 strings

二、批量处理并把结果分别保存到文件

每个文件对应一个 strings 输出

for f in /path/to/dir/*; do
    [ -f "$f" ] && strings "$f" > "$f.txt"
done

例如:

a.bin → a.bin.txt
b.bin → b.bin.txt

三、批量处理并合并成一个结果文件

find /path/to/dir -type f -exec strings {} \; > all_strings.txt

或:

cat /path/to/dir/* | strings > all_strings.txt

(仅适用于普通文件,不推荐用于二进制混合)


四、加上文件名前缀(推荐分析用)

find /path/to/dir -type f | while read f; do
    echo "===== $f ====="
    strings "$f"
done > result.txt

五、常用 strings 参数(批量时也常用)

strings -n 6 file        # 最少 6 个字符
strings -e l file        # 16-bit little endian
strings -e B file        # 32-bit big endian

批量示例:

find . -type f -print0 | xargs -0 strings -n 6

六、处理大量文件时的注意事项

  • 用 -print0 | xargs -0 避免文件名空格问题
  • 大文件可加 -n 减少噪音
  • 可用 file 先过滤二进制文件:
find . -type f -exec sh -c 'file "$1" | grep -q ELF && strings "$1"' _ {} \;

如果你能说清楚:

  • 是 目录 / 文件列表 / 压缩包
  • 是否要 保留文件名
  • 是否用于 逆向 / 日志 / 安全分析

我可以给你更精确的脚本。

0 踩