1. 程式人生 > 實用技巧 >linxu檔案查詢、字元處理、字元擷取、字元替換、統計命令

linxu檔案查詢、字元處理、字元擷取、字元替換、統計命令

練習題

第一題
[root@wzh ~]# grep 'nologin$' /etc/passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
第二題
[root@wzh ~]# ifconfig|grep -w '10.0.0.200'|cut -d ' ' -f 10
10.0.0.200
第三題
[root@wzh ~]# cat /etc/sysconfig/selinux|sed 's#SELINUX=enforcing#SELINUX=disabled#g'

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
第四題
[root@wzh ~]# cat web.log|cut -d '/' -f3|sort|uniq -c
      1 mp3.driverzeng.com
      2 post.driverzeng.com
      3 www.driverzeng.com
第五題
[wzh@wzh root]$ su wzh
Password: 
su: Authentication failure
[wzh@wzh root]$ ^C
[wzh@wzh root]$ 
[wzh@wzh root]$ 
[wzh@wzh root]$ whoami
wzh
第六題
wzh@wzh ~]$ touch song{1..6}.mp3
[wzh@wzh ~]$ ll
total 0
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song1.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song2.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song3.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song4.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song5.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song6.mp3
第七題
[wzh@wzh ~]$ mv song{1..6}.mp3 Music
第八題
[wzh@wzh ~]$ mkdir friends family workl
[wzh@wzh ~]$ ll
第九題
[wzh@wzh friends]$ cp ../Music/song{1..3}.mp3 .
[wzh@wzh friends]$ ll
total 0
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:37 song1.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:37 song2.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:37 song3.mp3
第十題
[wzh@wzh family]$ mv ../Music/song{4,5,6}.mp3 .
[wzh@wzh family]$ ll
total 0
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song4.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song5.mp3
-rw-rw-r--. 1 wzh wzh 0 Mar 24 02:06 song6.mp3
十一題
cd
十二題
[wzh@wzh ~]$ rm -rf family
十三題
[wzh@wzh friends]$ rm -rf ./*
[wzh@wzh friends]$ ll
total 0
十四題
[wzh@wzh ~]$ rm -rf friends

檔案查詢命令

# 查詢⽂件或⽬錄locate
yum install -y mlocate
updatedb
[root@localhost opt]# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
命令查詢
[root@wzh wzh]# which cat
/usr/bin/cat

$():執⾏命令的結果
``:執⾏命令的結果
type -a 
[root@wzh wzh]# type -a grep
grep is aliased to `grep --color=auto'
grep is /usr/bin/grep
[root@wzh wzh]# whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz 命令路徑查詢

字元處理命令

sort
排序,預設以首字母順序排序
-t:指定分隔符
-r:倒敘排序,從大到小
-n:以數字大小排序,從小到大
-k:指定按哪一列排序
/etc/passwd檔案指定:分隔符以數值大小進行倒敘排序
[root@wzh wzh]# cat /etc/passwd|sort -t ':' -rnk3
wsh1:x:1002:1002::/home/wsh1:/bin/bash
wzh1:x:1001:1001::/home/wzh1:/bin/bash
wzh:x:1000:1000::/home/wzh:/bin/bash
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
----
以冒號為分隔符取第三列從小到大排序
[root@wzh wzh]# cat /etc/passwd|sort -t ':' -nk3
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
把相同的數字放在一起
[root@wzh wzh]# cat 123.txt|sort
123
123
456
456789
456789
789
uniq 去重命令還可以統計重複幾次
[root@wzh wzh]# cat 123.txt|sort|uniq
123
456
456789
789
統計重複次數
[root@wzh wzh]# cat 123.txt|sort|uniq -c
      2 123
      1 456
      2 456789
      1 789

字元擷取命令

cut
-d:指定分隔符
-f:指定區域
-c:按照字元擷取
以冒號為分隔符取第三列
[root@wzh wzh]# cat /etc/passwd|cut -d ':' -f3
以冒號為分隔符取第三,五,六列
[root@wzh wzh]# cat /etc/passwd|cut -d ':' -f3,5,6
取字元
[root@wzh wzh]# cat /etc/passwd|cut -c 2,3,4,5,6,7,8,9
oot:x:0:
in:x:1:1
aemon:x:
dm:x:3:4
p:x:4:7:
ync:x:5:
hutdown:
alt:x:7:
ail:x:8:

字元替換命令

把2替換成空格
[root@wzh wzh]# cat 123.txt|tr '2' ' '
1 3
456789
1 3

統計命令

-l:統計行數
-c:統計字元
-w:統計單詞
統計/etc/services檔案行數
[root@wzh wzh]# less /etc/services|wc -l
11176
統計/etc/services檔案單詞
[root@wzh wzh]# less /etc/services|wc -w
61033
統計/etc/services檔案有多少字元
[root@wzh wzh]# less /etc/services|wc -c
670293