歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux命令總結1

Linux命令總結1

日期:2017/3/3 12:11:49   编辑:Linux技術

1,mv (move (rename) files)移動文件或一並改名

參數:

-b, like --backup but does not accept an argument (如果已經有相同的文件,會先備份一個副本)

-f, --force

do not prompt before overwriting (強制執行,覆蓋時不提示)

-i, --interactive (覆蓋前提示)

prompt before overwrite

-n, --no-clobber

do not overwrite an existing file

If you specify more than one of -i, -f, -n, only the final one takes effect.( 如果你一下指定了多個參數-i,-f,-n只有最後一個生效)

所以:mv不能用混合參數,只能用單一參數

--strip-trailing-slashes

remove any trailing slashes from each SOURCE argument

-S, --suffix=SUFFIX

override the usual backup suffix

-t, --target-directory=DIRECTORY

move all SOURCE arguments into DIRECTORY

-T, --no-target-directory

treat DEST as a normal file

-u, --update

move only when the SOURCE file is newer than the destination file or when the destination file

is missing

-v, --verbose

explain what is being done

--help display this help and exit

--version

output version information and exit

使用實例:

1)移動文件。

[root@learnlinux test]# ll

total 20

drwxr-xr-x. 2 root root 4096 Apr 28 16:24 11

-rw-r--r--. 1 root root 9 Apr 28 16:54 2

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 a

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 b

drwxr-xr-x. 3 root root 4096 Apr 28 14:41 c

[root@learnlinux test]# cat 2

safhafdj

[root@learnlinux test]# mv 2 3 (移動文件2並改名為3)

[root@learnlinux test]# ll

total 20

drwxr-xr-x. 2 root root 4096 Apr 28 16:24 11

-rw-r--r--. 1 root root 9 Apr 28 16:54 3

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 a

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 b

drwxr-xr-x. 3 root root 4096 Apr 28 14:41 c

[root@learnlinux test]# cat 3

safhafdj

2)移動目錄。

[root@learnlinux test]# ll

total 20

-rw-r--r--. 1 root root 9 Apr 28 16:54 3

drwxr-xr-x. 2 root root 4096 Apr 28 14:41 a

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d ( a,b,c,d為目錄)

[root@learnlinux test]# mv a b (移動a到b,當b目錄存在時,a會移動到b目錄下面。當b目錄不存在時,a目錄改名為b)

[root@learnlinux test]# ll

total 16

-rw-r--r--. 1 root root 9 Apr 28 16:54 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cd b

[root@learnlinux b]# ll

total 4

drwxr-xr-x. 2 root root 4096 Apr 28 14:41 a

3)當文件同名時,用-b會復制個文件副本。

[root@learnlinux test]# ll

total 20

-rw-r--r--. 1 root root 4 Apr 29 14:34 1

-rw-r--r--. 1 root root 9 Apr 28 16:54 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 1

234

[root@learnlinux test]# cat 3

safhafdj

[root@learnlinux test]# mv -b 1 3

mv: overwrite `3'? y (提示:是否要覆蓋y,n取消本次移動)

[root@learnlinux test]# ll

total 20

-rw-r--r--. 1 root root 4 Apr 29 14:34 3

-rw-r--r--. 1 root root 9 Apr 28 16:54 3~ (這個是文件3的副本)

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 3

234

[root@learnlinux test]# cat 3~

safhafdj

4)-f,強行覆蓋,不提示。

[root@learnlinux test]# ll

total 20

-rw-r--r--. 1 root root 4 Apr 29 14:34 1

-rw-r--r--. 1 root root 9 Apr 28 16:54 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 1

234

[root@learnlinux test]# cat 3

safhafdj

[root@learnlinux test]# mv -f 1 3 (沒有提示)

[root@learnlinux test]# ll

total 16

-rw-r--r--. 1 root root 4 Apr 29 14:34 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 3

234 (3的內容已經被覆蓋成1的內容)

5)-i,覆蓋時提示是否要覆蓋。

-rw-r--r--. 1 root root 4 Apr 29 14:58 1

-rw-r--r--. 1 root root 4 Apr 29 14:34 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 3

234

[root@learnlinux test]# echo jhdf >>1

[root@learnlinux test]# cat 1

123

jhdf

[root@learnlinux test]# cat 3

234

[root@learnlinux test]# mv -i 1 3

mv: overwrite `3'? y

[root@learnlinux test]# ll

total 16

-rw-r--r--. 1 root root 9 Apr 29 14:59 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 3

