macOS查看文件内容常用的命令小结

3,330 阅读2分钟

前言

最近苹果的热更新和私有库问题检查的更严了,如果应用被拒,苹果在拒绝信中会说到使用strings/otool/nm命令进行自查,所以总结了一些命令,方便查找和定位文件内容相关信息。

1、grep

  • 作用:判断是否包含字符串

  • 使用示例:

    grep -r "xxx” path

检查是否包含(weixin)字符串:

grep -r
matches 表示包含。

2、strings

  • 作用:find the printable strings in a object, or other binary, file

  • 使用示例:

    strings a.out | grep hello //检查 a.out 文件中含有 hello 字符串的字节序列

  • 检查二进制是否含有关键词的库(比如检查私有库):

    strings a.out | grep hello.png

  • 更多使用说明 man strings

3、otool

  • 作用: object file displaying tool. (针对目标文件的展示工具,用来发现应用中使用到了哪些系统库,调用了其中哪些方法,使用了库中哪些对象及属性)

  • 使用示例: otool -L path //查看可执行程序都链接了那些库

    otool -L path | grep "xxx" //筛选是否链接了xxx库

    otool -D path //查看支持的架构

    otool -ov path //output the Objective-C class structures and their defined methods.(输出Object-C类结构以及定义的方法)

  • 查看该应用是否砸壳: otool -l path | grep crypt //cryptid 0(砸壳) 1(未砸壳)

    otool -l path | grep crypt.png

  • 更多用法 man otool

4、nm

  • 作用:display name list (symbol table). (显示符号表)

  • 使用示例: nm path //得到Mach-O中的程序符号表

    nm -nm path//目标文件的所有符号

    nm -nm path.png

    符号表中标示为 undefined,意思是目标文件引用了类_XXX(XXX库),不过这并没有实现它。

  • 更多用法 man nm

5、file

  • 作用:determine file type. (判断文件类型)

  • 使用示例: file path

    判断.a/framework是静态库还是动态库: 静态库:

    file path - static Lib.png
    动态库:
    file path - dynamically Lib.png

6、lipo

  • 作用:create or operate on universal files(创建或处理通用文件的工具)

  • 使用示例: lipo -info 'file path' // 判断静态库所支持的平台 - i386 armv7 armv7s x86_64 arm64

    lipo -remove i386 origin_xxx.a -output op_xxx.a // 删除静态库包括的i386平台

    lipo -thin i386 origin_xxx.a -output op_xxx.a // 拆分静态库,只保留i386 CPU架构

    lipo -create device_xxx.a simulator_xxx.a -output universal_xxx.a //对真机或者模拟器分别打出 .a 文件合并

参考阅读


> 注:本文首发于 [iHTCboy's blog](http://ihtcboy.com),如若转载,请注明来源。