常用指令
shutdown -h 20 #20分钟之后关机shutdown -r 60 #60分钟之后重启type cd #查看内部命令hostnamectl #查看主机信息hostnamectl set-hostname 主机名 #设置主机名df -T #查看光盘挂载到哪个目录了touch file{1..5}.txt #同时创建多个文件wc -wlc linux.txt #统计一个文件的总行数、总单词数以及总字节数du -sh ./.tldr/ #:查看文件或目录(会递归显示子目录)占用磁盘空间大小 systemctl start|stop|restart networksystemctl status sshd #查看网络状态systemctl reload 系统服务名称systemctl enable|disable 系统服务的名称 #开机启动date +"%F %T %Y %m %d %H %M %S" #格式化时间
压缩 解压
tar -cvf abc.tar a.txt b.txt c.txt #把a.txt、b.txt、c.txt文件打包到abc.tar文件中
tar -cvf wechat.tar wechat #把wechat文件夹进行打包wechat.ta
tar -tf abc.tar #查看abc.tar包中的文件信息
tar -uf abc.tar d.txt #向abc.tar包中添加一个d.txt文件
tar -xf abc.tar #解压
tar -zcf abc.tar.gz a.txt b.txt c.txt #把a.txt、b.txt、c.txt文件打包并压缩为abc.tar.gz
tar -zcf wechat.tar.gz wechat #把wechat文件夹压缩为wechat.tar.gz格式的压缩包
tar -zxf 名称.tar.gz # *.tar.gz格式的压缩包
tar -jxf 名称.tar.bz2 # *.tar.bz2格式的压缩包
tar -Jxf 名称.tar.xz # .tar.xz格式的压缩包
tar -xvf #解压.tar.gz文件
unzip wechat.zip -d /usr/local/nginx/ # 把wechat.zip解压到/usr/local/nginx目录下
find
find /var -name "boot.log" -type f #搜索/var目录中boot.log文件(普通文件)
find / -name "ssh" -type d #全盘搜索ssh目录
find /var/log -name "*.log" -type f #搜索/var/log目录下的所有的以".log"结尾的文件信息
find /var/log -name "*.log" -mtime +10 |xargs rm -rf #删除Linux系统中/var/log目录下10天以前的日志信息(日志文件格式*.log结尾)
find /var/log -name "*.log" -mtime +10 -exec rm -rf {} \;
find ./ -type f -size 5M #搜索当前目录下大小为5M的文件信息
find ./ -type f -size -5M #搜索/root目录下大小为5M以内的文件信息(5M>size>=0)
find / -type f -size +100M #搜索/目录中,文件大小大于100M的文件信息(size>100M)
grep
grep network initial-setup-ks.cfg #在initial-setup-ks.cfg文件中搜索包含关键词"network"的行
grep -n network initial-setup-ks.cfg #在initial-setup-ks.cfg文件中搜索包含关键词"network"的行,然后显示行号信息
grep 要搜索的关键词 多个文件的名称
grep network /var/log/* #搜索/var/log目录下所有文件,找到包含关键词"network"的所有行信息
grep -i root passwd 忽略大小写匹配包含root的行
grep -w ftp passwd 精确匹配ftp单词
grep -w hello passwd 精确匹配hello单词;自己添加包含hello的行到文件
grep -wo ftp passwd 打印匹配到的关键字ftp
grep -n root passwd 打印匹配到root关键字的行好
grep -ni root passwd 忽略大小写匹配统计包含关键字root的行
grep -nic root passwd 忽略大小写匹配统计包含关键字root的行数
grep -i ^root passwd 忽略大小写匹配以root开头的行
grep bash$ passwd 匹配以bash结尾的行
grep -n ^$ passwd 匹配空行并打印行号
grep ^# /etc/vsftpd/vsftpd.conf 匹配以#号开头的行
grep -v ^# /etc/vsftpd/vsftpd.conf 匹配不以#号开头的行
grep -A 5 mail passwd 匹配包含mail关键字及其后5行
grep -B 5 mail passwd 匹配包含mail关键字及其前5行
grep -C 5 mail passwd 匹配包含mail关键字及其前后5行
echo
echo "hello world" > readme.txt #把echo输出的"hello world"写入到readme.txt文件中
echo "hello linux" >> readme.txt #把echo输出的"hello linux"写入到readme.txt,要求不能覆盖原来的内容
管道
ls / | grep y #获取/根目录下包含关键字"y"的文件信息
rpm -qa | grep mariadb #-q :query,查询 -a :all,所有
ps -ef | grep apt #的进程中进行查找,查找与apt进程信息
cat initial-setup-ks.cfg | less
ls / | wc -l #统计/根目录下一共有多少个文件
cat /etc/passwd | wc -l #用户在计算机中有一个配置文件(/etc/passwd),一般情况下,一个用户会占用一行配置,请你使用现学的管道统计当前计算机中一共有多少个用户个信息(一个用户一行)
find /etc -name "*.conf" | xargs ls -l
netstat -tnlp |grep httpd #查询Web Server(httpd)服务的端口信息
ss -naltp |grep sshd #查询sshd服务的端口信息
kill
ps -ef |grep crond #7120
kill 7102
killall crond #使用killall命令杀死crond进程
scp
# scp [选项] 用户名@linux主机地址:资源路径 linux本地文件路径
选项说明:
-r :代表递归操作,主要针对文件夹
scp root@192.168.80.129:/root/tt.txt ./ #下载文件
# scp -r root@10.1.1.17:/root/shop ./ # 下载文件夹
# scp [选项] linux本地文件路径 用户名@linux主机地址:远程路径
选项说明:
-r :递归操作
scp /root/video.mp4 root@10.1.1.17:/root/ #上传文件
scp -r /root/shop root@10.1.1.17:/root/ #上传文件夹
cut
cut -d: -f1 1.txt 以:冒号分割,截取第1列内容
cut -d: -f1,6,7 1.txt 以:冒号分割,截取第1,6,7列内容
cut -c4 1.txt 截取文件中每行第4个字符
cut -c1-4 1.txt 截取文件中每行的1-4个字符
cut -c4-10 1.txt 截取文件中每行的4-10个字符
cut -c5- 1.txt 从第5个字符开始截取后面所有字符
grep-cut
runlevel |cut -c3
runlevel | cut -d ' ' -f2 #-d: 自定义分隔符,默认为制表符\t
grep -v '^#' /etc/inittab | cut -d: -f2
grep '^id' /etc/inittab |cut -d: -f2
grep "initdefault:$" /etc/inittab | cut -c4
grep -v ^# /etc/inittab |cut -c4 #-c: 以字符为单位进行分割,截取
grep 'id:' /etc/inittab |cut -d: -f2
cut -d':' -f2 /etc/inittab |grep -v ^#
cut -c4 /etc/inittab |tail -1
cut -d: -f2 /etc/inittab |tail -1 #-f: 与-d一起使用,指定截取哪个区域
sort
-u :去除重复行
-r :降序排列,默认是升序
-o : 将排序结果输出到文件中,类似重定向符号>
-n :以数字排序,默认是按字符排序
-t :分隔符
-k :第N列
-b :忽略前导空格。
-R :随机排序,每次运行的结果均不同
# sort -n -t: -k3 1.txt 按照用户的uid进行升序排列
# sort -nr -t: -k3 1.txt 按照用户的uid进行降序排列
# sort -n 2.txt 按照数字排序
# sort -nu 2.txt 按照数字排序并且去重
# sort -nr 2.txt
# sort -nru 2.txt
# sort -nru 2.txt
# sort -n 2.txt -o 3.txt 按照数字排序并将结果重定向到文件
# sort -R 2.txt
# sort -u 2.txt
uniq
uniq用于去除重复的行
常见选项:
-i: 忽略大小写
-c: 统计重复行次数
-d:只显示重复行
举例说明:
# uniq 2.txt
# uniq -d 2.txt
# uniq -dc 2.txt
tee 从标准输入读取并写入到标准输出和文件,即:双向覆盖重定向(屏幕输出|文本输入)
选项:
-a 双向追加重定向
# echo hello world
# echo hello world|tee file1
# cat file1
# echo 999|tee -a file1
# cat file1