|
1.pwd Print the current working directory. With the -P option, pwd prints the physical directory, without any symbolic links; the -L option makes pwd follow symbolic links. 通常進(jìn)入系統(tǒng)后,往往想知道自己當(dāng)前位置,因此pwd就派上用場(chǎng)了,使用時(shí)注意一下-L,-P的區(qū)別,-P選項(xiàng)會(huì)顯示物理路徑。 ![]()
1 mengxialeideMBP:~ mengxianglei$ ls -ld /etc
2 lrwxr-xr-x@ 1 root wheel 11 12 2 2017 /etc -> private/etc
3 mengxialeideMBP:~ mengxianglei$ cd /etc
4 mengxialeideMBP:etc mengxianglei$ pwd
5 /etc
6 mengxialeideMBP:etc mengxianglei$ pwd -L
7 /etc
8 mengxialeideMBP:etc mengxianglei$ pwd -P
9 /private/etc
2.paste The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character, and writes the resulting lines to standard output. If end-of-file is reached on an input file while other input files still contain data, the file is treated as if it were an endless source of empty lines. 英文的意思粘貼,可以將兩個(gè)文件粘合為一個(gè),粘貼的方式時(shí)按照行進(jìn)行粘合。 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:mxl mengxianglei$ cat char.txt 13 a 14 b 15 c 16 d 17 e 18 f 19 g 20 h 21 i 22 j 23 mengxialeideMBP:mxl mengxianglei$ paste int.txt char.txt 24 1 a 25 2 b 26 3 c 27 4 d 28 5 e 29 6 f 30 7 g 31 8 h 32 9 i 33 10 j -d 選項(xiàng)可以指定粘合的分隔符 ![]()
1 mengxialeideMBP:mxl mengxianglei$ paste -d '*' char.txt int.txt
2 a*1
3 b*2
4 c*3
5 d*4
6 e*5
7 f*6
8 g*7
9 h*8
10 i*9
11 j*10
-s 選項(xiàng)使文本內(nèi)容按照一行顯示 ![]()
1 mengxialeideMBP:mxl mengxianglei$ seq 1000 > test.txt 2 mengxialeideMBP:mxl mengxianglei$ wc -l test.txt 3 1000 test.txt 4 mengxialeideMBP:mxl mengxianglei$ paste -s test.txt |wc -l 5 1 -sd配合使用格式單個(gè)文件,這個(gè)時(shí)候的分隔符是依次使用的 paste 還可以將列轉(zhuǎn)行的時(shí)候,指定幾行的內(nèi)容歸為一行,- 代表一行的內(nèi)容,下面是把三行歸為一行進(jìn)行處理 ![]()
1 mengxialeideMBP:mxl mengxianglei$ paste - - - <char.txt 2 a b c 3 d e f 4 g h i 5 j 6 7 mengxialeideMBP:mxl mengxianglei$ paste -d '+-*' - - - -<char.txt 8 9 a+b-c*d 10 11 e+f-g*h 12 13 i+j-* 3.ps The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals. ps命令工作中常用,排查進(jìn)程是否啟動(dòng),獲取進(jìn)程號(hào)等等,通常可以結(jié)合grep獲取到想要的進(jìn)程的運(yùn)行情況 -e選項(xiàng)顯示所有進(jìn)程,-f選項(xiàng)顯示uip,pid等 ![]()
1 mengxialeideMBP:mxl mengxianglei$ ps -ef|grep server
2 55 51 1 0 251018 ?? 0:01.46 /System/Library/CoreServices/appleeventsd --server
3 0 364 1 0 251018 ?? 0:00.04 /System/Library/CoreServices/CrashReporterSupportHelper server-init
4 0 408 1 0 251018 ?? 0:00.19 /usr/sbin/systemsoundserverd
5 0 567 1 0 261018 ?? 0:00.23 /System/Library/CoreServices/SubmitDiagInfo server-init
6 501 3621 3488 0 11:40上午 ttys000 0:00.00 grep server
-u可以獲取到指定用戶下的相關(guān)進(jìn)程 ![]()
1 mengxialeideMBP:mxl mengxianglei$ ps -u mengxianglei 2 UID PID TTY TIME CMD 3 501 265 ?? 0:06.43 /usr/sbin/cfprefsd agent 4 501 266 ?? 0:12.98 /usr/libexec/UserEventAgent (Aqua) 5 501 268 ?? 0:19.82 /usr/sbin/distnoted agent 4.cd Change the current directory to DIR 更改當(dāng)前的目錄到指定的路徑 用法比較簡(jiǎn)單,cd 后面直接跟需要跳轉(zhuǎn)的路徑即可。 特殊符號(hào)說(shuō)明 ~代表用戶家目錄 -代表用戶上一次的目錄 . 代表用戶當(dāng)前目錄 ..代表上一級(jí)目錄 以/開(kāi)頭的是絕對(duì)路徑,否則是以當(dāng)前路徑為基礎(chǔ)的相對(duì)路徑 5.mkdir The mkdir utility creates the directories named as operands, in the order specified, using mode rwxrwxrwx (0777) as modified by the current umask(2). 創(chuàng)建目錄命令,類似window里面的信件文件夾 -p 如果創(chuàng)建多級(jí)目錄,需要使用-p選項(xiàng) -v 顯示創(chuàng)建詳細(xì)過(guò)程 創(chuàng)建目錄時(shí)可以技巧性創(chuàng)建多個(gè)目錄 ![]()
1 mkdir -p ./mulu/{1,2}
2 mengxialeideMBP:mxl mengxianglei$ cd mulu/
3 mengxialeideMBP:mulu mengxianglei$ ls
4 1 2
5 mengxialeideMBP:mulu mengxianglei$
![]()
1 mengxialeideMBP:mxl mengxianglei$ mkdir -p ./mulu/{1..3} 2 mengxialeideMBP:mxl mengxianglei$ cd mulu 3 mengxialeideMBP:mulu mengxianglei$ ls 4 1 2 3 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat mkmulu.txt
2 ./mulu/1
3 ./mulu/2
4 ./mulu/3
5 ./mulu/4
6 mengxialeideMBP:mxl mengxianglei$ mkdir -p `cat mkmulu.txt`
7 mengxialeideMBP:mxl mengxianglei$ cd mulu
8 mengxialeideMBP:mulu mengxianglei$ ls
9 1 2 3 4
6.touch The touch utility sets the modification and access times of files. If any file does not exist, it is created with default permissions. 創(chuàng)建空文件,如果文件存在修改文件時(shí)間 linux文件時(shí)間介紹 atime:文件的最后訪問(wèn)時(shí)間 mtime:最后的文件修改時(shí)間(修改文件內(nèi)容) ctime:最后的文件狀態(tài)改變時(shí)間(訪問(wèn)、修改文件內(nèi)容、文件移動(dòng)、權(quán)限改變) ![]()
1 mengxialeideMBP:mxl mengxianglei$ touch touchfile 2 mengxialeideMBP:mxl mengxianglei$ ls -l 3 total 0 4 -rw-r--r-- 1 mengxianglei staff 0 11 4 13:06 touchfile 5 mengxialeideMBP:mxl mengxianglei$ stat touchfile 6 16777220 3212171 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:06:48 2018' 'Nov 4 13:06:48 2018' 'Nov 4 13:06:48 2018' 'Nov 4 13:06:48 2018' 4194304 0 0 touchfile 7 mengxialeideMBP:mxl mengxianglei$ touch -a touchfile 8 mengxialeideMBP:mxl mengxianglei$ stat touchfile 9 16777220 3212171 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:07:41 2018' 'Nov 4 13:06:48 2018' 'Nov 4 13:07:41 2018' 'Nov 4 13:06:48 2018' 4194304 0 0 touchfile 10 mengxialeideMBP:mxl mengxianglei$ touch -m touchfile 11 mengxialeideMBP:mxl mengxianglei$ stat touchfile 12 16777220 3212171 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:07:41 2018' 'Nov 4 13:08:55 2018' 'Nov 4 13:08:55 2018' 'Nov 4 13:06:48 2018' 4194304 0 0 touchfile -r 選項(xiàng),可以修改目標(biāo)文件時(shí)間屬性與源文件一樣 ![]()
1 mengxialeideMBP:mxl mengxianglei$ touch 1.txt
2 mengxialeideMBP:mxl mengxianglei$ stat 1.txt
3 16777220 3212193 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 4194304 0 0 1.txt
4 mengxialeideMBP:mxl mengxianglei$ touch 2.txt
5 mengxialeideMBP:mxl mengxianglei$ stat 2.txt
6 16777220 3212194 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:13:00 2018' 'Nov 4 13:13:00 2018' 'Nov 4 13:13:00 2018' 'Nov 4 13:13:00 2018' 4194304 0 0 2.txt
7 mengxialeideMBP:mxl mengxianglei$ touch -r 1.txt 2.txt
8 mengxialeideMBP:mxl mengxianglei$ stat 2.txt
9 16777220 3212194 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 'Nov 4 13:13:14 2018' 'Nov 4 13:12:18 2018' 4194304 0 0 2.txt
7.ls list directory contents 顯示目錄內(nèi)容 常用參數(shù)介紹: l 長(zhǎng)格式顯示 a 顯示所有文件包擴(kuò)隱藏文件 A 顯示所有文件包括隱藏文件,但不顯示.、.. t 按照最后修改時(shí)間(mtime)排序 r 相反次序 F 目錄后面加/ i 顯示inode節(jié)點(diǎn)信息 d 顯示目錄本身信息 h 人類可讀 --full-time 顯示完整時(shí)間 ![]()
1 mengxialeideMBP:mxl mengxianglei$ ls -AlrtFih 2 total 0 3 3212194 -rw-r--r-- 1 mengxianglei staff 0B 11 4 13:12 2.txt 4 3212193 -rw-r--r-- 1 mengxianglei staff 0B 11 4 13:17 1.txt 5 3212415 drwxr-xr-x 2 mengxianglei staff 64B 11 4 13:38 mulu/ 8.cp In the first synopsis form, the cp utility copies the contents of the source_file to the target_file. In the second synopsis form, the contents of each named source_file is copied to the destination target_directory. The names of the files themselves are not changed. If cp detects an attempt to copy a file to itself, the copy will fail. 復(fù)制文件或目錄,通常對(duì)一些文件的修改,我們要先備份一下,因此cp命令比較常用 -r 復(fù)制目錄時(shí)遞歸復(fù)制,必須加上-r選項(xiàng) -p 保留文件所有者、權(quán)限信息、時(shí)間屬性 -d 如果文件為鏈接文件,僅復(fù)制鏈接文件本身 -a 相當(dāng)于drp ![]()
1 mengxialeideMBP:mxl mengxianglei$ ls -ld script/
2 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:36 script/
3 mengxialeideMBP:mxl mengxianglei$ cp -r script 'script.$(date +%F)'
4 mengxialeideMBP:mxl mengxianglei$ ls -l
5 total 0
6 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:36 script
7 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:37 script.2018-11-04
-i 如果存在相同文件,則提示用戶進(jìn)行確認(rèn) ![]()
1 mengxialeideMBP:script mengxianglei$ touch getdate.sh getcurrentdate.sh 2 mengxialeideMBP:script mengxianglei$ cp -i getdate.sh getcurrentdate.sh 3 overwrite getcurrentdate.sh? (y/n [n]) y 9.mv In its first form, the mv utility renames the file named by the source operand to the destination path named by the target operand. This form is assumed when the last operand does not name an already existing directory. 移動(dòng)文件或目錄,如果目標(biāo)不存在,則重命名 ![]()
1 mengxialeideMBP:script mengxianglei$ mkdir dir{1..5}
2 mengxialeideMBP:script mengxianglei$ ls
3 dir1 dir2 dir3 dir4 dir5
4 mengxialeideMBP:script mengxianglei$ mv dir1 dir6
5 mengxialeideMBP:script mengxianglei$ ls -ld
6 drwxr-xr-x 7 mengxianglei staff 224 11 4 14:52 .
7 mengxialeideMBP:script mengxianglei$ ls -l ./
8 total 0
9 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir2
10 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir3
11 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir4
12 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir5
13 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir6
14 mengxialeideMBP:script mengxianglei$ mv dir2 dir6
15 mengxialeideMBP:script mengxianglei$ ls -l ./
16 total 0
17 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir3
18 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir4
19 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir5
20 drwxr-xr-x 3 mengxianglei staff 96 11 4 14:52 dir6
21 mengxialeideMBP:script mengxianglei$ ls -l dir6
22 total 0
23 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir2
10.rm The rm utility attempts to remove the non-directory type files specified on the command line. If the permissions of the file do not permit writing, and the standard input device is a terminal, the user is prompted (on the standard error output) for confirmation. 刪除文件或者目錄,系統(tǒng)運(yùn)行會(huì)產(chǎn)生一些垃圾文件,因此為了節(jié)省磁盤(pán),會(huì)經(jīng)常將不需要的備份或者垃圾文件進(jìn)行刪除操作 -r 可以遞歸刪除目錄及目錄里面的內(nèi)容 -f 強(qiáng)制刪除,無(wú)需提示 ![]()
1 mengxialeideMBP:script mengxianglei$ rm -rf dir6 2 mengxialeideMBP:script mengxianglei$ ls 3 dir3 dir4 dir5 通??梢越Y(jié)合find命令進(jìn)行文件刪除操作 ![]()
1 mengxialeideMBP:script mengxianglei$ ls
2 dir3 dir4 dir5
3 mengxialeideMBP:script mengxianglei$ find ./ -type d -name 'dir3'|xargs rm -rf
4 mengxialeideMBP:script mengxianglei$ ls
5 dir4 dir5
11.ln The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the ``copies''; instead, a link ``points'' to the original copy. There are two types of links; hard links and symbolic links. How a link ``points'' to a file is one of the differences between a hard and symbolic link. linux下創(chuàng)建鏈接分為硬鏈接合軟鏈接,兩種鏈接方式時(shí)由區(qū)別的,默認(rèn)創(chuàng)建硬鏈接,-s參數(shù)創(chuàng)建軟鏈接,即快捷方式。硬鏈接相當(dāng)于文件的另一個(gè)入口,與源文件是同一個(gè)inode編號(hào),軟鏈接則由自己的inode,是一個(gè)鏈接文件。 平時(shí)的應(yīng)用創(chuàng)建軟鏈接的情況較多,另外,目錄是不可以創(chuàng)建硬鏈接的。 ![]()
1 mengxialeideMBP:script mengxianglei$ touch file1 2 mengxialeideMBP:script mengxianglei$ ln file1 hard_link 3 mengxialeideMBP:script mengxianglei$ ln -s file1 soft_link 4 mengxialeideMBP:script mengxianglei$ ls -l 5 total 0 6 -rw-r--r-- 2 mengxianglei staff 0 11 4 15:15 file1 7 -rw-r--r-- 2 mengxianglei staff 0 11 4 15:15 hard_link 8 lrwxr-xr-x 1 mengxianglei staff 5 11 4 15:16 soft_link -> file1 9 mengxialeideMBP:script mengxianglei$ ls -lhi 10 total 0 11 3213173 -rw-r--r-- 2 mengxianglei staff 0B 11 4 15:15 file1 12 3213173 -rw-r--r-- 2 mengxianglei staff 0B 11 4 15:15 hard_link 13 3213177 lrwxr-xr-x 1 mengxianglei staff 5B 11 4 15:16 soft_link -> file1 14 mengxialeideMBP:script mengxianglei$ 12.find The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree. 通常為了查找一些內(nèi)容,進(jìn)行find命令,比較常用。 -type 查找類型 -name 文件名稱 -mtime 修改時(shí)間 +、- ! 取反 -user 屬于某個(gè)用戶的文件 -nouser 不屬于任何用戶的文件 -maxdepth 1 最深一級(jí)目錄 ![]()
1 mengxialeideMBP:script mengxianglei$ touch {1..5}.txt
2 mengxialeideMBP:script mengxianglei$ ls
3 1.txt 2.txt 3.txt 4.txt 5.txt
4 mengxialeideMBP:script mengxianglei$ find ./ -name '1.txt' |xargs rm
5 mengxialeideMBP:script mengxianglei$ ls
6 2.txt 3.txt 4.txt 5.txt
7 mengxialeideMBP:script mengxianglei$ find ./ -name '2.txt' -exec rm {} \;
8 mengxialeideMBP:script mengxianglei$ ls
9 3.txt 4.txt 5.txt
通過(guò)-exec命令,給找到的文件批量改名字,逐條處理 ![]()
1 mengxialeideMBP:script mengxianglei$ touch {1..9}.txt 2 mengxialeideMBP:script mengxianglei$ find ./ -name '*.txt' -exec mv {} {}.bak \; 3 mengxialeideMBP:script mengxianglei$ ls 4 1.txt.bak 2.txt.bak 3.txt.bak 4.txt.bak 5.txt.bak 6.txt.bak 7.txt.bak 8.txt.bak 9.txt.bak 通過(guò)xargs,將查找的結(jié)果集一起處理 ![]()
1 mengxialeideMBP:script mengxianglei$ find ./ -name '*.txt'|xargs
2 .//9.txt .//8.txt .//5.txt .//4.txt .//6.txt .//7.txt .//3.txt .//2.txt .//1.txt
3 mengxialeideMBP:script mengxianglei$ find ./ -name '*.txt'|xargs rm -r
13.xargs The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments 標(biāo)準(zhǔn)輸入處理為入?yún)?/p> -n 每行顯示個(gè)數(shù) -d 指定分隔符號(hào) ![]()
1 mengxialeideMBP:script mengxianglei$ cat int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:script mengxianglei$ xargs < int.txt 13 1 2 3 4 5 6 7 8 9 10 14 mengxialeideMBP:script mengxianglei$ xargs -n 2 < int.txt 15 1 2 16 3 4 17 5 6 18 7 8 19 9 10 14.rename 替換文件名 rename from to file 15.basename 顯示文件名,不顯示路徑 ![]()
1 mengxialeideMBP:script mengxianglei$ ls
2 int.txt
3 mengxialeideMBP:script mengxianglei$ basename int.txt
4 int.txt
5 mengxialeideMBP:script mengxianglei$ basename int.txt .txt
6 int
16.dirname 顯示路徑,參數(shù)如果是相對(duì)路徑,則返回內(nèi)容也同樣相對(duì),參數(shù)如果是絕對(duì)路徑,返回內(nèi)容也是絕對(duì)。 ![]()
1 mengxialeideMBP:script mengxianglei$ dirname /Users/mengxianglei/mxl/script/int.txt 2 /Users/mengxianglei/mxl/script 3 mengxialeideMBP:script mengxianglei$ dirname int.txt 4 . 17.chattr 修改文件擴(kuò)展屬性 a 只能想文件追加數(shù)據(jù)。+a 、 -a i. 文件不能被刪除、改名、寫(xiě)入或者新增 +i、-i 18.lsattr 查看文件擴(kuò)展屬性 19.chown The chown utility changes the user ID and/or the group ID of the specified files. Symbolic links named by arguments are silently left unchanged unless -h is used. 更改文件所屬用戶即屬主 -R 遞歸更改目錄下文件 ![]()
1 chown mengxianglei ./dir1
2 chown -R mengxianglei.staff ./dir
3 chown -R .staff ./dir
4 chown -R :staff ./dir
20.chmod 改變文件或者目錄權(quán)限 -R 遞歸處理 r 讀權(quán)限 w 寫(xiě)權(quán)限 x 執(zhí)行權(quán)限 4,2,1 通過(guò)4,2,1來(lái)計(jì)算,可以理解4,2,1為讀寫(xiě)執(zhí)行的權(quán)重,計(jì)算的總和即文件的執(zhí)行權(quán)限。 ls -l 查看文件時(shí)候,第一位為文件的類型,后面9位分別代表屬主、屬組、其他用戶的讀、寫(xiě)、執(zhí)行 21.cat cat -- concatenate and print files 查看或者聚合文件,與paste不同的是,是按照文件內(nèi)容依次結(jié)合,而非逐行結(jié)合 通過(guò)cat將兩個(gè)文件內(nèi)容簡(jiǎn)單合并 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:mxl mengxianglei$ cat char.txt 13 a 14 b 15 c 16 d 17 e 18 f 19 g 20 h 21 i 22 j 23 k 24 l 25 m 26 n 27 o 28 p 29 q 30 r 31 s 32 t 33 u 34 v 35 w 36 x 37 y 38 z 39 mengxialeideMBP:mxl mengxianglei$ cat int.txt char.txt 40 1 41 2 42 3 43 4 44 5 45 6 46 7 47 8 48 9 49 10 50 a 51 b 52 c 53 d 54 e 55 f 56 g 57 h 58 i 59 j 60 k 61 l 62 m 63 n 64 o 65 p 66 q 67 r 68 s 69 t 70 u 71 v 72 w 73 x 74 y 75 z -n 顯示行號(hào) ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat -n char.txt
2 1 a
3 2 b
4 3 c
5 4 d
6 5 e
7 6 f
8 7 g
9 8 h
10 9 i
11 10 j
12 11 k
13 12 l
14 13 m
15 14 n
16 15 o
17 16 p
18 17 q
19 18 r
20 19 s
21 20 t
22 21 u
23 22 v
24 23 w
25 24 x
26 25 y
27 26 z
通過(guò)cat命令清空文件內(nèi)容 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat /dev/null>test.txt 2 mengxialeideMBP:mxl mengxianglei$ cat test.txt 3 mengxialeideMBP:mxl mengxianglei$ 通過(guò)cat 非交互式寫(xiě)入文件內(nèi)容,如果結(jié)尾<<-EOF,最后的EOF可以不頂格 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat >>test.txt<<EOF
2 > aaa
3 > ddd
4 > ddd
5 > EOF
6 mengxialeideMBP:mxl mengxianglei$ cat test.txt
7 aaa
8 ddd
9 ddd
22.less 根據(jù)需要加載文件,支持分頁(yè)查看 -N 顯示每頁(yè)行號(hào) 輸入空格,跳轉(zhuǎn)到下一頁(yè),輸入b,返回到上一頁(yè),上下鍵逐行移動(dòng) 23.head head -- display first lines of a file 顯示文件開(kāi)頭幾行 -n 指定顯示的行數(shù) ![]()
1 mengxialeideMBP:mxl mengxianglei$ head -n 11 int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 24.tail tail -- display the last part of a file 顯示文件的后面行數(shù),通常查看日志時(shí)常用該命令監(jiān)控日志文件信息 -n 指定顯示多少行 -f 實(shí)時(shí)監(jiān)控文件變化 -F 實(shí)時(shí)監(jiān)控文件變化,文件不存在是不報(bào)錯(cuò),一直等待 25.cut The cut utility cuts out selected portions of each line (as specified by list) from each file and writes them to the standard output. If no file arguments are specified, or a file argument is a single dash (`-'), cut reads from the standard input. The items specified by list can be in terms of column position or in terms of fields delimited by a special character. Column numbering starts from 1. 從文本中提取文字 -b 以字節(jié)單位進(jìn)行分隔 -c 以字符為單位進(jìn)行分隔 -d 指定分隔符 -f 指定顯示區(qū)域 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat char.txt
2 a b c d e f g h i j k l m n o p q r s t u v w x y z
3 mengxialeideMBP:mxl mengxianglei$ cut -c 1-5 char.txt
4 a b c
5 mengxialeideMBP:mxl mengxianglei$ cut -b 1-5 char.txt
6 a b c
7 mengxialeideMBP:mxl mengxianglei$ cut -b 1-5,7 char.txt
8 a b cd
9 mengxialeideMBP:mxl mengxianglei$ cut -d ' ' -f 2 char.txt
10 b
![]()
1 mengxialeideMBP:mxl mengxianglei$ cut -d : -f 1 /etc/passwd 2 ## 3 # User Database 4 # 5 # Note that this file is consulted directly only when the system is running 6 # in single-user mode. At other times this information is provided by 7 # Open Directory. 8 # 9 # See the opendirectoryd(8) man page for additional information about 10 # Open Directory. 11 ## 12 nobody 13 root 14 daemon 15 _uucp 16 _taskgated 17 _networkd 18 _installassistant 19 _lp 20 _postfix 21 _scsd 22 _ces 23 _appstore 24 _mcxalr 25 _appleevents 26 _geod 27 _serialnumberd 28 _devdocs 29 _sandbox 30 _mdnsresponder 26.split split -- split a file into pieces 將大文件分割為小軟件 -l 指定分隔的行數(shù) ![]()
1 mengxialeideMBP:mxl mengxianglei$ wc -l int.txt
2 100 int.txt
3 mengxialeideMBP:mxl mengxianglei$ split -l 30 int.txt int_s_
4 mengxialeideMBP:mxl mengxianglei$ ls -l
5 total 40
6 -rw-r--r-- 1 mengxianglei staff 292 11 5 00:02 int.txt
7 -rw-r--r-- 1 mengxianglei staff 81 11 5 00:12 int_s_aa
8 -rw-r--r-- 1 mengxianglei staff 90 11 5 00:12 int_s_ab
9 -rw-r--r-- 1 mengxianglei staff 90 11 5 00:12 int_s_ac
10 -rw-r--r-- 1 mengxianglei staff 31 11 5 00:12 int_s_ad
-a 指定文件擴(kuò)展名長(zhǎng)度 ![]()
1 mengxialeideMBP:mxl mengxianglei$ split -l 10 -a 5 int.txt 2 mengxialeideMBP:mxl mengxianglei$ ls -l 3 total 88 4 -rw-r--r-- 1 mengxianglei staff 292 11 5 00:02 int.txt 5 -rw-r--r-- 1 mengxianglei staff 21 11 5 00:16 xaaaaa 6 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaab 7 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaac 8 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaad 9 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaae 10 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaaf 11 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaag 12 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaah 13 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaai 14 -rw-r--r-- 1 mengxianglei staff 31 11 5 00:16 xaaaaj 27.sort sort -- sort or merge records (lines) of text and binary files -n 按照數(shù)值大小排序 -r 倒敘 -t 指定分隔 -k 指定排序 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat test.txt
2 1 z
3 2 y
4 3 x
5 4 w
6 5 v
7 6 u
8 7 t
9 8 s
10 9 r
11 10 q
12 p
13 o
14 n
15 m
16 l
17 k
18 j
19 i
20 h
21 g
22 f
23 e
24 d
25 c
26 b
27 a
28 mengxialeideMBP:mxl mengxianglei$ sort -t ' ' -k 2 test.txt
29 a
30 b
31 c
32 d
33 e
34 f
35 g
36 h
37 i
38 j
39 k
40 l
41 m
42 n
43 o
44 p
45 1 z
46 10 q
47 2 y
48 3 x
49 4 w
50 5 v
51 6 u
52 7 t
53 8 s
54 9 r
28.uniq The uniq utility reads the specified input_file comparing adjacent lines, and writes a copy of each unique input line to the output_file. If input_file is a single dash (`-') or absent, the standard input is read. If output_file is absent, standard output is used for output. The second and succeeding copies of identical adjacent input lines are not written. Repeated lines in the input will not be detected if they are not adjacent, so it may be necessary to sort the files first. 去除重復(fù)行,只能去除相鄰的重復(fù)行,如果想去除所有重復(fù)行,需要先排序 -c 去除重復(fù)行,統(tǒng)計(jì)出重復(fù)行的個(gè)數(shù) ![]()
1 mengxialeideMBP:mxl mengxianglei$ sort -n int.txt |uniq -c 2 5 1 3 5 2 4 5 3 5 5 4 6 5 5 7 5 6 8 5 7 9 5 8 10 5 9 11 5 10 29.wc wc -- word, line, character, and byte count -l 統(tǒng)計(jì)行數(shù) -L 統(tǒng)計(jì)最長(zhǎng)行長(zhǎng)度 -m 統(tǒng)計(jì)字符數(shù) 30.diff diff - compare files line by line 逐行比較文件 31.tr The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. -d 刪除字符 -s 保留連續(xù)字符的第一個(gè)字符 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat test.txt 2 aafdfdsfsfaaaaafsdfsdffn1122111fsf343 3 mengxialeideMBP:mxl mengxianglei$ tr 'abc' '123' <test.txt 4 11fdfdsfsf11111fsdfsdffn1122111fsf343 5 mengxialeideMBP:mxl mengxianglei$ tr -d 'a' <test.txt 6 fdfdsfsffsdfsdffn1122111fsf343 7 mengxialeideMBP:mxl mengxianglei$ tr -s 'a' <test.txt 8 afdfdsfsfafsdfsdffn1122111fsf343 32.tee tee -- pipe fitting 多重定向 -a 追加 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat a.txt|tee b.txt
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 mengxialeideMBP:mxl mengxianglei$ cat a.txt|tee -a b.txt
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 10
23 mengxialeideMBP:mxl mengxianglei$ cat b.txt
24 1
25 2
26 3
27 4
28 5
29 6
30 7
31 8
32 9
33 10
34 1
35 2
36 3
37 4
38 5
39 6
40 7
41 8
42 9
43 10
34.grep the grep utility searches any given input files, selecting lines thatmatch one or more patterns. -v 排除,顯示不匹配的行 ![]()
1 mengxialeideMBP:mxl mengxianglei$ cat a.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:mxl mengxianglei$ grep -v 2 a.txt 13 1 14 3 15 4 16 5 17 6 18 7 19 8 20 9 21 10 -n 顯示匹配行或者行號(hào) -i 不區(qū)分大小寫(xiě) -E 擴(kuò)展的grep ![]()
1 mengxialeideMBP:mxl mengxianglei$ grep '1|2' a.txt
2 mengxialeideMBP:mxl mengxianglei$ grep -E '1|2' a.txt
3 1
4 2
5 10
--color=auto -w 只匹配過(guò)濾的單詞 去除空行和日志 35.sed the sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. 添加內(nèi)容指定行前增加內(nèi)容 sed '2i 2' int.txt 36.awk awk scans each input file for lines that match any of a set of patterns specified literally in prog or in one or more files specified as -f progfile. -F 指定分隔符 NR 行號(hào) NF 尾 $0 整行 輸出指定的行信息 輸出指定行范圍信息 gsub函數(shù)使用 gsub('替換對(duì)象',“替換成什么內(nèi)容”,哪一列) 獲取以太網(wǎng)eth0的ip地址 37.uname uname -- Print operating system name -r 顯示內(nèi)核版本 -m 顯示硬件架構(gòu) -a 顯示所有信息 -n 顯示主機(jī)名 38.hostname hostname -- set or print name of current host system /etc/sysconfig/network /etc/hostname -a顯示主機(jī)別名 -i 主機(jī)IP -I 主機(jī)IP,不依賴DNS解析,速度快 39.stat 40.du 41.date 42.echo |
|
|
來(lái)自: copy_left > 《linux相關(guān)》