123

jhdf

系統默認已經將命令設置了別名mv = mv -i 所以前面移動時不加-i也會提示

[root@learnlinux test]# alias

alias cp='cp -i'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i' (已經設置了,所以輸入mv等於輸mv -i了)

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

2,touch (- change file timestamps)改變文件時間戳 (還有創建文檔的意思)

參數:

-a change only the access time (僅僅改變訪問時間)

-c, --no-create (不創建任何文件)

do not create any files

-d, --date=STRING

parse STRING and use it instead of current time (解析字符串並用它代替當前時間)

-f (ignored)

-h, --no-dereference

affect each symbolic link instead of any referenced file (useful

only on systems that can change the timestamps of a symlink)

-m change only the modification time

-r, --reference=FILE

use this fileas times instead of current time

-t STAMP

use [[CC]YY]MMDDhhmm[.ss] instead of current time

--time=WORD

change the specified time: WORD is access, atime, or use: equiv-

alent to -a WORD is modify or mtime: equivalent to -m

--help display this help and exit

--version

output version information and exit

使用實例:

1)當文件存在時,僅僅改變文檔時間。

[root@learnlinux test]# ll

total 16

-rw-r--r--. 1 root root 9 Apr 29 14:59 3 (時間為14:59)

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# touch 3

[root@learnlinux test]# ll

total 16

-rw-r--r--. 1 root root 9 Apr 29 15:11 3 (時間變成15:11)

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# cat 3

123

jhdf

2)當文件不存在時,就創建文件。

[root@learnlinux test]# touch test.txt

[root@learnlinux test]# ll

total 16

-rw-r--r--. 1 root root 0 Apr 29 15:12 1

-rw-r--r--. 1 root root 9 Apr 29 15:11 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

-rw-r--r--. 1 root root 0 Apr 29 15:28 test.txt

NAME

cat - concatenate files and print on the standard output 連接到文件並打印到標准輸出。(查看文件內容)

SYNOPSIS

cat [OPTION]... [FILE]...

DESCRIPTION

Concatenate FILE(s), or standard input, to standard output.

-A, --show-all

equivalent to -vET (等同於-vET)

-b, --number-nonblank

number nonempty output lines (為查看文本添加行號,空行不加行號)

-e equivalent to -vE

-E, --show-ends

display $ at end of each line

-n, --number

number all output lines (給查看的內容添加行號,空行也有行號)

-s, --squeeze-blank

suppress repeated empty output lines

-t equivalent to -vT

-T, --show-tabs

display TAB characters as ^I

-u (ignored)

-v, --show-nonprinting

use ^ and M- notation, except for LFD and TAB

--help display this help and exit

--version

output version information and exit

使用實例:

1)查看文件內容

[root@learnlinux test]# ll

total 20

-rw-r--r--. 1 root root 52 Apr 29 15:51 1

-rw-r--r--. 1 root root 9 Apr 29 15:11 3

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

-rw-r--r--. 1 root root 0 Apr 29 15:28 test.txt

[root@learnlinux test]# cat 1

yrwe

rtur4

36

264327

327

6

347

32467

7426

2365

2)查看內容,並為內容添加行號,空行不加行號

[root@learnlinux test]# cat -b 1

1 yrwe

2 rtur4

3 36

4 264327

5 327

6 6

7 347

8 32467

9 7426

10 2365

3)查看內容,並為內容添加行號,空行也添加行號

[root@learnlinux test]# cat -n 1

1 yrwe

2 rtur4

3 36

4 264327

5

6 327

7 6

8 347

9 32467

10

11 7426

12 2365

13

14

15

4)打入新內容到文檔,並覆蓋原來內容

[root@learnlinux test]# cat 1.txt

yrwe

rtur4

36

264327

327

6

347

32467

7426

2365

[root@learnlinux test]# cat > 1.txt <<eof (>為重定向,>>為追加重定向,eof為開始和結束符,任意字符都可以,但開始和結尾必須相同)

> aabc

> shag

> jsahgj (為輸入的內容)

> sahfj

> eof

[root@learnlinux test]# cat 1.txt

aabc

shag

jsahgj

sahfj

5)在文檔末端增加文本

[root@learnlinux test]# cat >>1.txt <<eof

> 1234

> 35436

> 165464

> 7547 (為輸入內容)

> 754

> eof

[root@learnlinux test]# cat 1.txt

aabc

shag

jsahgj

sahfj

1234

35436

165464

7547

754

3,rm - remove files or directories(刪除文件或目錄)

