Linux基础命令之二

1.查看 CPU 信息
[root@web01 ~]# lscpu

 

2.查看日志,推荐使用head/tail,less/more,这种命令占用系统资源比较少.因为日志文件,通常比较大,有的日志达到几十G,通过cat,vi/vim进行查看,系统可能卡死,可能内存不足.
head -n num file
默认不加参数,显示10行,可以简写为 head -num file
[root@web01 ~]# head -n5 /etc/passwd
等于
[root@web01 ~]# head -5 /etc/passwd
tail一样的用法
tail -n num file
同时,tai可以查看文件末尾的实时更新.
tail -f /var/log/secure

less,按页显示文件内容
选项与快捷方式:
-N 显示行号
q,退出查看
空格/f 下一页
b 上一页
/ 搜索

G 最后一行
g 第一行
99g 第99行

more,按页显示文件内容,到达最后一行就退出,功能不如less多。

查看日志小结:
前后多少组合
head/tail
less/more
大文件避免使用cat/vi/vim查看

3.wc:would count,统计文件中单词情况,大小,行数,工作中常用于统计行数;
简单使用是统计文件的行数,未来还可以统计一些命令的结果有多少行(个);
选项:
-l:统计行数
基本用法:
[root@web01 ~]# wc /etc/services
11176 61033 670293 /etc/services
第一个数是多少行,第二个是单词数,第三个是文件大小
只看行数
[root@web01 ~]# wc -l /etc/services
11176 /etc/services
案例:统计系统中登录失败的次数
[root@web01 ~]# grep 'Failed password' /var/log/secure | wc -l
|,管道,把前边命令的输出,传递给后面的命令使用;
未来一般都是配合其他命令,可以取出xxx次数;
还可以放在脚本中进行判断;

4.which:查询命令位置
whereis:查询命令的位置
[root@web01 ~]# which grep
alias grep='grep --color=auto'
/usr/bin/grep
[root@web01 ~]# whereis grep
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz
[root@web01 ~]# which head tail less more wc
/usr/bin/head
/usr/bin/tail
/usr/bin/less
/usr/bin/more
/usr/bin/wc
一般来讲,可以来检测一下命令是否存在,不存在的命令,会有提示;

5.文件比较命令
diff/vimdiff
在服务的配置中,我们需要对比下新旧的配置文件,查看修改了哪些内容;这时候我们需要进行文件的对比操作,可以通过diff、vimdiff命令实现。
[root@web01 mclind]# diff 1.txt 2.txt
4c4
...
解读,a append 增加,c,替换,修改,d 删除,4c4就是第4行不一样

6.排序去重组合
sort/uniq
这个是重点,工作中经常会用,统计日志,日志分析,系统信息统计,必备命令;
sort:排序
选项:
-n,从小到大;
-r,逆序;
-k,指定某一列,根据某一列进行排序
-t,指定分隔符,只能指定单个字符;
uniq:去重;
示例:
[root@web01 mclind]# cat sort.txt
1
2
333
555
899
11
23
42
452
556
554
123
[root@web01 mclind]# sort sort.txt
1
11
123
2
23
333
42
452
554
555
556
899
可以看出,sort排序,默认按字符排序,而不是数字;
解决,sort -n
[root@web01 mclind]# sort -n sort.txt
1
2
11
23
42
123
333
452
554
555
556
899
[root@web01 mclind]# cat sort.txt
1 li
2 a
333 b
555 c
899 d
[root@web01 mclind]# sort -nr -k1 sort.txt
899 d
555 c
333 b
2 a
1 li
综合示例:
[root@web01 ~]# ll /etc/ | sort -rnk5 | head
[root@web01 mclind]# sort -rnk3 -t ':' passwd
多列排序
[root@web01 mclind]# sort -rn -k2 -k4 fileName
//解释,先根据第2列排序,如果第2列有一样的,再根据第4列排序
案例:如何以.为分隔符,按第3列和第4列进行排序?
192.168.0.2 xxxxx(问题中,第4列之后是空格)
sort -t '.' -n -k3,3 -k4,4 filename
指定分隔符,多列排序的时候,容易出现排序失误。
这时候需要我们手动告诉sort,排序范围;
-k3,3 表示仅对第3列排序
-k4,4 表示仅对第4列排序

uniq:unique独一无二,相邻两行,有一样的,合并,所以,需要和sort合并使用;
选项:
-c:去重并显示次数(重复次数)
[root@web01 mclind]# cat 1.txt
mclind
mclind
mclind
mclind
mclind
mclind
mclind
chen
chen
chen
chen
mclind
mclind
mclind
[root@web01 mclind]# uniq -c 1.txt
7 mclind
4 chen
3 mclind
[root@web01 mclind]# sort 1.txt | uniq -c
4 chen
10 mclind
sort secure-ip.txt | uniq -c | sort -rn | head
[root@web01 mclind]# sort secure-ip.txt | uniq -c | sort -rn | head

补充:和windows上传下载命令
yum install -y lrzsz
rz上传到linux中
sz从linux下载到windows中
wc -l secure-ip.txt

7.日期组合
在linux中我们需要日常查看系统时间,保证整个网站所有服务器的系统时间是一致的;
在日常操作中,写脚本的时候也需要使用时间,比如取出系统时间,比如 创建以当前日期命名的文件,目录,备份;
date:设置或查看系统的日期,时间;一般主要用来查看日期或取日期;
[root@web01 mclind]# date
ntpdate:自动同步时间
特殊符号

date
[root@web01 mclind]# date +%F
2022-12-07
[root@web01 mclind]# date +%Y
2022
[root@web01 mclind]# date +%m
12
[root@web01 mclind]# date +%d
07
[root@web01 mclind]# date +%Y-%m-%d
2022-12-07
[root@web01 mclind]# date +%T
20:37:38 时分秒
[root@web01 mclind]# date +%H:%M:%S
20:38:05
[root@web01 mclind]# date +%w
3 小写w,代表周几
[root@web01 mclind]# date -d '20221111 11:11:11'
[root@web01 mclind]# date -d '1day'
2022年 12月 08日 星期四 20:43:18 CST
[root@web01 mclind]# date -d '-1day'
2022年 12月 06日 星期二 20:43:28 CST
[root@web01 mclind]# date -s '20221111 11:11:11' //手动修改时间

ntpdate:同步时间
[root@web01 mclind]# yum -y install ntpdate
[root@web01 mclind]# ntpdate ntp1.aliyun.com
7 Dec 20:48:42 ntpdate[107191]: step time server 120.25.115.20 offset -2.404655 sec
注:offset xxxx sec代表成功
[root@web01 mclind]# timedatectl set-timezone Asia/Shanghai //更改时区为上海
[root@web01 mclind]# timedatectl status //查看时区
应用实战
date应用场景:企业备份时候,给压缩包加上时间
创建文件或目录,给文件名或目录名字上加个时间
特殊符号``,反引号里面的命令会被优先执行。
[root@web01 mclind]# touch test-backup-`date +%F`.txt
[root@web01 mclind]# ls
test-backup-2022-12-07.txt

 

8,blkid:查看磁盘的UUID

 

9,dd:转化或复制文件

dd if=/dev/zero of=/tmp/1g bs=1M count=1024

if : input file

of : output file

bs : block size

count : 数量

 

posted @ 2022-12-17 16:20  mclind  阅读(53)  评论(0编辑  收藏  举报