Linux常用命令汇总

dahlin
16
2020-04-01

Linux 常用命令汇总

我汇总了一些linux系统上经常用到的命令,并举例了一些命令的常用参数及用法。个人感觉已经相当全面了,足以满足日常开发和测试工作。

1. 目录命令

类别 命令 备注
pwd pwd 返回当前工作目录
pwd pwd -p 显示链接的真实路径
cd cd … 返回上一级目录
cd cd - 返回前一个目录
cd cd 切换至家目录
cd cd /usr/share 切换路径
ls ls -a 显示所有
ls ls -d 显示目录本身信息
ls ls -h 人性化显示容量
ls ls -l 长格式显示文档详细信息
ls ls -u 显示文件最后被访问的时间
ls ls -t 按修改时间排序
lsattr lsattr anaconda-ks.cfg 查看文件的隐藏属性
chattr chattr +a anaconda-ks.cfg 添加隐藏属性,a只能追加数据不能删除,i无法更改和删除

2. 文件命令

类别 命令 备注
file file /root 查看文件的类型
touch touch hello.txt 创建文件或更新文件所有时间
mkdir mkdir -p /tmp/test 创建多级目录
cp cp -r /var/log/ /tmp/ 递归复制目录与文件
cp cp -a /etc/passwd /tmp 复制时保留源文件的所有属性
rm rm -rf /tmp 强制递归删除文件目录
rm rm -i /tmp/abc.txt 删除前提示是否删除
mv mv a.txt b.txt 文件重命名
mv mv a.doc /tmp/ 将文件移动至tmp目录下
ln ln -s /test/hello.txt /tmp/hi.txt 创建文件的软连接
ln ln /test/hello.txt /test/hi.txt 硬链接,删除源文件后可以继续使用
du du -a 查看所有目录及文件的容量信息
du du -h 人性化显示容量
du du -s 仅显示总容量
du du -sh /root 查看/root所占磁盘空间的总和

3. 查找文件

类别 命令 备注
find find -name hello.doc 按名称查找文件
find find /tmp “*.log” 查找/tmp目录下所有名称以.log结尾的
find find / -empty 查找空文档
find find / -group tom 查找所属组为tom的文档
find find / -mtime -3 查找三天以内被修改过的文档
find find / -mtime +4 查找四天前被修改过的文档
find find ./ -size +10M 查找当前目录下大于10M的文档
find find ./ -type f 查找普通文件f,文件d目录, b,c设备,l连接
find find / -user tom 查找tom所拥有者的所有文档
find find ./ -size +1M -exec ls -l {} ;
find find / -size +1M -a -type f 参数-a并且-o或者
find updatedb 更新文件列表数据库
find locate httpd.conf 查找文件httpd.conf
find which passwd 查找文件路径
find whereis passwd 查找文件路径

4. 字符命令

类别 命令 备注
cat cat -b /etc/passwd 显示行号,空白行不显示行号
cat cat -n /etc/passwd 显示行号,包括空白行
more more /etc/passwd 分页查看内容,空格下一页,q键退出
less less /etc/passwd 分页查看内容,空格下一页,方向键回翻,q键退出
head head -c 2K /etc/passwd 查看文件的前2K内容
head head -20 /etc/passwd 查看文件的前20行
tail tail -c 2K /etc.passwd 查看文件末尾2K的内容
tail tail -20 /root/install 查看文件末尾的20行内容
tail tail -f /var/log/messages 实时动态查看文件内容
wc wc -c /root/install.log 显示文件的字节信息
wc wc -l /root/intall.log 显示文件行数
wc wc -w /root/install.log 显示文件的单词个数
grep grep th test.txt 过滤包含th的行
grep grep --color th test.txt 对匹配的关键词显示颜色
grep grep -i the test.txt 不区分大小写
grep grep -w num test.txt 过滤单词num
grep grep -v the test.txt 过滤不包含the的关键词
grep grep -n ‘name’ helo.txt 打印包含行号
grep grep -c ‘name’ tom.txt 计算包含name的行数
grep cat hello.txt grep -vi ‘name’
echo echo “Hello The Word” 直接输出,默认换行
echo echo -n “Hello World” 不打印换行符
echo echo -e “\n” 打印转义字符
echo echo -e “\” 输出转义字符
echo echo -e “hello\c” 不换行
echo echo -e “hello \fthe world” \f表单格式,\t水平tab键,\v垂直tab键
echo echo -e "\003[32mok\003[0m \033[字体颜色m字符串\033[0m30黑,31红,32绿,33黄,34蓝,35紫,36深绿,37白
echo cat sort.txt | sort -r 默认按每行第一个字符排序,-r反向排序
echo cat sort.txt | sort -t “:” -k 2 -n 指定分隔符,指定按第2列排序,以数字方式排序
echo cat uniq.txt | sort uniq -c
echo cat /etc/passwd | cut -f1,6-7 -d’:’ 同时打印出用户及家目录和登录shell
echo cat /etc/passwd | cut -c1-5,7-10 打印出每行第1-5个字符,以及7-10个字符
echo cat /etc/passwd | tr ‘[a-z]’ ‘[A-Z]’ 小写字母转换成大写字母
echo cat /etc/passwd | tr -d ‘:’ 删除文本中的冒号
echo paste -d: a.txt b.txt 将两个文本按照行合并,设置冒号为分割符,默认是tab键
echo split -l 500 big.txt small_ 分割为每500行一个小文件
echo split -b 64m big small 分割成没64m一个小文件

5. 压缩命令

类别 命令 备注
tar tar -czvf etc.tar.gz /etc/ 将etc目录打包
tar tar -cjvf etc.tar.bz2 /etc/ 目录打包
tar tar --delete etc/hosts -f etc.tar 从打包文件中删除文件host
tar tar -f etc.tar -r /root/install.log 追加文件至打包文档
tar tar -tvf etc.tar 查看压缩包详细信息
tar tar -xzvf boot.tar.gz -C /emp 指定解压路径解压
zip zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件.
zip unzip -o -d /home/sunny myfile.zip -o:不提示的情况下覆盖文件;-d:-d /home/sunny 指明将文件解压缩到/home/sunny目录下
zip zip -d myfile.zip smart.txt 删除压缩文件中smart.txt文件
zip zip -m myfile.zip ./rpm_info.txt 向压缩文件中myfile.zip中添加rpm_info.txt文件

6. 文档属性

类别 命令 备注
chmod chmod u=rwx,g=rwx,o=rwx install.log 改变文件或目录权限
chmod chmod u+rwx somefile r=4,w=2,x=1
chmod chmod a=rw install.log 设置所有人的权限为可读可写权限
chmod chmod g-x,o-wx install.log 减去相应权限
chmod chmod 700 install.log
chmod chmod 755 /home
chmod chmod --reference=install.log 1.txt 以install.log为标准修改1.txt的权限
chmod / -R chmod -R 754 somefile 递归将权限应用于所有的子目录与子文件
chmod chmod u+s somefile 添加SUID权限
chmod chmod g+x somefile 添加SGID权限
chmod chmod o+t somedir 给目录设置Sticky权限
chgrp chgrp -R john somdir 递归更改文件的拥有组
umask umask 022
umask umask 002
chown chown user2:mail install 修改文件的所有者为user2,所属组为mail
chown chown :root instll 仅修改文件的所述组为root
chown chown root install 今修改文件所属者为root
chown getfacl install.log 查看acl访问控制权限
chown setfacl -m u:user1:rw test.txt 添加用户user1对test.txt文件的可读写权限
chown setfacl -m g:user1:r test.txt 使user1组对test.txt文件可读
chown setfacl -x g:user1 test.txt 删除组user1的acl条目
chown setfacl -b test.txt 删除所有附加的ACL条目

7. 软件安装

类别 命令 备注
yum yum search ifconfig
yum yum install net-tools.x86_64
yum yum clean all && yum makecache 清空YUM缓存,刷新缓存
yum yum install dialog 交互式安装软件包
yum yum -y expect 非交互式安装
yum yum update 检查所有的软件并更新
yum yum remove dialog 卸载软件包
yum yum list 已经安装的软件包使用@标记
yum yum grouplist 列出所有组包
yum yum groupinstall “GNOME Desktop” 安装图形组包
yum yum search web 查找web相关软件
yum yum history 查看历史记录
yum yum groupupdat “KDE”
yum yum update httpd 更新某个包
yum yum check-update 检查当前系统中需要更新的包
yum yum list installed 显示系统中已经安装过的包
yum yum info PAKAGE 显示某个包的信息
yum yum remove PACKAGE 删除某个包
yum yum -y install gcc pcre-devel zlib-devel 安装依赖

8. 账户操作

