Linux常用命令
快捷键
在命令行下已经输入一串命令下的快捷操作:
ctrl+c: 中断输入,另起一行ctrl+u: 删除光标以前的字符ctrl+k: 删除光标后以后的字符ctrl+a: 移动光标至头部ctrl+e: 移动光标至尾部ctrl+l: 清屏ctrl+r: 搜索历史命令
简单命令
init 0或poweroff: 关机cd -: 进入上次访问目录init 6或reboot: 重启passwd username: 修改username密码,比如修改root密码,passwd rootfind /home -name file: 在/home目录及子目录查找文件file,可以包含通配符*.find /home -name xxxx -exec cp {} abc/ \;: 将找到的文件拷贝到abc目录ln -sf abc.sh /usr/bin/abc: 建立软链接,-s表示软链接,-f表示forcediff file1 file2: 比较2个文件diff folder1 folder2: 比较2个文件夹cmp file1 file2: 比较二进制,-s安静模式ssh-copy-id -i id_rsa.pub username@192.168.1.1: 添加ssh认证scp -r username@192.168.1.1:/home/test .: 将远程目录拷贝到本地,-r代表目录script: 保存终端所有输入输出到当前目录的typescript,退出用exitps -aux: 查看所有进程kill <pid>: 杀掉指定进程minicom -D /dev/ttyUSB0 -b 115200 -c on -C $(date +%Y-%m-%d_%H:%M:%S).log: 开启usb串口,记录日志du -h -d 1 .: 查看当前目录的各个文件(夹)大小chown -R user:user *: 将当前目录以及子目录的用户都改为userreadlink file: 查看软链接file的路径last | grep 'shutdown\|reboot': 查看上一次重启时间tar -xzvf file.tar.gz: 解压tar.gztar –czf jpg.tar.gz *.jpg: 将所有的jpg图片压缩到文件jpg.tar.gz中python -m SimpleHTTPServer 8000: 创建简单的http服务器,http://localhost:8000打开ldd xxx.out: 查看程序的链接库路径xxd a.bin: 十六进制显示文件od -tf4 a.bin: 二进制文件,显示浮点值strip a.bin: 去掉可执行文件的符号,大幅减少文件大小;-g只去掉debug的符号nohup cmd > cmd.log 2>&1 &: 不挂断后台运行cmd,nohup cmd &默认输出到nohup.outshopt -s extglob: 开启扩展模式,支持?*+@!匹配,比如rm -rf !(except)file a.lib: 识别文件类型
复杂命令
# 重命名多个文件
for name in `ls *.3.2`; do mv $name ${name%.3.2}.3; done #将文件.3.2改名为.3
# rename
apt install rename
rename 's/\.0$//' *
# mmv
apt install mmv
mmv '*.3.2.0' '#1.3.2'
工具
bless abc.bin: 十六进制读写Gparted: 磁盘分区netron: 神经网络查看工具typora: markdown编辑工具
CPU信息
# insall
apt-get install cpufrequtils
# show info
cpufreq-info
# set cpu to performance
sudo cpufreq-set -g performance
grep
对指定目录或文件查找字符串片段是否存在,并显示该行内容
参数说明
-q: 安静模式,通用用于脚本中做判断-r: 目录及子目录所有文件和文件夹查找-n: 输出包含行数-l: 列出符合条件的对应的文件名,注意不是指只有文件名符合-o: 只列出匹配的部分,通常和正则表达式一起用-E: 查找内容为正则表达式,grep -E=egrep
举例
grep test a.cpp: 在a.cpp中找包含test的行,并显示出来grep -q hello test.txt: 如果在test.txt中找到hello,则返回truelsmod | grep -q "abc": 查看加载的模块是否包含abc名字grep -r test: 目录包括子目录,找文件名或文件内容包含testgit log -1|head -1|grep -E [0-9a-fA-F]{40} -o: 输出git目录的最新sha1编码