基礎練習:
1.将指令曆史寫入到root使用者家目錄的普通檔案exercise中。
[[email protected] ~]# history
1 2
2 5
3 3
4 6
5 4
6 history
[[email protected] ~]# history > exercise
[[email protected] ~]# cat exercise
1 2
2 5
3 3
4 6
5 4
6 history
7 history > exercise
[[email protected] ~]#
history : 指令曆史
[[email protected] ~]# history | tee exercise
1 2
2 5
3 3
4 6
5 4
6 history
7 history > exercise
8 cat exercise
9 rm -f exercise
10 ll
11 history | tee exercise
[[email protected] ~]#
管道符 | : 管道符左邊指令的輸出作為管道符右邊指令的輸入。
tee指令 : 在輸出到檔案的同時輸出到螢幕
2.截取目前日期的年月日将其寫入檔案tim.txt中?
[[email protected] ~]# date | cut -d ' ' -f 2,3,6 > time1.txt
[ro[email protected] ~]# cat time1.txt
Mar 20 2019
[[email protected] ~]#
————————————
[[email protected] ~]# date +%Y-%m-%d > time2.txt
[r[email protected] ~]# cat time2.txt
2019-03-20
[[email protected] ~]#
————————————
[[email protected] ~]# date +%y/%m/%d > time3.txt
[[email protected] ~]# cat time3.txt
19/03/20
[[email protected] ~]#
3.統計目前系統有多少使用者
[[email protected] ~]# wc -l /etc/passwd
44 /etc/passwd
[[email protected] ~]#
文本統計:wc
顯示檔案行數、單詞數、位元組數和檔案名:wc 檔案名
統計目前目錄下的檔案數:wc -l
隻顯示單詞數:wc -w 檔案名
隻顯示位元組數:wc -c 檔案名
隻顯示行數:wc -l 檔案名
隻顯示字元數:wc -m 檔案名
顯示最長的一行的字元數(不包括斷行符):wc -L 檔案名
注:在UTF-8編碼格式裡面,顯示時一個字元占一個位元組,一個中文字占用3個位元組
4.有一個檔案叫lianxi.txt,檔案内容為排序過的/etc/passwd内容的第三列和第三列的行數。
[[email protected] ~]# cut -d : -f 3 /etc/passwd | sort -n | tee lianxi.txt | wc -l >> lianxi.txt
[[email protected] ~]# cat lianxi.txt
0
1
2
3
4
5
6
7
8
11
12
14
27
29
32
38
42
59
70
72
74
75
81
89
99
107
113
170
171
172
173
989
990
991
992
993
994
995
996
997
998
999
1000
65534
44
[[email protected] ~]#
排序顯示(預設根據字元在ASCII碼中的升序排序):sort 檔案名
按照數值大小排序:sort -n 檔案名
排序并去掉重複的:sort -u 檔案名
逆序排序:sort -r 檔案名
進階練習:
1.在/home下建立普通檔案file1,在/下給它建立一個硬連結檔案為file1.bak,在root使用者的家目錄下給file1.bak檔案建立一個硬連結為file1.bak.bak,給file1.bak.bak建立一個軟連結為f1。
[[email protected] ~]# touch /home/file1
[[email protected] ~]# ln /home/file1 /file1.bak
[[email protected] ~]# ln /file1.bak file1.bak.bak
[[email protected] ~]# ln -s file1.bak.bak f1
[[email protected] ~]# ll /home
total 4
-rw-r--r--. 3 root root 0 Mar 20 23:50 file1
drwx------. 14 redhat redhat 4096 Mar 14 23:22 redhat
[[email protected] ~]# ll /
total 32
lrwxrwxrwx. 1 root root 7 Mar 6 05:26 bin -> usr/bin
dr-xr-xr-x. 3 root root 4096 Mar 14 23:21 boot
drwxr-xr-x. 19 root root 3240 Mar 20 23:49 dev
drwxr-xr-x. 138 root root 8192 Mar 21 2019 etc
-rw-r--r--. 3 root root 0 Mar 20 23:50 file1.bak
drwxr-xr-x. 3 root root 31 Mar 20 23:50 home
lrwxrwxrwx. 1 root root 7 Mar 6 05:26 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 6 05:26 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 May 25 2015 media
drwxr-xr-x. 2 root root 6 May 25 2015 mnt
drwxr-xr-x. 3 root root 15 Mar 6 05:33 opt
dr-xr-xr-x. 486 root root 0 Mar 21 2019 proc
dr-xr-x---. 13 root root 4096 Mar 20 23:51 root
drwxr-xr-x. 38 root root 1140 Mar 20 23:49 run
lrwxrwxrwx. 1 root root 8 Mar 6 05:26 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 25 2015 srv
dr-xr-xr-x. 13 root root 0 Mar 21 2019 sys
drwxrwxrwt. 25 root root 4096 Mar 20 23:50 tmp
drwxr-xr-x. 13 root root 4096 Mar 6 05:26 usr
drwxr-xr-x. 20 root root 4096 Mar 21 2019 var
[[email protected] ~]# ll
total 8
-rw-------. 1 root root 1632 Mar 6 05:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Desktop
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Documents
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Downloads
lrwxrwxrwx. 1 root root 13 Mar 20 23:51 f1 -> file1.bak.bak
-rw-r--r--. 3 root root 0 Mar 20 23:50 file1.bak.bak
-rw-------. 1 root root 1725 Mar 5 21:46 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Music
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Pictures
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Public
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Templates
drwxr-xr-x. 2 root root 6 Mar 14 23:23 Videos
[[email protected] ~]#
建立軟連結檔案 :ln -s 原檔案 連結檔案
軟連結記錄的是它所連接配接的檔案的路徑
————————————
建立硬連結檔案 : ln 原檔案 新檔案
硬連結記錄的是目标的inode
2.找出root使用者的家目錄下以f開頭的檔案。
[[email protected] ~]# ls f*
f1 file1.bak.bak
[[email protected] ~]#
3.寫出一個永久生效的指令别名為cha,該指令别名可以顯示出以上四個檔案的詳細屬性和inode号
[[email protected] ~]# vim ~/.bashrc
alias cha='ll -i /home/file1 /file1.bak /root/file1.bak.bak /root/f1'
ls -i 顯示檔案索引節點号(inode)一個索引節點代表一個檔案,在linux中儲存在磁盤分區中的檔案都給它配置設定一個編号,稱為索引節點号inode
Esc
ZZ
[[email protected] ~]# source ~/.bashrc
[[email protected] ~]# cha
19969981 -rw-r--r--. 3 root root 0 Mar 22 19:16 /file1.bak
19969981 -rw-r--r--. 3 root root 0 Mar 22 19:16 /home/file1
35700377 lrwxrwxrwx. 1 root root 13 Mar 20 23:51 /root/f1 -> file1.bak.bak
19969981 -rw-r--r--. 3 root root 0 Mar 22 19:16 /root/file1.bak.bak
[[email protected] ~]#
4.将/etc/passwd的内容添加到file1.bak檔案中,保留file1.bak檔案中前9行的内容,删除其他行的内容。
[[email protected] ~]# head -9 /etc/passwd >> /file1.bak
head /etc/passwd :預設檢視檔案前10行
head -9 /etc/passwd :指定檢視前9行
[[email protected] ~]# cat -n /file1.bak
cat :連接配接并顯示檔案内容到标準輸出
cat -n 檔案名 :顯示檔案内容并顯示行号
1 root:x :0:0:root:/root:/bin/bash
2 bin:x :1:1:bin:/bin:/sbin/nologin
3 daemon:x :2:2:daemon:/sbin:/sbin/nologin
4 adm:x :3:4:adm:/var/adm:/sbin/nologin
5 lp:x :4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x :5:0:sync:/sbin:/bin/sync
7 shutdown:x :6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x :7:0:halt:/sbin:/sbin/halt
9 mail:x :8:12:mail:/var/spool/mail:/sbin/nologin
[[email protected] ~]#
5.查找出file1.bak的路徑。
[[email protected] ~]# find / -name file1.bak
/file1.bak
[[email protected] ~]#
6.将file1.bak檔案的第一列和最後一列輸入到檔案/root/zuoye。
[[email protected] ~]# cat /file1.bak | cut -d : -f 1,7 >> /root/zuoye
[[email protected] ~]# cat /root/zuoye
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
mail:/sbin/nologin
cut -d 指定分隔符 -f 指定第幾列 被切割檔案
-f 1,3第一列和第三列
-f 1-3第一列到第三列
-c 1-4 指定第一到第四個字元
7.将file1.bak.bak檔案的第一行和最後一行輸入到檔案/test。
[[email protected] ~]# head -1 /root/file1.bak.bak >> /test && tail -1 /root/file1.bak.bak >> /test
[[email protected] ~]# cat /test
root:x :0:0:root:/root:/bin/bash
mail:x :8:12:mail:/var/spool/mail:/sbin/nologin
[[email protected] ~]#
預設檢視後10行:tail /etc/passwd
檢視後5行:tail -5 /etc/passwd
8.有一個檔案叫lianxi.txt,檔案内容為排序過的file1.bak内容的第三列和第三列的行數。
[[email protected] ~]# cut -d : -f 3 /file1.bak | sort -n | tee lianxi.txt | wc -l >> lianxi.txt
[[email protected] ~]# cat lianxi.txt
0
1
2
3
4
5
6
7
8
9
[[email protected] ~]#