实用小命令
一、实用小命令
(1)测试文件 [[email?protected] ~]# cat F1 123 456 789 123123 (2)-n :查看文本时显示行号 [[email?protected] ~]# cat -n F1 1 123 2 456 3 789 4 123123 5 6 (3)-b :查看文本时显示行号,有空白的行不计其内 [[email?protected] ~]# cat -b F1 1 123 2 456 3 789 4 123123 (4)-E :显示是否换行,结尾以"$"结尾表示有换行 [[email?protected] ~]# cat -E F1 123$ 456$ 789 $ 123123 $ $ $ (5) -A :显示TAB是否有加键,一个"^I"表示一个TAB [[email?protected] ~]# cat -A F1 123$ 456$ 789 $ 123123^I^I $ $ $ (6)-s :压缩相邻的空行为一个 [[email?protected] ~]# cat -s F1 123 456 789 123123 (7)生成文件 [[email?protected] ~]# cat > F2 test ctrl + d退出 [[email?protected] ~]# cat F2 test (9)合并文件 [[email?protected] ~]# cat F1 F2 > F3 [[email?protected] ~]# cat F3 123 456 789 123123 test
(1)将文本倒过来查看 [[email?protected] ~]# tac F1 123123 789 456 123
(1)反向显示内容 [[email?protected] ~]# echo "abcd" | rev dcba [[email?protected] ~]# rev < /etc/fstab # batsf/cte/ # 9102 24:54:90 13 naJ uhT no adnocana yb detaerC # # ‘ksid/ved/‘ rednu deniatniam era,ecnerefer yb,smetsyselif elbisseccA # ofni erom rof )8(diklb ro/dna )8(tnuom,)8(sfdnif,)5(batsf segap nam eeS # # 0 0 stluafed sfx / toor-sotnec/reppam/ved/ 0 0 stluafed sfx toob/ 5fe0eaa3ec3f-6eb9-5234-0aee-21b8c81d=DIUU 0 0 stluafed paws paws paws-sotnec/reppam/ved/
(1)-# :#表示数字,表示显示前几#行 [[email?protected] ~]# head -5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin (2)-n # :#表示数字,表示显示前几#行,跟上面-#类似 [[email?protected] ~]# head -n 5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin (3)-c # :#表示数字,表示显示前几个字节的数据 [[email?protected] ~]# head -c 5 /etc/passwd root:
(1)-# :显示倒数后#行 [[email?protected] ~]# tail -5 /etc/passwd gitlab-prometheus:x:993:990::/var/opt/gitlab/prometheus:/bin/sh jenkins:x:992:989:Jenkins Automation Server:/var/lib/jenkins:/bin/false ntp:x:38:38::/etc/ntp:/sbin/nologin nginx:x:991:988:Nginx web server:/var/lib/nginx:/sbin/nologin mysql:x:990:987::/home/mysql:/sbin/nologin (2)-n # :跟-#一样,显示倒数后#行 [[email?protected] ~]# tail -n 5 /etc/passwd gitlab-prometheus:x:993:990::/var/opt/gitlab/prometheus:/bin/sh jenkins:x:992:989:Jenkins Automation Server:/var/lib/jenkins:/bin/false ntp:x:38:38::/etc/ntp:/sbin/nologin nginx:x:991:988:Nginx web server:/var/lib/nginx:/sbin/nologin mysql:x:990:987::/home/mysql:/sbin/nologin (3)-c # :显示倒数后#字节 [[email?protected] ~]# tail -c 10 /etc/passwd n/nologin (4) -f :实时显示最后一行,默认情况会打印后10行并且监控最后一行,当增加一行机会实时显示数据出来 [[email?protected] ~]# tail -f F1 123 456 789 123123 [[email?protected] ~]# echo "testline" >> F1 [[email?protected] ~]# tail -f F1 123 456 789 123123 testline 实时监测只显示最后一行 [[email?protected] ~]# tail -n 0 -f /etc/fstab tesst (5) -F :实时显示最后一行,当文件不存在则显示文件不存在 [[email?protected] ~]# tail -F F1 123 456 789 123123 testline [[email?protected] ~]# rm -f F1 [[email?protected] ~]# tail -F F1 123 456 789 123123 testline tail: ‘F1’ has become inaccessible: No such file or directory
[[email?protected] ~]# tailf /var/log/nginx/access.log
(1)测试文件 [[email?protected] ~]# cat > F1 123 34545 asdasdas 123 7878 ctrl + d退出 [[email?protected] ~]# cat F1 123 34545 asdasdas 123 7878 (2)删除文件内容,删除带有"123"字眼的内容 [[email?protected] ~]# tr -d "123" < F1 4545 asdasdas 7878 (3)将小写字母替换成大写字母,标准输出生成F3文件 [[email?protected] ~]# tr "a-z" "A-Z" < F1 > F3 [[email?protected] ~]# cat F3 123 34545 ASDASDAS 123 7878 (4)将多列空白压缩成一列 [[email?protected] ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 120527360 54045264 66482096 45% / devtmpfs 1919508 0 1919508 0% /dev tmpfs 1931784 0 1931784 0% /dev/shm tmpfs 1931784 11968 1919816 1% /run tmpfs 1931784 0 1931784 0% /sys/fs/cgroup /dev/sda1 1038336 145300 893036 14% /boot tmpfs 386360 0 386360 0% /run/user/0 [[email?protected] ~]# df -h | tr -s " " Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 115G 52G 64G 45% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 12M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda1 1014M 142M 873M 14% /boot tmpfs 378M 0 378M 0% /run/user/0
(1)以/etc/passwd为列子前5行为列子 [[email?protected] ~]# cat /etc/passwd | head -5 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin (2)取出第1列,用户名 [[email?protected] ~]# cut -d : -f 1 /etc/passwd | head -5 root bin daemon adm lp (3)取出第1和第3列 [[email?protected] ~]# cut -d : -f 1,3 /etc/passwd | head -5 root:0 bin:1 daemon:2 adm:3 lp:4 (4)取出第1列至第3列 [[email?protected] ~]# cut -d : -f 1-3 /etc/passwd | head -5 root:x:0 bin:x:1 daemon:x:2 adm:x:3 lp:x:4 (5)取出第1列至第3列,以|号为分割符输出 [[email?protected] ~]# cut --output-delimiter="|" -d : -f 1-3 /etc/passwd | head -5 root|x|0 bin|x|1 daemon|x|2 adm|x|3 lp|x|4
(1)大家如果用过ansible都知道,如果使用ansible批量发送SSH公钥需要在/etc/ansible/hosts中 定义每个主机的SSH用户和密码,一般在管理主机的时候我们都会将密码类的信息记录在xls中, 这时候就排上用场了(假如有100台主机,密码都不一样)将xls记录的IP列复制到F1文件,不排除有 windwos的"^M"回车,使用dos2unix清除,下面F2,F3文件也一样,当然ansible_ssh_user, ansible_ssh_pss不是xls记录内容,还需要先合并F1,F2这个我就不讲了,下面会了就明白了。 文件1 [[email?protected] ~]# cat F1 [host] 10.1.1.1 10.1.1.2 10.1.1.3 10.1.1.4 文件2 [[email?protected] ~]# cat F2 ansible_ssh_user=root ansible_ssh_user=root ansible_ssh_user=root ansible_ssh_user=root 文件3 [[email?protected] ~]# cat F3 ansible_ssh_pass=123 ansible_ssh_pass=456 ansible_ssh_pass=789 ansible_ssh_pass=910 合成文件,循序要F1,F2,F3 [[email?protected] ~]# paste F1 F2 F3 [host] 10.1.1.1 ansible_ssh_user=root ansible_ssh_pass=123 10.1.1.2 ansible_ssh_user=root ansible_ssh_pass=456 10.1.1.3 ansible_ssh_user=root ansible_ssh_pass=789 10.1.1.4 ansible_ssh_user=root ansible_ssh_pass=910 (2)加上-d选项的效果 [[email?protected] ~]# paste -d : F1 F2 F3 [host]:: 10.1.1.1:ansible_ssh_user=root:ansible_ssh_pass=123 10.1.1.2:ansible_ssh_user=root:ansible_ssh_pass=456 10.1.1.3:ansible_ssh_user=root:ansible_ssh_pass=789 10.1.1.4:ansible_ssh_user=root:ansible_ssh_pass=910 (3)-s :将每个文件的所有行合并成1行,第1个文件所有内容合并成第一行,第2个文件的所有内容合并成第二行 [[email?protected] ~]# paste -s F1 F2 F3 [host] 10.1.1.1 10.1.1.2 10.1.1.3 10.1.1.4 ansible_ssh_user=root ansible_ssh_user=root ansible_ssh_user=root ansible_ssh_user=root ansible_ssh_pass=123 ansible_ssh_pass=456 ansible_ssh_pass=789 ansible_ssh_pass=910
(1)以这个为列子 [[email?protected] ~]# cat F1 [host] 10.1.1.1 10.1.1.2 10.1.1.3 10.1.1.4 (2)默认不加任何选项表示为,5行,5个单词,43个字节数 [[email?protected] ~]# wc F1 5 5 43 F1 (3)-l :统计多少行 [[email?protected] ~]# cat F1 | wc -l 5 (4)-w :统计多少个单词 [[email?protected] ~]# cat F1 | wc -w 5 (5)-c :统计多少个字节 [[email?protected] ~]# cat F1 | wc -c 43 (6)-m :统计多少个字符 [[email?protected] ~]# cat F1 | wc -m 43 -L :显示文件中最长行的长度 [[email?protected] ~]# cat F1 | wc -L 8
(1)以/etc/passwd为列子前5行为列子 [[email?protected] ~]# cat /etc/passwd | head -5 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin (2)默认情况不加任何选项以每行第一个字符排序,adm,bin,daemon,lp,root [[email?protected] ~]# cat /etc/passwd | head -5 | sort adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin root:x:0:0:root:/root:/bin/bash (3)指定以第3列UID排序,并且以数字排序,升序 [[email?protected] ~]# cat /etc/passwd | head -5 | sort -t : -k 3 -n root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin (4)-r 倒排 [[email?protected] ~]# cat /etc/passwd | head -5 | sort -t : -k 3 -n -r lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin root:x:0:0:root:/root:/bin/bash (5)去掉重复的行 [[email?protected] ~]# cat F1 11 11 2 3 4 5 22 22 [[email?protected] ~]# cat F1 | sort -u 11 2 22 3 4 5
(1)以这个为例子 [[email?protected] ~]# cat F1 11 11 2 3 4 5 22 22 (2)-c :对连续重复以及不连续重复的行统计 [[email?protected] ~]# cat F1 | uniq -c 2 11 1 2 1 3 1 4 1 5 2 22 (3) -u :只显示不连续重复的行 [[email?protected] ~]# cat F1 | uniq -u 2 3 4 5 (4)-d :只显示相邻并且连续重复的行 [[email?protected] ~]# cat F1 | uniq -d 11 22
(1)生成随机数取前30个字节 base64 openssl rand -base64 30 | head -c 30 16进制 openssl rand -hex 30 | head -c 30 随机生成30个数字 tr -dc ‘[[:digit:]]‘ < /dev/urandom | head -c 30 随机生成30个小写字母 tr -dc ‘[[:lower:]]‘ < /dev/urandom | head -c 30 随机生成30个大写字母 tr -dc ‘[[:upper:]]‘ < /dev/urandom | head -c 30 二、简单实操一下
(1)以这个nginx日志文件为例子 [[email?protected] ~]# cat access.log | head -1 192.168.126.111 - - [20/Feb/2019:15:35:16 +0800] "GET / HTTP/1.1" 200 1482 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/72.0.3626.109 Safari/537.36" (2)先取出第1例,然后对他进行排序,排序完就去重复,然后统计 [[email?protected] ~]# cat access.log | cut -d " " -f 1 | sort -n | sort -u | wc -l 4 [[email?protected] ~]# cat access.log | cut -d " " -f 1 | sort -n | uniq | wc -l 4
(1)以这个nginx日志文件为例子 [[email?protected] ~]# cat access.log | head -1 192.168.126.111 - - [20/Feb/2019:15:35:16 +0800] "GET / HTTP/1.1" 200 1482 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/72.0.3626.109 Safari/537.36" (2)首先取出URI第7列,然后按字符排序,然后uniq -c对重复或者不重复的进行统计,然后升序排序,找出页面访问最大的URI [[email?protected] ~]# cat access.log | cut -d " " -f 7 | sort | uniq -c | sort -r -n 60 /favicon.ico 3 /zabbix 3 /lnmp.gif 3 / 1 /zabbix/api_jsonrpc.php 一步一步来!!!!!!!!!!!!!!!!!!!!!!其实很简单哒!!!!!!!!!!!!!!!!!!!!! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |