在Linux中,任何設(shè)備都是文件,不僅如此,連數(shù)據(jù)通信的接口也有專門的文件負(fù)責(zé)。可以說(shuō),一切皆文件,目錄也是一種文件,是路徑映射。因此,文件系統(tǒng)是Linux的基礎(chǔ)。
一、文件與目錄管理命令
1、ls(list)--查看文件與目錄命令
在linux中,該命令可能是最多被執(zhí)行的,因?yàn)槲覀冸S時(shí)要知道文件或者目錄的相關(guān)信息。ls命令默認(rèn)顯示非隱藏文件的文件名(以文件名進(jìn)行排序)以及文件名代表的顏色。
命令格式:ls [選項(xiàng)] [目錄名]
常用參數(shù):
-l(long):列出長(zhǎng)字符串,包含文件的屬性與權(quán)限等數(shù)據(jù)。
-a(all):列出所有文件,包含隱藏文件
-d(directory):僅列出所指定目錄本身,而不列出目錄內(nèi)的文件數(shù)據(jù)
-R(Recursive):連同子目錄內(nèi)容一起列出來(lái),等于該目錄下所有文件都會(huì)顯示出來(lái)
-h(human-readable):將文件容量以人類易讀的方式(GB,KB)列出來(lái)
2、cp(copy)--文件或者目錄復(fù)制命令
命令格式:cp [options] source1 source2 source3...directory ——多個(gè)文件拷貝到一個(gè)目錄
或者:cp [options] <source file or directory> <target file or directory> ——單個(gè)文件或目錄的拷貝
如果是多個(gè)文件拷貝到一個(gè)目錄下,則該目錄必須存在。如果某個(gè)文件拷貝到某個(gè)目錄下,則會(huì)保持原來(lái)的名字
1
cp
/etc/
passwd
/tmp
也可以重命名,如
1
cp
/etc/
passwd
/tmp/test_passwd
則會(huì)重新命名為test_passwd,如果test_passwd文件已經(jīng)存在,則會(huì)默認(rèn)覆蓋該文件。
cp這個(gè)命令,不同身份者執(zhí)行這個(gè)命令會(huì)有不同的結(jié)果產(chǎn)生,尤其是-a -p參數(shù)。
常用參數(shù):
-i(interactive):若目標(biāo)文件已經(jīng)存在,則會(huì)在覆蓋時(shí)詢問(wèn)
-r(recursive):復(fù)制目錄及目錄下所有文件
-l(link):進(jìn)行硬鏈接的連接文件創(chuàng)建,而非復(fù)制文件本身
-s(symbolic link):復(fù)制為符號(hào)鏈接文件,即快捷方式文件
1
cp
~/.bashrc /tmp/
bashrc
2
cp
-l /tmp/bashrc /tmp/
bashrc_hlink
3
cp
-s /tmp/bashrc /tmp/bashrc_slink
? -p(preserve):連同文件屬性一塊復(fù)制過(guò)去,而非使用默認(rèn)屬性
在cp命令不加-p參數(shù)時(shí)候,源文件與目標(biāo)文件的權(quán)限是不同的,目的文件的所有者通常會(huì)是命令操作者本身。而且時(shí)間戳等也會(huì)改變。因此用于備份時(shí)候,可以用-p參數(shù),完整復(fù)制文件權(quán)限的參數(shù)。
3、rm(remove)——移除文件或者目錄
命令格式 :rm [-fir]文件或者目錄
該命令也可以用于對(duì)文件或者目錄重命名。如下面將目錄mvtest重命名為mvtest1,當(dāng)然也可以使用rename命令
hao@hao:/tmp$
mv
mvtest mvtest1
常用參數(shù) :
-f(force):忽略不存在的文件,不會(huì)出現(xiàn)警告信息。
-i(interactive):互動(dòng)模式,在刪除前會(huì)詢問(wèn)用戶是否操作
-r(recursive):遞歸刪除目錄及目錄下所有文件。
4、mv(move)——移動(dòng)文件與目錄、或者更名
命令格式: mv [options] source1 source2 source3...director
命令參數(shù):
-f(force):強(qiáng)制的意思,如果目標(biāo)文件已經(jīng)存在不會(huì)詢問(wèn)直接覆蓋
-i(interactive):互動(dòng)模式,在目標(biāo)文件已經(jīng)存在時(shí),詢問(wèn)是否覆蓋
-u(update):若目標(biāo)文件已經(jīng)存在,且source比較新,才會(huì)更新
1 hao@hao:/$ cd /tmp
2 hao@hao:/tmp$ cp ~/.bashrc bashrc
3 hao@hao:/tmp$ mkdir mvtest
4 hao@hao:/tmp$ mv bashrc mvtest
5 hao@hao:/tmp$ ll /tmp
6
total
36
7
drwxrwxrwt
8
root root
4096
Jul
24
16
:
54
./
8
drwxr-xr-x
23
root root
4096
Jul
21
20
:
45
../
9
drwxrwxrwt
2
root root
4096
Jul
24
16
:
20
.ICE-unix/
10
-r--r--r--
1
root root
11
Jul
24
16
:
20
.X0-
lock
11
drwxrwxrwt
2
root root
4096
Jul
24
16
:
20
.X11-unix/
12
drwxrwxrwt
2
lightdm lightdm
4096
Jul
24
16
:
20
at-spi2/
13
drwxrwxr-x
2
hao hao
4096
Jul
24
16
:
54
mvtest/
14
drwx------
2
lightdm lightdm
4096
Jul
24
16
:
20
pulse-2L9K88eMlGn7/
15
drwx------
2
root root
4096
Jul
24
16
:
20
pulse-PKdhtXMmr18n/
16
-rw-rw-r--
1
lightdm lightdm
0
Jul
24
16
:
20
unity_support_test.
0
17 hao@hao:/tmp$ ll /tmp/mvtest
18
total
12
19
drwxrwxr-x
2
hao hao
4096
Jul
24
16
:
54
./
20
drwxrwxrwt
8
root root
4096
Jul
24
16
:
54
../
21
-rw-r--r--
1
hao hao
3486
Jul
24
16
:
53
bashrc
22 hao@hao:/tmp$ mv mvtest mvtest1
23 hao@hao:/tmp$ ll /tmp
24
total
36
25
drwxrwxrwt
8
root root
4096
Jul
24
16
:
55
./
26
drwxr-xr-x
23
root root
4096
Jul
21
20
:
45
../
27
drwxrwxrwt
2
root root
4096
Jul
24
16
:
20
.ICE-unix/
28
-r--r--r--
1
root root
11
Jul
24
16
:
20
.X0-
lock
29
drwxrwxrwt
2
root root
4096
Jul
24
16
:
20
.X11-unix/
30
drwxrwxrwt
2
lightdm lightdm
4096
Jul
24
16
:
20
at-spi2/
31
drwxrwxr-x
2
hao hao
4096
Jul
24
16
:
54
mvtest1/
32
drwx------
2
lightdm lightdm
4096
Jul
24
16
:
20
pulse-2L9K88eMlGn7/
33
drwx------
2
root root
4096
Jul
24
16
:
20
pulse-PKdhtXMmr18n/
34
-rw-rw-r--
1
lightdm lightdm
0
Jul
24
16
:
20
unity_support_test.
0
35 hao@hao:/tmp$ cp ~/.bashrc bashrc1
36 hao@hao:/tmp$ cp ~/.bashrc bashrc2
37 hao@hao:/tmp$ mv bashrc1 bashrc2 mvtest1
38 hao@hao:/tmp$ ll /tmp/mvtest1
39
total
20
40
drwxrwxr-x
2
hao hao
4096
Jul
24
16
:
58
./
41
drwxrwxrwt
8
root root
4096
Jul
24
16
:
58
../
42
-rw-r--r--
1
hao hao
3486
Jul
24
16
:
53
bashrc
43
-rw-r--r--
1
hao hao
3486
Jul
24
16
:
58
bashrc1
44
-rw-r--r--
1
hao hao
3486
Jul
24
16
:
58
bashrc2
? 二、文件內(nèi)容及屬性查閱
1、cat(concatenate)——文件內(nèi)容查看命令
cat命令可以同時(shí)查看多個(gè)文件內(nèi)容。cat命令是由第一行到最后一行連續(xù)顯示在屏幕上,還有一個(gè)命令tac是將文件內(nèi)容由最后一行到第一行反向在屏幕上顯示出來(lái)。當(dāng)文件內(nèi)容過(guò)多時(shí)候,可以配合more或者less命令來(lái)執(zhí)行較好。也可以配合shift+PageUp或者PageDown鍵使用。
命令格式 :cat [options] [file]...
命令參數(shù):
-b(number-nonblank):列出行號(hào),僅對(duì)非空白行做出行號(hào)顯示
-n(number):列出行號(hào),對(duì)空白行也作出行號(hào)顯示
-E(show-ends):顯示文本行結(jié)束符號(hào)'$',不同于windows的'^M$'
-T(show-tabs):將[Tab]鍵以^I顯示出來(lái)
-v(show-nonprinting):列出一些看不出來(lái)的特殊符號(hào)
1 hao@hao:/tmp$ cat -n -E -T /etc/issue /etc/fstab
2
1
Ubuntu
12.04
.
1
LTS \n \l$
3
2
$
4
3
# /etc/fstab: static
file
system information.$
5
4
#$
6
5
# Use
'
blkid
'
to print the universally unique identifier
for
a$
7
6
# device; this may be used with UUID= as a
more
robust way to name devices$
8
7
# that works even
if
disks are added and removed. See fstab(
5
).$
9
8
#$
10
9
# <
file
system> <
mount
point> <type> <options> <dump> <pass>
$
11
10
proc /proc proc nodev,noexec,nosuid
0
0
$
12
11
# / was on /dev/
sda1 during installation$
13
12
UUID=edc98e92-b3a2-44f0-a0e9-079b479d229b / ext4 errors=remount-ro
0
1
$
14
13
# swap was on /dev/
sda5 during installation$
15
14
UUID=8f9d467f-
8359
-421d-96
cc
-156b9e4310e5 none swap sw
0
0
$
16
15
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8
0
0
$
cat命令還可以用'cat file1 file2 >> file'來(lái)將幾個(gè)文件追加到另一個(gè)文件的末尾,或者用'cat file1 > file2'用來(lái)覆蓋文件。或者用'cat > file'從磁盤來(lái)創(chuàng)建文件(在輸入有錯(cuò)時(shí)候可以用ctrl+backspace刪除,最后按ctrl+d保存)
1 hao@hao:/tmp$ cat fstab
2
# /etc/fstab: static
file
system information.
3
#
4
# Use
'
blkid
'
to print the universally unique identifier
for
a
5
# device; this may be used with UUID= as a
more
robust way to name devices
6
# that works even
if
disks are added and removed. See fstab(
5
).
7
#
8
# <
file
system> <
mount
point> <type> <options> <dump> <pass>
9
proc /proc proc nodev,noexec,nosuid
0
0
10
# / was on /dev/
sda1 during installation
11
UUID=edc98e92-b3a2-44f0-a0e9-079b479d229b / ext4 errors=remount-ro
0
1
12
# swap was on /dev/
sda5 during installation
13
UUID=8f9d467f-
8359
-421d-96
cc
-156b9e4310e5 none swap sw
0
0
14
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8
0
0
15
Ubuntu
12.04
.
1
LTS \n \l
16
17 hao@hao:/tmp$ cat issue
18
Ubuntu
12.04
.
1
LTS \n \l
19
20 hao@hao:/tmp$ cat issue > fstab
21 hao@hao:/tmp$ cat fstab
22
Ubuntu
12.04
.
1
LTS \n \l
23
24 hao@hao:/tmp$ cat issue
25
Ubuntu
12.04
.
1
LTS \n \l
26
27 hao@hao:/tmp$ cp /etc/fstab /tmp
28 hao@hao:/tmp$ cat /tmp/issue >>/tmp/fstab
29 hao@hao:/tmp$ cat issue
30
Ubuntu
12.04
.
1
LTS \n \l
31
32 hao@hao:/tmp$ cat fstab
33
# /etc/fstab: static
file
system information.
34
#
35
# Use
'
blkid
'
to print the universally unique identifier
for
a
36
# device; this may be used with UUID= as a
more
robust way to name devices
37
# that works even
if
disks are added and removed. See fstab(
5
).
38
#
39
# <
file
system> <
mount
point> <type> <options> <dump> <pass>
40
proc /proc proc nodev,noexec,nosuid
0
0
41
# / was on /dev/
sda1 during installation
42
UUID=edc98e92-b3a2-44f0-a0e9-079b479d229b / ext4 errors=remount-ro
0
1
43
# swap was on /dev/
sda5 during installation
44
UUID=8f9d467f-
8359
-421d-96
cc
-156b9e4310e5 none swap sw
0
0
45
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8
0
0
46
Ubuntu
12.04
.
1
LTS \n \l
47
48
hao@hao:/tmp$ cat >test.txt
49
this is a test
file
50
hao@hao:/tmp$ cat test.txt
51
this is a test
file
2、more——一頁(yè)一頁(yè)翻動(dòng)
命令格式: more[options]file 或 more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]
命令參數(shù):?
常用功能鍵 :
Enter????向下n行,需要定義。默認(rèn)為1行
Ctrl+F???向下滾動(dòng)一屏
空格鍵??向下滾動(dòng)一屏
b或Ctrl+b ?返回上一屏
/字符串 向下查詢“字符串”關(guān)鍵字
=???????輸出當(dāng)前行的行號(hào)
:f?????輸出文件名和當(dāng)前行的行號(hào)
V??????調(diào)用vi編輯器
!命令???調(diào)用Shell,并執(zhí)行命令?
q???????退出more
less命令可以使用上下等按鍵功能來(lái)往前往后翻看文件,而more沒(méi)有辦法向前翻
3、head——數(shù)據(jù)選取命令( 默認(rèn)前十行)。
命令格式: head [-n number] file
[hao@hao ~]$
head
/etc/
man
.config
[hao@hao ~]$
head
-n
20
/etc/
man
.config
?4、tail——取出后面幾行
命令格式 :tail [-n -number] file
[hao@hao ~]$
tail
/etc/
man
.config
[hao@hao
~]$
tail
-n -
20
/etc/
man
.config
如果不知道文件有幾行,但是想列出100行以后的數(shù)據(jù)時(shí)
[hao@hao ~]$
tail
-n +
100
/etc/main.config
命令參數(shù):
-f(follow):即時(shí)輸出追加到文件末尾的新內(nèi)容,該參數(shù)可以用來(lái)監(jiān)控日志文件等
5、touch——修改文件時(shí)間或者創(chuàng)建新文件
文件時(shí)間:在linux下,有3個(gè)時(shí)間參數(shù),用來(lái)記錄每個(gè)文件的主要變動(dòng)時(shí)間。可以用stat命令來(lái)查看這三個(gè)時(shí)間。
a、access time(atime):訪問(wèn)時(shí)間。當(dāng)對(duì)文件進(jìn)行讀取時(shí),會(huì)改變這個(gè)時(shí)間。如使用cat.more.less,tail,head,cp等命令。ls命令不會(huì)更改該時(shí)間
hao@hao ~]$ touch /tmp/tstouch.txt
[hao@hao ~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
0
Blocks:
0
IO Block:
4096
regular empty
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
Modify:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
Change:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
[hao@hao ~]$ cat /tmp/tstouch.txt
[hao@hao ~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
0
Blocks:
0
IO Block:
4096
regular empty
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
13
:
40
:
12.682999977
+
0800
Modify:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
Change:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
[hao@hao ~]$ ls /tmp/
tstouch.txt
/tmp/
tstouch.txt
[hao@hao
~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
0
Blocks:
0
IO Block:
4096
regular empty
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
13
:
40
:
12.682999977
+
0800
Modify:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
Change:
2014
-
08
-
05
13
:
39
:
39.387999895
+
0800
b、 modification time(mtime):修改時(shí)間。當(dāng)文件內(nèi)容修改時(shí),會(huì)改變?cè)摃r(shí)間。如命令vi改變內(nèi)容時(shí),會(huì)改變?cè)摃r(shí)間。使用ll命令默認(rèn)顯示該時(shí)間參數(shù)
[hao@hao ~]$ vi /tmp/tstouch.txt
[hao@hao ~]$ cat /tmp/
tstouch.txt
hello
! world!
[hao@hao ~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
14
Blocks:
8
IO Block:
4096
regular
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
13
:
51
:
01.097999996
+
0800
Modify:
2014
-
08
-
05
13
:
50
:
51.630999942
+
0800
Change:
2014
-
08
-
05
13
:
50
:
51.633999974
+
0800
c、status time(ctime):狀態(tài)時(shí)間。是該文件的i節(jié)點(diǎn)最后一次被修改的時(shí)間,通過(guò)chmod、chown命令修改一次文件屬性,這個(gè)時(shí)間就會(huì)更新。
命令格式:touch [options] file
命令參數(shù):
-a(access time):僅修改訪問(wèn)時(shí)間.
-m(modification time):僅改變修改時(shí)間。
-t(stamp):后面可以接欲修改的時(shí)間而不用目前的時(shí)間。格式為[YYMMDDhhmm]
[hao@hao ~]$ touch -a /tmp/tstouch.txt
[hao@hao ~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
14
Blocks:
8
IO Block:
4096
regular
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
13
:
56
:
56.491999787
+
0800
Modify:
2014
-
08
-
05
13
:
50
:
51.630999942
+
0800
Change:
2014
-
08
-
05
13
:
56
:
56.491999787
+
0800
[hao@hao ~]$ touch -m /tmp/tstouch.txt
[hao@hao ~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
14
Blocks:
8
IO Block:
4096
regular
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
13
:
56
:
56.491999787
+
0800
Modify:
2014
-
08
-
05
14
:
06
:
10.507000067
+
0800
Change:
2014
-
08
-
05
14
:
06
:
10.507000067
+
0800
[hao@hao ~]$ touch -t 1408050206 /tmp/tstouch.txt
[hao@hao ~]$ stat /tmp/
tstouch.txt
File: `
/tmp/tstouch.txt
'
Size:
14
Blocks:
8
IO Block:
4096
regular
file
Device: 803h
/2051d Inode:
133739
Links:
1
Access: (
0664
/-rw-rw-r--) Uid: (
500
/ hao) Gid: (
500
/
hao)
Access:
2014
-
08
-
05
02
:
06
:
00.000000000
+
0800
Modify:
2014
-
08
-
05
02
:
06
:
00.000000000
+
0800
Change:
2014
-
08
-
05
14
:
07
:
21.688000461
+
0800
6、cut——分割文件中的字符串
命令格式 :cut [options] [file]
命令參數(shù):
-b(bytes):只選中指定的這些字節(jié)。
-c(characters):只選擇指定的這些字符。
-d(delimiter):使用指定分界符代替制表符作為區(qū)域分界。
-f(fields):只選中指定的這些域,并打印所有不包含分界符的行。
這里-3,代表的是選擇第一個(gè)到第三個(gè)字符,3-5則是選取第三個(gè)到第五個(gè)字符,3-5,8則是選取第三個(gè)到第五個(gè)字符以及第八個(gè)字符。
[hao@hao ~]$
cat
/tmp/
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
...
[hao@hao
~]$
cut
-b -
3
/tmp/
passwd
roo
bin
dae
adm
...
[hao@hao
~]$
cut
-b
3
-
5
,
8
/tmp/
passwd
ot:
0
n:x:
emox
m:x:
...
-c的用法和-b差不多,只不過(guò)是截取的字符,而不是字節(jié)。漢字也被認(rèn)為是一個(gè)字符。
-d后面根的冒號(hào)是分割文件行的的分割符,-f用來(lái)設(shè)置提取第幾個(gè)域。-d一般情況下根-f一起使用,而不能和-b,-c一起使用
[hao@hao ~]$
cut
-f
1
/tmp/
passwd
|
head
-n
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
[hao@hao
~]$
cut
-d : -f
1
/tmp/
passwd
|
head
-n
5
root
bin
daemon
adm
lp
[hao@hao
~]$
cut
-d : -f
1
,
2
/tmp/
passwd
|
head
-n
5
root:x
bin:x
daemon:x
adm:x
lp
:x
[hao@hao
~]$
c
ut -d : -f
2
-
3
,
5
/tmp/
passwd
|
head
-n
5
x:
0
:root
x:
1
:bin
x:
2
:daemon
x:
3
:adm
x:
4
:
lp
7、awk
8、sed
9、grep
10.sort——排序命令
不會(huì)改變?cè)次募拇涡颍粫?huì)改變顯示的次序。默認(rèn)排序方法,根據(jù)字符ASCII碼值得升序排序。
命令格式: sort [options] [file]
命令參數(shù):
-n(number):按數(shù)值大小排序
-r(reverse):降序排序
-t(field separator):指定字符分隔符
-k(key):指定字段
-f(ignore-case):不區(qū)分大小寫(xiě)
-u(unique):相同內(nèi)容只顯示一次。這里的相同是指 相鄰 并且內(nèi)容相同。
[hao@hao ~]$ cat /etc/
fstab
#
#
/etc/
fstab
# Created by anaconda on Sat Jul
26
20
:
12
:
53
2014
#
# Accessible filesystems, by reference, are maintained under
'
/dev/disk
'
# See
man
pages fstab(
5
), findfs(
8
),
mount
(
8
) and/or blkid(
8
)
for
more
info
#
UUID
=dfdbed34-b8ea-4f72-859c-58d511c2c5f9 / ext4 defaults
1
1
UUID
=96cf0fa8-75f7-
4215
-ab4e-ed9e1f223a41 /boot ext4 defaults
1
2
UUID
=3c71e294-3ab0-4bfa-ad86-a38fa51919d2 swap swap defaults
0
0
tmpfs
/dev/shm tmpfs defaults
0
0
devpts
/dev/pts devpts gid=
5
,mode=
620
0
0
sysfs
/sys sysfs defaults
0
0
proc
/proc proc defaults
0
0
[hao@hao ~]$ sort /etc/
fstab
#
#
#
# Accessible filesystems, by reference, are maintained under
'
/dev/disk
'
# Created by anaconda on Sat Jul
26
20
:
12
:
53
2014
devpts
/dev/pts devpts gid=
5
,mode=
620
0
0
#
/etc/
fstab
proc
/proc proc defaults
0
0
# See
man
pages fstab(
5
), findfs(
8
),
mount
(
8
) and/or blkid(
8
)
for
more
info
sysfs
/sys sysfs defaults
0
0
tmpfs
/dev/shm tmpfs defaults
0
0
UUID
=3c71e294-3ab0-4bfa-ad86-a38fa51919d2 swap swap defaults
0
0
UUID
=96cf0fa8-75f7-
4215
-ab4e-ed9e1f223a41 /boot ext4 defaults
1
2
UUID
=dfdbed34-b8ea-4f72-859c-58d511c2c5f9 / ext4 defaults
1
1
把/tmp/passwd按照第三個(gè)字段的數(shù)值大小排序
[hao@hao ~]$
sort
-t : -k
3
-n /tmp/
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
sync
:x:
5
:
0
:
sync
:/sbin:/bin/
sync
shutdown:x:
6
:
0
:shutdown:/sbin:/sbin/
shutdown
halt:x:
7
:
0
:halt:/sbin:/sbin/
halt
mail:x:
8
:
12
:mail:/var/spool/mail:/sbin/
nologin
uucp:x:
10
:
14
:uucp:/var/spool/uucp:/sbin/
nologin
operator:x:
11
:
0
:operator:/root:/sbin/
nologin
games:x:
12
:
100
:games:/usr/games:/sbin/
nologin
gopher:x:
13
:
30
:gopher:/var/gopher:/sbin/
nologin
ftp
:x:
14
:
50
:FTP User:/var/
ftp
:/sbin/
nologin
rpcuser:x:
29
:
29
:RPC Service User:/var/lib/nfs:/sbin/
nologin
rpc:x:
32
:
32
:Rpcbind Daemon:/var/cache/rpcbind:/sbin/
nologin
ntp:x:
38
:
38
::/etc/ntp:/sbin/
nologin
gdm:x:
42
:
42
::/var/lib/gdm:/sbin/
nologin
apache:x:
48
:
48
:Apache:/var/www:/sbin/
nologin
webalizer:x:
67
:
67
:Webalizer:/var/www/usage:/sbin/
nologin
haldaemon:x:
68
:
68
:HAL daemon:/:/sbin/
nologin
vcsa:x:
69
:
69
:virtual console memory owner:/dev:/sbin/
nologin
tcpdump:x:
72
:
72
::/:/sbin/
nologin
sshd:x:
74
:
74
:Privilege-separated SSH:/var/empty/sshd:/sbin/
nologin
dbus:x:
81
:
81
:System message bus:/:/sbin/
nologin
postfix:x:
89
:
89
::/var/spool/postfix:/sbin/
nologin
tomcat:x:
91
:
91
:Apache Tomcat:/usr/share/tomcat6:/sbin/
nologin
nobody:x:
99
:
99
:Nobody:/:/sbin/
nologin
usbmuxd:x:
113
:
113
:usbmuxd user:/:/sbin/
nologin
avahi
-autoipd:x:
170
:
170
:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/
nologin
abrt:x:
173
:
173
::/etc/abrt:/sbin/
nologin
pulse:x:
497
:
496
:PulseAudio System Daemon:/var/run/pulse:/sbin/
nologin
saslauth:x:
498
:
76
:
"
Saslauthd user
"
:/var/empty/saslauth:/sbin/
nologin
rtkit:x:
499
:
497
:RealtimeKit:/proc:/sbin/
nologin
hao:x:
500
:
500
:haohao:/home/hao:/bin/
bash
nfsnobody:x:
65534
:
65534
:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
?
[hao@hao ~]$
sort
/tmp/
tssort.txt
111
111
231
456
456
67
7890
[hao@hao
~]$
sort
-n /tmp/
tssort.txt
67
111
111
231
456
456
7890
[hao@hao ~]$
sort
-u -n /tmp/
tssort.txt
67
111
231
456
7890
11.uniq——報(bào)告相同的行,也可以去除相同的行
這里的相同的行也是指相鄰并且內(nèi)容相同的行
命令參數(shù):
-d(repeated):打印出重復(fù)的行
-c(count):顯示行 重復(fù) 的次數(shù)
[hao@hao ~]$
cat
/tmp/
tssort.txt
456
456
67
111
231
456
7890
111
[hao@hao ~]$
uniq
/tmp/
tssort.txt
456
67
111
231
456
7890
111
?
[hao@hao ~]$
uniq
-d /tmp/
tssort.txt
456
?
[hao@hao ~]$
uniq
-c /tmp/
tssort.txt
2
456
1
67
1
111
1
231
1
456
1
7890
1
111
12.wc(word count)——文本統(tǒng)計(jì)
命令參數(shù):
-l(line):只顯示行數(shù)
-w(word):只顯示單詞數(shù)
-c(bytes):只顯示字節(jié)數(shù)
-m(chars):只顯示字符數(shù)
-L(max-line-length):最長(zhǎng)一行包含的字節(jié)數(shù)
下面的命令輸出含義,15行,78個(gè)單詞,805個(gè)字節(jié)(包括空白)
[hao@hao ~]$
wc
/etc/
fstab
15
78
805
/etc/fstab
?
[hao@hao ~]$
wc
-L /etc/
fstab
93
/etc/fstab
?
13.tr(translate or remove)——轉(zhuǎn)換或刪除字符
命令格式: tr [OPTION]... SET1 [SET2]
命令參數(shù):
-d(delete):刪除指定字符
將小寫(xiě)字母a,b轉(zhuǎn)換為大寫(xiě)字母A、B
?
[hao@hao ~]$
tr
'
ab
'
'
AB
'
acd
Acd
abcd
ABcd
cd
cd
acdb
AcdB
?
由上面可以看出,tr可以直接對(duì)終端界面進(jìn)行輸入并且替換。命令格式中并沒(méi)有對(duì)指定文件進(jìn)行轉(zhuǎn)換,因此,若要對(duì)文件進(jìn)行轉(zhuǎn)換,則需要用到輸入重定向。
[hao@hao ~]$
tr
'
ab
'
'
AB
'
< /tmp/
passwd
|
head
-n
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
將所有的小寫(xiě)字母變成大寫(xiě)字母
[hao@hao ~]$
tr
'
a-z
'
'
A-Z
'
刪除字母a、b
[hao@hao ~]$ tr -d 'ab'
abc
c
ac
c
acb
c
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

