日常开发和调试中,用好 Linux 下的各种工具能事半功倍。本文记录了除基础命令外,一些非常实用但又容易被忽略的工具和技巧。
系统调试 查看内核/设备信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 objdump -dS vmlinux > /tmp/kernel.s objdump -t vmlinux nm -n char.ko ld char.ko --print-map cat /proc/iomemcat /sys/kernel/debug/memblock/reservedcat /proc/mtdzgrep -i debugfs /proc/config.gz dmesg -w
设备与驱动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 udevadm info /dev/video33 find /sys/ -name video4linux ls -l /sys/class/video4linuxlsusb -t lsof nmtui route -n
网络工具 网络延时模拟 使用 tc 命令模拟网络延迟,便于测试网络应用的健壮性:
1 2 3 4 5 6 7 8 sudo tc qdisc add dev eth0 root netem delay 100ms ping 8.8.8.8 sudo tc qdisc del dev eth0 root
其他网络命令 1 2 3 4 5 6 7 8 9 10 11 echo "1.1.1.1 lee1.com lee2.com" >> /etc/hostsping lee1.com echo > /dev/tcp/<server_ip>/<port>nslookup telnet ethtool netstat
文件操作进阶 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 find Git/ -type f -exec sed -i 's/oldstring/newstring/g' {} \; find <source > -iname '*.md' -exec mv '{}' <dest> \; find <source > -name '*' -exec file {} \; \ | grep -o -P '^.+: \w+ image' \ | cut -d':' -f1 \ | xargs -I{} cp -v {} <dest> grep pattern files | xargs realpath find . -type f | xargs dirname | sort -u
实用系统工具 脚本对话框 - whiptail 1 2 3 4 5 6 7 8 #!/bin/bash whiptail --yesno "would you like to continue?" 10 40 RESULT=$? if [ $RESULT = 0 ]; then echo "you clicked yes" else echo "you clicked no" fi
文件安全删除 - shred
文件分割 - split 1 2 split --lines 2 prefix_
行号显示 - nl 1 2 3 4 cat <file> | nl ls | nl -s ":" -w 1 echo -e "one\ntwo\nthree" | nl less -N <file>
文件锁 - flock 防止脚本重复执行:
1 2 3 4 5 6 7 8 9 10 11 LOCKFILE=/tmp/lockfile already_locked () { echo "lock is already held, exiting" exit 1 } exec 200>$LOCKFILE flock -n 200 || already_locked echo "lock obtained, proceeding" sleep 10echo "releasing lock, done"
按名称杀进程 - killall 1 killall -I <Application>
历史命令编辑 - fc
修改文件时间戳 - touch 1 2 touch -t 2104250101 <file> touch -r <file1> <file2>
进程与日志 Android 调试 1 2 3 4 5 adb logcat -c adb logcat -G 256M adb logcat -v threadtime -b main -b crash -b kernel 1 \ | tee log -$(date "+%Y%m%d-%H%M%S" ).log
进程管理 1 2 3 4 5 6 7 8 9 10 11 12 13 nohup foo | cat &( cd "${foo} " ) pushd "${foo} " popd
Tmux 终端复用 1 2 3 4 5 6 7 8 9 10 11 12 13 echo 'set -g default-terminal "screen-256color"' >> ~/.tmux.conftmux a -dt <session> bind -n C-S-Left swap-window -t -1bind -n C-S-Right swap-window -t +1
小乐趣
实用 Shell 函数 日常工作中积累的一些自定义函数,写入 ~/.bashrc 或 ~/.zshrc 即可使用。
带时间戳的日志输出 1 2 3 4 5 6 7 8 9 10 11 12 13 function msg () { if [ "$1 " = "-h" ]; then cat << EOF Usages of msg: $ msg "log info..." $ msg "hello" "world" EOF else echo "MSG:$(date) : $*" fi }
在脚本中追踪执行进度时非常实用,尤其是需要和日志文件一起分析时。
批量修改文件扩展名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function batch_extensions () { if [ "$1 " = "-h" ]; then cat << 'EOF' Usages of batch_extensions: $1 : The path where you want to change the extensions. $2 : The extension what you want to change to. Example: batch_extensions ~/test sh EOF else for file in "$1 " /*; do mv "$file " "$file .$2 " done fi }
注意 :原版使用 `ls $1/*`,这里改成了 "$1"/* 避免文件名含空格或特殊字符时出错。
参考