目录与文件管理
# 复制文件
cp <src> <dest> # dest可以是目录或者文件,为文件时相当于复制并重命名
# 复制目录dir1本身及内部文件目录到dir2目录中
cp -r dir1 dir2
# 复制dir1中的文件和目录到dir2目录中,dir1目录本身并不复制
cp -r dir1/ dir2
# 删除目录
rm -r dir
# 显示文件大小
stat file
du -h file
# 显示当前目录大小
du -sh
# 显示当前目录下所有文件的权限、所属用户、大小、修改时间等详细信息
# 第一位: d =>目录,l =>软连接,- =>普通文件
ls -lh
# 显示当前目录下的所有子目录
ls -d */
# 显示目录下匹配文件的详细信息
ls -l /usr/bin/python*
# 显示文件前n行(默认10行)
head -n <num> file
# 显示文件后n行
tail -n <num> file
# 动态显示文件尾行
tailf file
tail -f -n 10 file
# 打开文件并跳到第10行
vim +10 file
# 打印文件内容(添加行号)
cat -n file
# 创建文件夹并且创建路径上所有不存在的目录
mkdir -p path
# 查找可执行文件位置(通过PATH环境变量中的路径查找)
which <command>
# 查找程序位置(不只是可执行文件)
where <program>
# 判断一个命令是否存在
command -v <command>
# 排序文件内容(只输出,不该改变原文件)
sort file
# 显示删除重复的行后的文件内容(不改变原文件,只对相邻行有效); 一般配合sort使用
uniq file
# 显示排序且去重后的输出
sort file | uniq
# 显示重复行和重复次数
sort file | uniq -c
# 移动目录或文件; 不需要-r
mv dir/file path
# 统计行数
wc -l file
# 统计单词数
wc -w file
# 统计字符数
wc -c file
# 建立连接
ln path/to/file path/to/link
ln -s path/to/file path/to/link # 软连接; 如果链接文件和源文件不在同一个目录中,则两者都必须使用绝对路径
# 去除连接
unlink <path/to/link>
# base64加密
echo xxx | base64
base64 <file>
# base64解密
echo xxx | base64 -d
# md5校验
echo xxx |md5sum
md5sum <file>
# 格式化json输出
jq
网络
# 查看IP所属信息
whois <ip>
# 查看URL对应IP
nslookup
# 查看该端口被什么程序占用
sudo lsof -i :<port>
# 普通用户下可能无法通过lsof看到root用户进程的端口占用信息
# 但这不代表该端口没有被占用,应该用netstat查看
# -p参数显示占用进程名字,可能和lsof一样没有权限显示
netstat -lnp | grep <port>
# 查看网卡信息(例如IP地址)
ifconfig
ip addr ls
# 查看路由表
netstat -rn
# 查看域名的DNS解析过程
dig [+short] <域名>
# 使用指定DNS服务器解析域名
dig @<8.8.8.8> <域名>
# 查看主机名
hostname
# 启动web server
python3 -m http.server <port>
# 等价于curl
wget -q -O-
系统
# 查看n条历史命令
history -n
# 执行编号n的历史命令
!n
# 以sudo权限执行上一条命令
sudo !!
# 查看帮助文档
man command
# 查看命令常用操作(too long don't read的缩写)
# 安装:npm install -g tldr
tldr command
# xargs 不能操作文件名有空格的文件
# 将系统中所有jpd文件压缩打包
find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
# 下载文件中列出的所有url对应的页面
cat url-list.txt | xargs wget –c
# 如果命令执行出错,错误输出写入空文件
# 一般用于shell脚本,例如创建文件,如果文件已经存在会报错,但是其实没关系
<command> 2>/dev/null
# 设置http(s)代理
export http_proxy=http://127.0.0.1:xxxx
export https_proxy=https://127.0.0.1:xxxx
# 清空所有环节变量并执行命令
env -i <command>
# 查看CPU核心数
lscpu
cat /proc/cpuinfo | grep 'processor' | wc -l
# 查看内核名称,版本号,处理器等信息
uname -a
# 查看OS版本
cat /etc/os-release
# 查看当前用户
whoami
# 查看当前登陆到了系统(远程登陆时用)
who
# 切换到用户
su <name>
# 切换到root
sudo su
# 关机
shutdown -h now
#重启
shutdown -r now
程序与进程
# 强制结束进程
kill -9 pid
# 强制结束所有同名进程
killall -9 name
# 查看进程信息(一般与grep同用)
ps aux
ps -ef
# 使用bash执行command
bash -c "command"
# 执行Python命令
python -c "command"
# 执行Python脚本
python -i <script.py>
压缩与解压
tar [-C 路径】[主选项 + 辅选项】 文件或目录
-
-C
参数可以指定要解压\压缩到的路径 -
主选项
- 只能选其一
- -c 创建压缩文件
- -x 解压
- -t 查看压缩文件中的内容
- r 追加文件
- -u 更新文件内容
-
r,u参数只能作用域tar文件,即压缩前的文件
-
辅选项
- -z 以gzip格式打包或解压。 打包后格式一般为 xxx.tar.gz 或者 xxx.tar.tgz
- -j 以bzip2格式打包或解压。 打包后格式一般为 xxx.tar.bz2
- -f 使用文档名
- 必加, 并且一定是最后一个参数
- -v 显示打包过程
# 只打包
tar -cvf file.tar file
# 打包压缩
tar -cvzf file.tar.gz file
# 解压并拆包到\usr\local目录
tar -C \usr\local -xvzf file.tar.gz
# 解压zip包
unzip file.zip
# 查看(压缩)包
tar -tvf file.tar.gz
后台程序
- 后台执行的进程默认输出依旧是屏幕, 如果不想看到输出需要重定向
- 运行后台进程前会输出作业号,也可通过
jobs
查看job号 ctrl-z
暂停时也会输出job号,这可以用来指定重启的作业
# 查看当前终端在后台的进程任务(并显示PID)
jobs -l
# 只列出运行(停止)的作业
jobs -r(-s)
# 重启暂停的任务并在后台运行(ctrl-z暂停)
bg
# 重启作业号为2的暂停程序
bg 2
# 将后台任务重新恢复到前台运行
fg
优先级
nice
可以设置进程优先级(运行前)-20
最高优先级,19
最低优先级- 默认优先级为
0
;普通用户只能调低优先级(>0),不能调高优先级
renice
可以调整已经运行的进程的优先级- 只能操作属于你的进程
- 普通用户只能调低优先级,root或者sudo可以随意调整
# 设定启动时的优先级
nice -n 10 ./run.sh
# 调整优先级
renice -n 10 <pid>
包管理
- CentOS、RedHat 使⽤用 yum 包管理理器器,软件安装包格式为
rpm
- Debian、Ubuntu 使⽤用 apt 包管理理器器,软件安装包格式为
deb
- 软件仓库(索引)储存在
/etc/apt/sources.list
文件中
- 软件仓库(索引)储存在
- 如果遇到包依赖等问题,可以使用
aptitude
代替apt
- 需要先安装:
sudo apt install aptitude
- 需要先安装:
# ubuntu为apt,mac为brew,centos为yum
# 更新索引(只检查,不更新)
apt update
# 更新所有包
apt upgrade
# 下载时跳过确认
apt install -y <pkg>
# 列出所有安装的包
apt list
# 列出其依赖
apt depends <pkg>
# 下载包的源代码
apt source <pkg>
# 删除包
apt remove <pkg>
# 模拟删除包;用于查看卸载过程会删除哪些内容
apt remove -s <pkg>
# 纯净删除;包括删除配置文件
apt remove --purge <pkg>
# 删除不再被任何包依赖的包
apt autoremove
# 备份源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 替换国内镜像源
sudo vim /etc/apt/sources.list
# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
输入
ctrl+a
将光标移至输入行头ctrl+e
将光标移至输入行末ctrl+u
删除光标所在位置到行首ctrl+k
删除光标所在位置到行末ctrl+w
向前删除一个单词ctrl+y
取消删除
-
ctrl+c
强制结束 -
ctrl+s
暂停任务 -
ctrl+q
恢复任务 -
ctrl+z
把当前进程调入后台并停止运行 -
在命令行尾部使用
&
运行后台任务 -
使用
&&
在一行输入多个命令