类别 命令 备注
useradd useradd Frank 创建普通账户Frank
useradd useradd -c administrator 账号全称,描述信息
useradd / -d /home/admin 设置家目录
useradd / -e 2013-12-24 设置失效日期
useradd / -g root 设置账户的基本组
useradd / -G bin,adm ,mail admin 设置附加组,用逗号隔开
useradd useradd -s /sbin/nologin 设置账户的登录shell,默认为bash
useradd / -M user2 不创建家目录
groupadd groupadd tome 创建tom组
groupadd groupadd -g 1000 设置组ID号
id id root 显示账户及组信息
passwd passwd 为当前账户设置新密码
passwd passwd tom 指定修改tom密码
passwd echo “wer087” passwd --stdin tom
passwd passwd -l tom 锁定账户tom
passwd passwd -u tom 解锁账户
passwd passwd -d tom 清空账户tom的密码
usermod usermod -d /home/tomcat tom 修改tom的家目录
usermod usermod -e 2017-10-01 tome 修改tom账户的失效日期
usermod usermod -g mail tom 修改账户的基本组为mail
usermod usermod -s /bin/bash user2 修改user2的登录shell为bash
usermod usermod -u 1001 tom 修改tom的UID为1001
userdel userdel tom 删除账户tom
userdel userdel -r tom 删除账户及相关及目录
groupdel groupdel jerry 删除组jerry
gpasswd gpasswd admin 设置组密码
gpasswd gpasswd -A mail admin 将mail账户设置为组admin的管理员
/etc/passwd;/etc/shadow;/etc/group;/etc/gshadow
id id,groups,who,users,w 查看用户信息
su su ;su - user1 切换用户信息
visudo visudo
finger finger ;finger user1 调查用户信息

9. 计划任务

类别 命令 备注
at at 23:11 回车+计划任务内容+……回车+Ctrl+D 小时:分钟 或者4pm+3 days
at at now + 30 minutes \ at>/sbin/shutdown -h now … 设置30分钟后自动关机
at atq 查看at的任务序列
at atrm 1 删除标号为1的任务
at at 12:00 2014-12-12
at at -l 查看计划任务
at at -c 1 查看编号为1的计划任务的具体内容
at at -d 1 删除编号为1的计划任务
crontab crontab -u user1 -l 查看指定计划任务的用户,查看计划任务
crontab crontab -e 编辑计划任务,-r删除计划,-I删除时需确认
crontab 分 时 日 月 周; /etc/cron.allow ;/etc/cron.deny

10. 系统命令

类别 命令 备注
uptime uptime 监控CPU使用情况
free free 监控-b,-k,-m内存量Byte,KB,MB
df df -hiT 监控磁盘使用情况
ip ip a s ip命令可以查看网卡接口信息
ip ip -s link show eno1677773 查看网卡流量信息
netstat netstat -nutlp \ -s显示各种协议的统计信息-an查看网络链接
ps ps -ef或者-ax或者-aux 全格式显示进程信息
top top -d 1 -p 1,2 刷新间隔,默认3秒,-p查看指定PID的进程信息
top f 显示更多字段;空格键选中字段;
top P 按cpu使用率排序;
top M 按照内存排序;
top T 按CPU使用时间排序
top N 以PID排序
top K :kill进程
top R 表示renice,
top 帮助模式
ps ps -ef / grep dhcp
pidof pidof dhcpd 寻找进程的PID
nice nice -n -10 ./job.sh 用比较高的优先级运行它 -20~19 普通用户 0~19
renice renice -10 -p 5555 对于已经启动的进程
kill kill -l 查看信号代码
kill kill 2877 ; -9 强杀进程 -15 正常退出
killall killall httpd 使用进程名字强杀进程
lsof lsof filename 显示打开文件的所有进程
lsof lsof -c string 显示进程打开的所有文件
lsof lsof -u username 显示所属于user进程打开的文件
lsof lsof +d /DIR/ 显示目录下被打开进程的文件

11. 网络命令

类别 命令 备注
ifconfig ifconfig eno1677 192.168.0.31 netmask 255.255.
ifconfig ifconfig eno16777736 查看网卡接口信息
ifdown ifdown eth0 关闭网卡
ifup ifup eth0 开启网卡
ifconfig ifconfig eno16777736 down /up 开关网卡
"/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.159.129
NETMASK=255.255.255.0
GATEWAY=192.168.1.1"
service network restart
route route add default gw 192.168.1.1 添加删除路由add或者del
route route -n 查看当前路由表
route /etc/hosts /etc/resolv.conf
route host www.baidu.com 查询DNS记录
route hostnamectl status 查看主机名称和主机信息
route hostnamectl set-hostname centos.example 设置主机名称
route ping 192.168.0.31 ping次数-c发送间隔-i指定超时时间-w
route traceroute -I www.baidu.com 跟踪数据包转发的过程
route nslookup www.baidu.com 检查本地设置的DNS服务器是否正常
route dig www.baidu.com MX 查看邮件记录
route dig baidu.com NS 查看域名服务器记录

12. 磁盘命令

类别 命令 备注
fdisk fdisk -l 查看系统设备磁盘分区
fdisk fdisk /dev/sdb 给设备分区
mkfs mkfs -t ext3 /dev/sdb1 格式化文件系统
mkfs mkfs.ext3 /dev/sdb1 格式化文件系统
fdisk mount 显示所有挂载
mount mount /dev/sdb1 newDisk 将设备挂载到目录下
umount umount /dev/sdb1
umount umount /root/newDisk 卸载设备
动物装饰