參數:

-f, --force

ignore nonexistent files, never prompt (忽略不存在的文件,不提示)

-i prompt before every removal (每次刪除前提示)

-I prompt once before removing more than three

files, or when removing recursively. Less

intrusive than -i, while still giving protection

against most mistakes

-r, -R, --recursive

remove directories and their contents recur-sively (遞歸地刪除目錄及其內容)

-v, --verbose

explain what is being done (解釋正在做的事情)

1)刪除文件。

[root@learnlinux test]# rm -v test.txt (-v輸出刪除信息)

removed `test.txt'

[root@learnlinux test]# ll

[root@learnlinux test]# ll

total 12

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

-rw-r--r--. 1 root root 0 Apr 29 16:57 test.txt

[root@learnlinux test]# rm -i test.txt (-i提示並問要刪除文件嗎?y確定,n取消)

rm: remove regular empty file `test.txt'? y

[root@learnlinux test]# ll

total 12

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29

[root@learnlinux test]# ll

total 12

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

-rw-r--r--. 1 root root 0 Apr 29 16:58 test.txt

[root@learnlinux test]# rm -f test.txt (-f強制刪除)

[root@learnlinux test]# ll

total 12

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

2)刪除目錄,要加-r參數

[root@learnlinux b]# rm b (沒有這個b目錄時,刪除目錄b,會提示,如下面)

rm: cannot remove `b': No such file or directory (rm:無法刪除` b”:沒有這樣的文件或目錄 。因為當前在目錄b下,而b目錄下並沒有b目錄)

[root@learnlinux test]# ll

total 12

drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d

[root@learnlinux test]# rm b

[root@learnlinux test]# rm b (不加參數-r時,會提示)

rm: cannot remove `b': Is a directory (rm:不能刪除`b':是一個目錄)

[root@learnlinux test]# rm -r b (加-r後,會立即刪除)

[root@learnlinux test]# ll

total 8

drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c

drwxr-xr-x. 2 root root 4096 Apr 28 14:29

注意:rm命令要慎用,一旦刪除,數據就會丟失。所以用該命令時,要先備份數據。切記,切記!!

4,rmdir -remove empty directories(刪除空目錄)

[root@learnlinux aa]# ll

total 8

drwxr-xr-x. 2 root root 4096 Apr 30 14:26 test1

drwxr-xr-x. 2 root root 4096 Apr 30 14:25 test2

[root@learnlinux aa]# cd test1

[root@learnlinux test1]# ll

total 0

-rw-r--r--. 1 root root 0 Apr 30 14:26 1

-rw-r--r--. 1 root root 0 Apr 30 14:26 2

-rw-r--r--. 1 root root 0 Apr 30 14:26 3

[root@learnlinux test1]# cd ../test2

[root@learnlinux test2]# ll

total 0

[root@learnlinux test2]# cd ../

[root@learnlinux aa]# rmdir test1

