linux操作命令汇总

基础命令

  1. less/more 逐行或逐页查看文件信息内容
    more是一个基本的分页器,可以查看较长的文本文件
[root@hcss-ecs-72ce ~]# more /var/log/cron-20240630 
Jun 23 03:26:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[1573]: finished logrotate
Jun 23 03:26:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[1561]: starting man-db.cron
Jun 23 03:26:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[1584]: finished man-db.cron
Jun 23 03:26:01 hcss-ecs-72ce anacron[1418]: Job `cron.daily' terminated
...后退 1Jun 23 03:26:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[1573]: finished logrotate
Jun 23 03:26:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[1561]: starting man-db.cron
Jun 23 03:26:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[1584]: finished man-db.cron
Jun 23 03:26:01 hcss-ecs-72ce anacron[1418]: Job `cron.daily' terminated

less是一个更高级的分页器,比more功能更丰富允许向前跟向后浏览文件,在文件很大时更高效,它不会通过加载整个文件来翻页。

[root@hcss-ecs-72ce ~]# less /var/log/cron-20240630 
Jun 24 03:01:01 hcss-ecs-72ce anacron[8100]: Jobs will be executed sequentially
Jun 24 03:01:01 hcss-ecs-72ce run-parts(/etc/cron.hourly)[8102]: finished 0anacron
Jun 24 03:14:01 hcss-ecs-72ce anacron[8100]: Job `cron.daily' started
Jun 24 03:14:01 hcss-ecs-72ce run-parts(/etc/cron.daily)[8140]: starting 

在less中可以使用按键来进行导航

  • Enter,向下一行
  • y或t向上一行
  • b 上一页
  • /搜索词 向下搜索对应的搜索词
  • ?搜索词 向上搜索对应的搜索词
  • q 退出less,同样也可以退出more。

  1. whereis 查看命令所在路径以及命令相关手册所在路径
[root@hcss-72ce ~]# whereis pwd
pwd: /usr/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz
[root@hcss-72ce ~]# 
  1. yum下载并安装软件命令
  2. yum install -y 名称 直接安装软件,无需二次确认
  3. yum groupinstall -y 包组名称 直接安装软件包组,无需二次确认
  4. yum repolist 查看yum源信息
[root@hcss-72ce ~]# yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
源标识                源名称                                              状态
!base/7/x86_64        CentOS-7 - Base                                     10,072
!epel/x86_64          Extra Packages for Enterprise Linux 7 - x86_64      13,791
!extras/7/x86_64      CentOS-7 - Extras                                      526
!updates/7/x86_64     CentOS-7 - Updates                                   6,173
repolist: 30,562
[root@hcss-72ce ~]# 
  1. yum list查看已安装的软件&可安装的软件
[root@hcss-ecs-72ce ~]# yum list
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
已安装的软件包
GeoIP.x86_64                             1.5.0-14.el7                  @base    
NetworkManager.x86_64                    1:1.18.8-2.el7_9              @updates
zlib.x86_64                              1.2.7-21.el7_9                @updates 
可安装的软件包
0ad.x86_64                               0.0.22-1.el7                  epel     
0ad-data.noarch                          0.0.22-1.el7                  epel     
0install.x86_64                          2.11-1.el7                    epel     
2048-cli.x86_64                          0.9.1-1.el7                   epel     
  1. yum grouplist 查看哪些软件包组可以安装/已经安装的软件包组
  2. yum -h 显示命令的参数帮助信息
  3. yum provides “对应的命令” 查看对应的命令属于哪个软件大礼包

高级命令

一、find

在文件系统中搜索符合特定条件的文件和目录,命令有很多选项,可以根据文件名、类型、大小、权限、所有者、修改时间以及其它一些条件来搜索文件
🐯 按名称进行查找
💻 语法格式find /path/to/search(想进行搜索的目录路径) -name “filename”

[root@hcss-ecs-72ce ~]# find /home/paipai/ -name test
/home/paipai/test
[root@hcss-ecs-72ce ~]# find /  -name test
/home/paipai/test
/usr/bin/test
/usr/lib64/python2.7/unittest/test
/usr/lib64/python2.7/test
/usr/lib64/python3.6/test
/usr/lib/modules/3.10.0-862.el7.x86_64/kernel/drivers/ntb/test

📢:使用 -iname 参数可以忽略大小写进行搜索🔍


🐯按文件类型进行搜索🔍
💻 语法格式 find /查找范围的路径 -type f 默认把目标路径下的所有文件展示出来

[root@hcss-ecs-72ce ~]# find /home/ -type f
/home/H101/.bash_profile
/home/H101/.bashrc
/home/H101/.bash_logout
/home/home/test.sh
/home/H100/.bash_profile
/home/H100/.bashrc
/home/H100/.bash_logout
/home/paipai/.bash_profile
/home/paipai/.bashrc
/home/paipai/.bash_logout

💻 语法格式 find /查找范围的路径 -type d,默认把目标路径下的所有目录展示出来

[root@hcss-ecs-72ce ~]# find /home/ -type d 
/home/
/home/H101
/home/home
/home/H100
/home/paipai
/home/paipai/test
/home/paipai/ok
/home/oldboy
/home/Alex
[root@hcss-ecs-72ce ~]#

🐯基于修改时间进行搜索
可以根据文件最后的修改时间搜索文件,使用-mtime参数,后面跟上对应的天数(自然日)

  • 搜索X天前修改的文件,比如搜索/home 5天前修改过的文件
[root@hcss-ecs-72ce ~]# find /home -mtime +5
/home/H101
/home/H101/.bash_profile
/home/H101/.bashrc
/home/H101/.bash_logout
/home/paipai/.bash_logout
/home/oldboy
/home/oldboy/.bash_profile
/home/oldboy/.bashrc
  • 搜索过去X天内修改的文件,比如搜索/home 过去5天内修改过的文件
[root@hcss-ecs-72ce ~]# find /home -mtime -5
/home
/home/paipai
/home/paipai/test
/home/paipai/ok
/home/paipai/ok/practice
/home/paipai/.bash_history
[root@hcss-ecs-72ce ~]# 
  • 搜索正好在X天前修改的文件,比如搜索/home 过去第3天修改过的文件
[root@hcss-ecs-72ce ~]# find /home  -mtime  5
[root@hcss-ecs-72ce ~]# find /home  -mtime  4
[root@hcss-ecs-72ce ~]# find /home  -mtime  3
/home
/home/paipai
/home/paipai/test
/home/paipai/ok
/home/paipai/ok/practice
/home/paipai/.bash_history
[root@hcss-ecs-72ce ~]# 

🐯 基于大小进行搜索
使用参数-size基于文件大小进行搜索,find认可的大小单位有

单位内容
c字节byte
k千字节,KB,1KB=1024字节
M兆字节,MB,百万字节,1MB=1024KB
G吉字节,GB,十亿字节,1GB=1024MB
  1. 搜索大于XXX大小用+号
[root@hcss-ecs-72ce ~]# find /var/log -size +10M
/var/log/hostguard/hostguard.log.2
/var/log/hostguard/hostguard.log.5
/var/log/hostguard/hostguard.log.6
  1. 搜索小于XXX大小用 - 号
  2. 搜索具体大小前面不用加具体符号

🐯搜索并执行命令
-exec允许你对找到的文件执行命令

[root@hcss-ecs-72ce home]# echo hhhhhh>>/home/H100/test.txt
[root@hcss-ecs-72ce home]# ll /home/H100/test.txt 
-rw-r--r-- 1 root root 7 79 11:01 /home/H100/test.txt
[root@hcss-ecs-72ce home]# find /home/H100/  -name "*.txt" -exec rm -f {} \;
[root@hcss-ecs-72ce home]# ll /home/H100/test.txt 
ls: 无法访问/home/H100/test.txt: 没有那个文件或目录
[root@hcss-ecs-72ce home]# 

📢:如果通过find查找对应目录下我文件,不满足预设的条件,如果使用 rm -rf强制删除命令也会吧这个目录给直接删除

[root@hcss-ecs-72ce home]# echo hhhhhh>>/home/H100/test.txt
[root@hcss-ecs-72ce home]# find /home/H100 -size -10k -exec rm -f {} \;
rm: 无法删除"/home/H100": 是一个目录
[root@hcss-ecs-72ce home]# find /home/H100 -size +10k -exec rm -f {} \;
[root@hcss-ecs-72ce home]# ll /home/H100
总用量 0
[root@hcss-ecs-72ce home]# ls
Alex  H100  H101  home  oldboy
[root@hcss-ecs-72ce home]# ll /home/Alex/
总用量 0
[root@hcss-ecs-72ce home]# echo hhhhhh>>/home/Alex/test.txt
[root@hcss-ecs-72ce home]# ll /home/Alex/
总用量 4
-rw-r--r-- 1 root root 7 79 10:48 test.txt
[root@hcss-ecs-72ce home]# find /home/Alex/  -size -10k -exec rm -rf {} \;
find:/home/Alex/: 没有那个文件或目录
[root@hcss-ecs-72ce home]# ll /home/Alex/
ls: 无法访问/home/Alex/: 没有那个文件或目录
[root@hcss-ecs-72ce home]# cd /home
[root@hcss-ecs-72ce home]# ll
总用量 16
drwx------ 2 H100   H100   4096 416 22:41 H100
drwx------ 2 H101   H101   4096 416 22:45 H101
drwxr-xr-x 2 root   root   4096 624 23:13 home
drwx------ 2 oldboy oldboy 4096 526 13:37 oldboy
[root@hcss-ecs-72ce home]# 

🐯 组合条件搜索
可以使用多个组合条件来进行精确搜索,使用-and或是使用-or

[root@hcss-ecs-72ce home]# find /home/H100 -name "*.txt" -or -name "*.log"
/home/H100/test.log
/home/H100/test.txt
[root@hcss-ecs-72ce home]# 

📢:使用 -or 或者是 -and时,后面也需要加上参数对应的查询参数,否则会出现下面的报错。

[root@hcss-ecs-72ce home]# ll /home/H100
总用量 8
-rw-r--r-- 1 root root 7 79 11:44 test.log
-rw-r--r-- 1 root root 7 79 11:44 test.txt
[root@hcss-ecs-72ce home]# find /home -name "*.txt" -and "*.log"
find: 路径必须在表达式之前: *.log
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

🐯使用正则表达式进行搜索
正则表达式的使用方式这里不做过多描述,后面我会专门整理一篇帖子描述正则表达式具体内容~

二、查看实时更新日志

sudo tail -f 需要查看实时更新的日志路径

[root@hcss-ecs-72ce ~]# sudo tail -f /var/log/yum.log 
Jul 04 15:43:42 Installed: pytalloc-2.1.16-1.el7.x86_64
Jul 04 15:43:42 Installed: nettle-2.7.1-9.el7_9.x86_64
Jul 04 15:43:42 Installed: trousers-0.3.14-2.el7.x86_64
Jul 04 15:43:42 Installed: gnutls-3.3.29-9.el7_6.x86_64
Jul 04 15:43:42 Installed: libwbclient-4.10.16-25.el7_9.x86_64
Jul 04 15:43:43 Installed: samba-client-libs-4.10.16-25.el7_9.x86_64

在linux系统中,系统日志通常存放在 /var/log/xx目录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值