BusyBox 文件操作工具 cp mv rm mkdir ls 详解
核心工具一览
BusyBox 内置的文件操作 Applet 覆盖了绝大多数日常需求:
| 命令 | 用途 | GNU 版对比 |
|---|---|---|
| ls | 列出目录内容 | 没有 --time-style、--group-directories-first |
| cp | 复制文件和目录 | 没有 -t、-T、--no-preserve 的全部选项 |
| mv | 移动/重命名 | 基本一致 |
| rm | 删除文件和目录 | 没有 -d 的全部语义(BusyBox rm -d 只删空目录) |
| mkdir | 创建目录 | 没有 -m 和 -p 的组合灵活性 |
| rmdir | 删除空目录 | 基本一致 |
| cat | 连接文件 | 没有 -n 的全部控制(GNU cat 有 -A、-v、-E 等) |
| touch | 修改时间戳/创建空文件 | 没有 -d 的复杂日期解析 |
| find | 搜索文件 | 没有 -path、-regex 的完整正则 |
ls 常见差异
# BusyBox ls 的默认输出格式
ls -l /tmp
# GNU ls -l 会额外显示 group 名、时间更精确
# BusyBox 只显示 owner、size、date、name
# BusyBox ls 不支持 --time-style
# ls -l --time-style=long-iso 会报错
# 改用 BusyBox ls -l 即可
# 颜色输出
# BusyBox ls --color=auto 在大多数版本里支持
# 行为和 GNU 版一致
cp 的几个坑
# GNU cp 有 -t 参数指定目标目录在前
cp -t /dest file1 file2
# BusyBox cp 没有这个参数,等价写法:
cp file1 file2 /dest
# GNU cp -a 等价于 -dR --preserve=all
# BusyBox cp -a 行为一致
cp -a src_dir dest_dir # 可用
# GNU cp --parents
cp --parents /a/b/c dest/
# BusyBox cp 没有 --parents,需要手动 mkdir -p
mkdir -p dest/a/b && cp c dest/a/b/
find 替代方案
BusyBox find 的正则和 path 功能有限,复杂查找任务建议用 shell 循环:
# GNU find 的 -path:
find /var -path '*log*' -name '*.txt'
# BusyBox find 可能不支持 -path,用 shell 循环替代:
find /var -name '*.txt' | while read f; do case "$f" in *log*) echo "$f";; esac; done
# GNU find 的 -regex:
find /etc -regex '.*\.conf$'
# BusyBox find 可能不支持 -regex,用 -name 组合:
find /etc -name '*.conf'
实用建议
在 BusyBox 环境下写文件操作脚本时,最稳妥的方式是:
- 先用
busybox <command> --help查看具体支持哪些选项 - 在目标设备上实际跑一遍,观察输出格式
- 如果脚本需要跨平台(主机 GNU + 目标 BusyBox),用 POSIX sh 兼容写法
- 复杂 find/mount 操作,考虑在主机上生成文件列表后传到目标设备处理