rmdir: failed to remove `test1': Directory not empty

[root@learnlinux aa]# rmdir test2

[root@learnlinux aa]# ll

total 4

drwxr-xr-x. 2 root root 4096 Apr 30 14:26 test1

5, head - output the first part of files(輸出文件前面一部分)-----默認顯示文件前十行

[root@learnlinux ~]# cat test.txt

ekwlksdagl

sajgkjsagjs

249u49ajtu49

833t3 jsjsdfg

agkjkjs02393-e

faskjgjht934u

ashrkelyk23

92390-5035-=23943959

jastihioerooeeeeeejds

asjdfffffffks

2388888888883

rasjtgjijgtijgag

[root@learnlinux ~]# head test.txt

ekwlksdagl

sajgkjsagjs

249u49ajtu49

833t3 jsjsdfg

agkjkjs02393-e

faskjgjht934u

ashrkelyk23

92390-5035-=23943959

jastihioerooeeeeeejds

asjdfffffffks

[root@learnlinux ~]# head -5 test.txt

ekwlksdagl

sajgkjsagjs

249u49ajtu49

833t3 jsjsdfg

agkjkjs02393-e

[root@learnlinux ~]# head -12 test.txt

ekwlksdagl

sajgkjsagjs

249u49ajtu49

833t3 jsjsdfg

agkjkjs02393-e

faskjgjht934u

ashrkelyk23

92390-5035-=23943959

jastihioerooeeeeeejds

asjdfffffffks

2388888888883

rasjtgjijgtijgag

6,tail - output the last part of files(輸出文件結尾一部分)---默認結尾十行

[root@learnlinux ~]# cat test.txt

ekwlksdagl

sajgkjsagjs

249u49ajtu49

833t3 jsjsdfg

agkjkjs02393-e

faskjgjht934u

ashrkelyk23

92390-5035-=23943959

jastihioerooeeeeeejds

asjdfffffffks

2388888888883

rasjtgjijgtijgag

[root@learnlinux ~]# tail test.txt

833t3 jsjsdfg

agkjkjs02393-e

faskjgjht934u

ashrkelyk23

92390-5035-=23943959

jastihioerooeeeeeejds

asjdfffffffks

2388888888883

rasjtgjijgtijgag

[root@learnlinux ~]# tail -5 test.txt

jastihioerooeeeeeejds

asjdfffffffks

2388888888883

rasjtgjijgtijgag

(空行也顯示)

[root@learnlinux ~]#

7,grep

Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹 配的行打印出來。grep全稱是

Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。

grep的工作方式是這樣的,它在一個或多個文件中搜索字符串模板。如果模板包括空格,則必須被引用,模板後的所有字符串被

看作文件名。搜索的結果被送到標准輸出,不影響原文件內容

grep可用於shell腳本,因為grep通過返回一個狀態值來說明搜索的狀態,如果模板搜索成功,則返回0,如果搜索不成功,則返回

1,如果搜索的文件不存在,則返回2。我們利用這些返回值就可進行一些自動化的文本處理工作。

1.命令格式:

grep [option] pattern file

2.命令功能:

用於過濾/搜索的特定字符。可使用正則表達式能多種命令配合使用,使用上十分靈活

3.命令參數:

-a --text #不要忽略二進制的數據。

-A<顯示行數> --after-context=<顯示行數> #除了顯示符合范本樣式的那一列之外,並顯示該行之後的內容。

-b --byte-offset #在顯示符合樣式的那一行之前,標示出該行第一個字符的編號。

-B<顯示行數> --before-context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前的內容。

-c --count #計算符合樣式的列數。

-C<顯示行數> --context=<顯示行數>或-<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前後的內容。

-d <動作> --directories=<動作> #當指定要查找的是目錄而非文件時,必須使用這項參數,否則grep指令將回報信息並停止動作。

-e<范本樣式> --regexp=<范本樣式> #指定字符串做為查找文件內容的樣式

-E --extended-regexp #將樣式為延伸的普通表示法來使用。

-f<規則文件> --file=<規則文件> #指定規則文件,其內容含有一個或多個規則樣式,讓grep查找符合規則條件的文件

內容,格式為每行一個規則樣式

-F --fixed-regexp #將樣式視為固定字符串的列表。

-G --basic-regexp #將樣式視為普通的表示法來使用

-h --no-filename #在顯示符合樣式的那一行之前,不標示該行所屬的文件名稱。

-H --with-filename #在顯示符合樣式的那一行之前,表示該行所屬的文件名稱。

-i --ignore-case #忽略字符大小寫的差別。

-l --file-with-matches #列出文件內容符合指定的樣式的文件名稱。

-L --files-without-match #列出文件內容不符合指定的樣式的文件名稱。

-n --line-number #在顯示符合樣式的那一行之前,標示出該行的列數編號。

-q --quiet或--silent #不顯示任何信息。

-r --recursive #此參數的效果和指定“-d recurse”參數相同

-s --no-messages #不顯示錯誤信息。

-v --revert-match #顯示不包含匹配文本的所有行

-V --version #顯示版本信息。

-w --word-regexp #只顯示全字符合的列。

-x --line-regexp #只顯示全列符合的列。

-y #此參數的效果和指定“-i”參數相同。

4.規則表達式:

^ #錨定行的開始 如:'^grep'匹配所有以grep開頭的行。

$ #錨定行的結束 如:'grep$'匹配所有以grep結尾的行。

. #匹配一個非換行符的字符 如:'gr.p'匹配gr後接一個任意字符,然後是p。

* #匹配零個或多個先前字符 如:'*grep'匹配所有一個或多個空格後緊跟grep的行。

.* #一起用代表任意字符。

[] #匹配一個指定范圍內的字符,如'[Gg]rep'匹配Grep和grep。

[^] #匹配一個不在指定范圍內的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。

\(..\) #標記匹配字符,如'\(love\)',love被標記為1。

\< #錨定單詞的開始,如:'\<grep'匹配包含以grep開頭的單詞的行。

\> #錨定單詞的結束,如'grep\>'匹配包含以grep結尾的單詞的行。

x\{m\} #重復字符x,m次,如:'0\{5\}'匹配包含5個o的行。

x\{m,\} #重復字符x,至少m次,如:'o\{5,\}'匹配至少有5個o的行。

x\{m,n\} #重復字符x,至少m次,不多於n次,如:'o\{5,10\}'匹配5--10個o的行。

\w #匹配文字和數字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G後跟零個或多個文字或數字字符,然後是p。

\W #\w的反置形式,匹配一個或多個非單詞字符,如點號句號等。

\b #單詞鎖定符,如: '\bgrep\b'只匹配grep。

5. 使用實例:

實例1:查找指定進程

命令:

ps -ef|grep svn

輸出:

[root@learn ~]# ps -ef|grep svn

root 2748 2680 3 15:56 pts/0 00:00:00 grep svn

實例2:查找指定進程個數

命令:

ps -ef|grep -c svn

ps -ef|grep svn -c

輸出:

[root@learn ~]# ps -ef|grep -c svn

1

[root@learn ~]# ps -ef|grep svn -c

1

實例3:從文件中讀取關鍵詞進行搜索

命令:

cat test.txt|grep -f test2.txt

輸出:

[root@learn ~]# cat test.txt

learn linux

show

beatuful

abc

redhat

linux

[root@learn ~]# cat test2.txt

redhat

linux

abcdef

[root@learn ~]# cat test.txt|grep -f test2.txt

learn linux

redhat

linux

說明:

輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行

實例3:從文件中讀取關鍵詞進行搜索 且顯示行號

命令:

cat test.txt|grep -nf test2.txt

輸出:

[root@learn ~]# cat test.txt

learn linux

show

beatuful

abc

redhat

linux

[root@learn ~]# cat test2.txt

redhat

linux

abcdef

[root@learn ~]# cat test.txt|grep -nf test2.txt

1:learn linux

5:redhat

6:linux

說明:

輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行,並顯示每一行的行號

實例4:從文件中查找關鍵詞

命令:

grep “Linux” test.txt

輸出:

[root@learn ~]# grep 'linux' test.txt

learn linux

linux

[root@learn ~]# grep linux test.txt

learn linux

linux

[root@learn ~]# grep "linux" test.txt

learn linux

linux

實例5:從多個文件中查找關鍵詞。

命令:

grep linux test.txt test2.txt

輸出:

[root@learn ~]# grep linux test.txt test2.txt

test.txt:learn linux

test.txt:linux

test2.txt:linux

說明:

多文件時,輸出查詢到的信息內容行時,會把文件名放在查找出來內容行最前面並且加上":"作為標示符

實例7:grep不顯示本身進程

命令:

ps aux|grep \[s]sh

ps aux | grep ssh | grep -v "grep"

輸出:

[root@learn ~]# ps aux|grep ssh

root 810 0.0 0.1 8944 1028 ? Ss 14:03 0:00 /usr/sbin/sshd

root 2650 0.0 0.4 11860 3288 ? Ss 15:28 0:00 sshd: test [priv]

test 2652 0.0 0.2 11860 1548 ? S 15:28 0:00 sshd: test@pts/0

root 2820 2.0 0.0 4356 712 pts/0 S+ 16:44 0:00 grep ssh

[root@learn ~]# ps aux|grep \[s]sh

root 810 0.0 0.1 8944 1028 ? Ss 14:03 0:00 /usr/sbin/sshd

root 2650 0.0 0.4 11860 3288 ? Ss 15:28 0:00 sshd: test [priv]

test 2652 0.0 0.2 11860 1548 ? S 15:28 0:00 sshd: test@pts/0

實例8:找出以字母l開頭的內容

命令:

grep ^l test.txt

輸出:

[root@learn ~]# grep ^l test.txt

learn linux

linux

實例9:找出非以字母l開頭的內容

命令:

grep ^[^l] test.txt

輸出:

[root@learn ~]# cat test.txt

learn linux

show

beatuful

abc

redhat

linux

[root@learn ~]# grep ^[^l] test.txt

show

beatuful

abc

redhat

實例10:找出以linux結尾的行內容

命令:

grep linux$ test.txt

輸出:

[root@learn ~]# cat test.txt

learn linux

show

beatuful

abc

redhat

linux

[root@learn ~]# grep linux$ test.txt

learn linux

linux

[root@learn ~]# cat test.txt|grep linux$

learn linux

linux

實例11:顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字符串至少有7個連續小寫字符的字符串的行

命令:

grep '[a-z]\{7\}' *.txt

輸出:

[root@learn ~]# grep '[a-z]\{7\}' *.txt

test2.txt:abcdefghf

test.txt:beatuful

[root@learn ~]#

實例12:顯示包含ed或者at字符的內容行

命令:

grep -E "ed|at" test.txt

輸出:

[root@learn ~]# grep -E "ed|at" test.txt

beatuful

redhat

[root@learn ~]# grep -nE "ed|at" test.txt

3:beatuful

5:redhat

說明:

n給查出的行加上行號。

實例13:不區分大小寫查找內容

命令:

grep -i B test.txt

輸出

[root@learn ~]# cat test.txt

learn linux

ABCDE

aVIlin

show

beatuful

abc

redhat

linux

[root@learn ~]# grep -i B test.txt

ABCDE

beatuful

abc

[root@learn ~]# grep B test.txt

ABCDE

8,sed----stream editor for filtering and transforming text用於篩選和轉換文本的流編輯器

命令格式:

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

參數:

-n, --quiet, --silent (取消默認輸出)

suppress automatic printing of pattern space

-e script, --expression=script (允許多點編輯)

add the script to the commands to be executed

-f script-file, --file=script-file

add the contents of script-file to the commands to be executed將腳本文件的內容添加到要執行的命令

--follow-symlinks

follow symlinks when processing in place; hard links will still be

broken.

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied). The

default operation mode is to break symbolic and hard links. This

can be changed with --follow-symlinks and --copy.

-c, --copy

use copy instead of rename when shuffling files in -i mode. While

this will avoid breaking links (symbolic or hard), the resulting

editing operation is not atomic. This is rarely the desired mode;

--follow-symlinks is usually enough, and it is both faster and more

secure.

-l N, --line-length=N

specify the desired line-wrap length for the ala command

--posix

disable all GNU extensions.

-r, --regexp-extended

use extended regular expressions in the script.

-s, --separate

consider files as separate rather than as a single continuous long

stream.

-u, --unbuffered

使用實例:

實例1:取消默認輸出。

命令:

sed -n /ab/p test.txt

輸出:

[root@learn ~]# sed /ab/p test.txt

learn linux

ABCDE

aVIlin

show

beatuful

abc

abc

redhat

linux

[root@learn ~]# sed -n /ab/p test.txt

abc

實例2:允許多點編輯 同時刪除含ab,A行的內容。

命令:

sed -e /ab/d -e /A/d test.txt

輸出:

[root@learn ~]# cat test.txt

learn linux

ABCDE

aVIlin

show

beatuful

abc

redhat

linux

[root@learn ~]# sed -e /ab/d -e /A/d test.txt

learn linux

aVIlin

show

beatuful

redhat

linux

實例3:替換內容 將abc替換為123456789

命令:

sed -i s#abc#123456789#g test.txt

輸出:

[root@learn ~]# sed s#abc#123456789#g test.txt (沒有加-i參數時,只是臨時更改打印出來)

learn linux

ABCDE

aVIlin

show

beatuful

123456789

redhat

linux

[root@learn ~]# cat test.txt

learn linux

ABCDE

aVIlin

show

beatuful

abc (abc還在,所以並沒有替換)

redhat

linux

[root@learn ~]# sed -i s#abc#123456789#g test.txt (加-i後,直接更改文檔內容)

[root@learn ~]# cat test.txt

learn linux

ABCDE

aVIlin

show

beatuful

123456789

redhat

linux

實例4:打印奇數行或偶數行

命令:

sed -n 'p;n' test.txt 或 sed -n '1~2p' test.txt 奇數

sed -n 'n;p' test.txt 或 sed -n '2~2p' test.txt 偶數

輸出:

[root@learn ~]# cat -n test.txt

1 learn linux

2 ABCDE

3 aVIlin

4 show

5 beatuful

6 abc

7 abc

8 redhat

9 linux

[root@learn ~]# sed -n 'p;n' test.txt

learn linux

aVIlin

beatuful

abc

linux

[root@learn ~]# sed -n 'n;p' test.txt

ABCDE

show

abc

redhat

[root@learn ~]# sed -n '1~2p' test.txt

learn linux

aVIlin

beatuful

abc

linux

[root@learn ~]# sed -n '2~2p' test.txt

ABCDE

show

abc

redhat

本文出自 “永不止步” 博客,請務必保留此出處http://learningwm.blog.51cto.com/10658232/1785569

Copyright © Linux教程網 All Rights Reserved