初識50個Linux命令


1. 【命令】:cat

【功能說明】:

concatenate files and print on the standard output #連接文件並打印到標准輸出,有標准輸出的都可以用重定向定向導入到文件里面

【語法格式】:

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

【選項參數】:

參數

說明

簡解

-b,--number-nonblank

number nonempty output lines

非空輸出行編號

-n,--number

number all outputnlines

所有行輸出編號

【示例】:

1、基本用法:查看文件內容

[root@oldboy ~]# cat /etc/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

 

2、把log1.txt的文件內容加上行號輸入log2.txt這個文件里

[root@oldboy ~]# cat -n log1.txt

     1  2012-1

     2  2013-1

     3  2014-1

     4

     5

[root@oldboy ~]# cat -n log1.txt>log2.txt

[root@oldboy ~]# cat log2.txt

     1  2012-1

     2  2013-1

     3  2014-1

     4

     5

 

3、把文件log1.txt里面的文件空格不顯示行號加入log2.txt

[root@oldboy ~]# cat -n log1.txt

     1  2012-1

     2  2013-1

     3  2014-1

     4

     5

[root@oldboy ~]# cat -b log1.txt

     1  2012-1

     2  2013-1

     3  2014-1

 

 

[root@oldboy ~]# cat -b log1.txt>log2.txt

[root@oldboy ~]# cat log2.txt

     1  2012-1

     2  2013-1

     3  2014-1

 

4、同時顯示log1.txtlog2.txt

[root@oldboy ~]# cat log1.txt log2.txt

2012-1

2013-1

2014-1

 

 

     1  2012-1

     2  2013-1

     3  2014-1

 

5、使用cat編輯文檔內容

[root@oldboy ~]# cat >log1.txt<<EOF

> 2012-3

> 2013-3

> 2014-3

> EOF

[root@oldboy ~]# cat log1.txt

2012-3

2013-3

2014-3

 

6、使用cat追加編輯文檔內容

[root@oldboy data]# cat >>oldboy.txt<<EOF

> ni hao

> jin tian tian qi zhen hao

> EOF

[root@oldboy data]# cat oldboy.txt

I am oldboy

I am study linux

ni hao

jin tian tian qi zhen hao

 

 

2. 【命令】:cd

【功能說明】:

Change the current directory to DIR #更改當前路徑為DIR

【語法格式】:

cd [-L|-P] [dir]

【選項參數】:

參數

說明

簡解

-L

force symbolic links to be followed

 

-P

use the physical directory structure without following symbolic

 

-

 

 

【示例】:

1、基本用法:更改目錄路徑到根目錄

[root@oldboy ~]# cd /

[root@oldboy /]#

 

2、基本用法:更改目錄路徑到上級目錄

[root@oldboy ~]# cd ..

[root@oldboy /]#

 

3、更改目錄路徑到上一次使用的目錄

[root@oldboy /]# cd -

/root

[root@oldboy ~]#

 

 

3. 【命令】:mkdir

【功能說明】:

make directories   #創建目錄

【語法格式】:

mkdir [OPTION]... DIRECTORY...

【選項參數】:

參數

說明

簡解

-m, --mode

set file mode (as in chmod), not a=rwx - umask

設定權限<模式>(類似chmod),而不是rwxrwxrwxumask

-p, --parents

no error if existing, make parent directories as needed

遞歸創建目錄

-v, --verbose

print a message for each created directory

每次創建新目錄都顯示信息

-Z

set the SELinux security context of each created directory to CTX

 

【示例】:

1、基本用法:創建目錄

[root@oldboy ~]# mkdir /data

[root@oldboy ~]# cd /data

[root@oldboy data]# pwd

/data

 

2、遞歸創建多個目錄

[root@oldboy data]# mkdir -p /oldboy/oldboy

[root@oldboy data]# cd /oldboy/oldboy

[root@oldboy oldboy]# pwd

/oldboy/oldboy

 

 

 

4. 【命令】:ls

【功能說明】:

list directory contents   #列出目標目錄中所有的子目錄和文件

【語法格式】:

ls [OPTION]... [FILE]...

【選項參數】:

參數

說明

簡解

-a, --all

do not ignore entries starting with.

列出目錄下的所有文件

-A, --almost-all

do not list implied.and ..

-a,但不列出“.”(表示當前目錄)“..”(表示當前目錄的父目錄)

-b, --escape

print octal escapes for nongraphic characters

把文件名中不可輸出的字符用反斜杠加字符編號的形式列出

-B, --ignore-backups

do not list implied entries ending with ~

不輸出以“~”結尾的備份文件

-c

sort by ctime

配合-lt;根據ctime排序及顯示ctime(文件狀態最后更改的時間)配合-l:顯示ctime但根據名稱排序否則:根據ctime排序

-C

list entries by clumns

每欄由上至下列出項目

-d, --directory

list directory entries instead of contentsm,and do not dereference symbolic links

將目錄象文件一樣顯示,而不是顯示其下的文件

-D, --dired

generate output designed for Emacs’ dired mode

產生適合Emacsdired模式使用的結果

-f

do not sort,enable -aU,disable -ls --color

對輸出的文件不進行排序,-aU選項生效,-1st選項失效

-F --classify

append indicator(one of */=>@|)to entries

在每個文件名后附上一個字符以說明該文件的類型,“*”表示可執行的普通
文件;“/”表示目錄;“@”表示符號鏈接;“|”表示FIFOs;“=”表示套
接字(sockets)

-g

like -l,but do not list owner

類似-l,但不列出所有者

-h, --human-readable

with -l,print sizes in human readable format

以容易理解的格式列出文件大小

-H, --dereference-command-line

follow symbolic links listed on the command line

使用命令列中的符號鏈接指示的真正目的地

-i, --inode

print the index number of each file

打印出每個文件的inode

-I,--ignore=PATTERN

do not list implied entries matching shell PATTERN

不打印出任何符合shell萬用字符<樣式>的項目

-k

like --block-size=1K

k字節的形式表示文件的大小

-l

use a long listing format

除了文件名之外,還將文件的權限、所有者、文件大小等信息詳細列出來

-L,--dereference

when showing file information for a symbolic link,show information for the file the link references rather than for the link itself

當顯示符號鏈接的文件信息時,顯示符號鏈接所指示的對象而並非符號鏈接本身的信息。

-m

fill width with a comma separated list of entries

所有項目以逗號分隔,並填滿整行行寬

-n, --numeric-uid-gid

like -l,but list numeric user and group IDs

用數字的UIDGID代替名稱

-N, --literal

print raw entry names

不限制文件長度

-o

like -l,but do not list group information

類似-l,顯示文件的除組信息外的詳細信息

-p, --indicator-style=slash

append / indicator to directories

對目錄添加“/”符號

-q, --hide-control-chars

print ? instead of non graphic characters

用?號代替不可輸出的字符

-Q, --quote-name

enclose entry names in double quotes

把輸出的文件名用雙引號括起來

-r, --reverse

reverse order while sorting

依相反次序排列

-R, --recursive

list subdirectories recursively

同時列出所有子目錄層

-s, --size

print the allocated size of each file,in blocks

以塊大小為單位列出所有文件的大小

-S

sort by file size

根據文件大小排序

-t

sort by modification time

以文件修改時間排序

-T, --tabsize=COLS

assume tab stops at each COLS instead of 8

 

-u

with -lt: sort by, and show, access time with

-l:show access time and sort by name

otherwise: sort by access time

配合-lt:顯示訪問時間而且依訪問時間排序

配合-l:顯示訪問時間但根據名稱排序

其他:根據訪問時間排序

-U

do not sort; list entries in directory order

不進行排序;依文件系統原有的次序列出項目

-v

natural sort of (version) numbers within text

根據版本進行排序

-w, --width=COLS

assume screen width instead of current value

自行指定屏幕寬度而不使用目前的數值

-x

list entries by lines instead of by columns

逐行列出項目而不是逐欄列出

-X

sort alphabetically by entry extension

根據擴展名排序

-1

list one file per line

每行只列出一個文件

 

 

【示例】:

1、基本用法:查看目錄內容

[root@oldboy ~]# ls

anaconda-ks.cfg  data  install.log  install.log.syslog  log1.txt  log2.txt  test.txt

 

2、基本用法:查看目錄全部內容(包含隱藏文件)

[root@oldboy ~]# ls -la

total 88

dr-xr-x---.  3 root root  4096 Mar  7 18:16 .

dr-xr-xr-x. 24 root root  4096 Mar  9 17:00 ..

-rw-------.  1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

-rw-------.  1 root root  5003 Mar  9 04:13 .bash_history

-rw-r--r--.  1 root root    18 May 20  2009 .bash_logout

-rw-r--r--.  1 root root   176 May 20  2009 .bash_profile

-rw-r--r--.  1 root root   176 Sep 23  2004 .bashrc

-rw-r--r--.  1 root root   100 Sep 23  2004 .cshrc

drwxr-xr-x.  3 root root  4096 Mar  9 01:47 data

-rw-r--r--.  1 root root 22179 Mar  3 23:14 install.log

-rw-r--r--.  1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r--r--.  1 root root    18 Mar  7 18:13 log1.txt

-rw-r--r--.  1 root root    44 Mar  7 17:53 log2.txt

-rw-r--r--.  1 root root   129 Dec  4  2004 .tcshrc

-rw-r--r--.  1 root root     0 Mar  7 18:27 test.txt

 

3、基本用法:查看目錄(使用長模式)

[root@oldboy ~]# ls -l

total 52

-rw-------. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 3 root root  4096 Mar  9 01:47 data

-rw-r--r--. 1 root root 22179 Mar  3 23:14 install.log

-rw-r--r--. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r--r--. 1 root root    18 Mar  7 18:13 log1.txt

-rw-r--r--. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r--r--. 1 root root     0 Mar  7 18:27 test.txt

 

5. 【命令】:pwd

【功能說明】:

print name of current/working directory   #打印當前目錄路徑

【語法格式】:

pwd [OPTION]

【選項參數】:

參數

說明

簡解

-L, --logical

use PWD from environment,even if it contains symlinks

當目錄為鏈接路徑時,顯示鏈接路徑

-P, --physical

avoid all symlinks

顯示實際物理路徑,而非使用鏈接

【示例】:

1、基本用法:顯示當前目錄所在路徑

[root@oldboy ~]# pwd

/root

[root@oldboy ~]#

 

2、顯示當前目錄的物理路徑

[root@oldboy ~]# cd /etc/init.d

[root@oldboy init.d]# pwd -P

/etc/rc.d/init.d

 

3、顯示當前目錄的鏈接路徑

[root@oldboy ~]# cd /etc/init.d

[root@oldboy init.d]# pwd -L

/etc/init.d

 

 

6. 【命令】:touch

【功能說明】:

change file timestamps   #創建文件或改變文件的時間戳

【語法格式】:

pwd [OPTION]... FILE...

【選項參數】:

參數

說明

簡解

-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 files 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: equivalent to -a WORD is modify or mtime: equivalent to -m

使用WORD指定的時間:accessatimeuse都等於-a選項的效果,而modifymtime等於-m選項的效果

【示例】:

1、基本用法:創建新文件

[root@oldboy data]# touch log2012.log log2013.log

[root@oldboy data]# ll

total 0

-rw-r--r--. 1 root root 0 Mar  9 17:48 log2012.log

-rw-r--r--. 1 root root 0 Mar  9 17:48 log2013.log

 

2、不創建不存在的文件

[root@oldboy data]# touch -c log2014.log

[root@oldboy data]# ll

total 0

-rw-r--r--. 1 root root 0 Mar  9 17:48 log2012.log

-rw-r--r--. 1 root root 0 Mar  9 17:48 log2013.log

 

3、更新log2012.log的時間和log.log時間戳相同

[root@oldboy data]# touch log.log

[root@oldboy data]# ll

total 0

-rw-r--r--. 1 root root 0 Mar  9  2016 log2012.log

-rw-r--r--. 1 root root 0 Mar  9  2016 log2013.log

-rw-r--r--. 1 root root 0 May 23 00:00 log.log

[root@oldboy data]# touch -r log.log log2012.log

[root@oldboy data]# ll

total 0

-rw-r--r--. 1 root root 0 May 23 00:00 log2012.log

-rw-r--r--. 1 root root 0 Mar  9  2016 log2013.log

-rw-r--r--. 1 root root 0 May 23 00:00 log.log

 

4、設定文件時間戳

[root@oldboy data]# touch -t 201211142234.50 log2016.log

[root@oldboy data]# ll

total 0

-rw-r--r--. 1 root root 0 Nov 14  2012 log2016.log

 

【說明】:

     -t  time 使用指定的時間值 time 作為指定文件相應時間戳記的新值.此處的 time規定為如下形式的十進制數:      

      [[CC]YY]MMDDhhmm[.SS]     

      這里,CC為年數中的前兩位,即”世紀數”;YY為年數的后兩位,即某世紀中的年數.如果不給出CC的值,則touch   將把年數CCYY限定在1969--2068之內.MM為月數,DD為天將把年數CCYY限定在1969--2068之內.MM為月數,DD為天數,hh 為小時數(幾點)mm為分鍾數,SS為秒數.此處秒的設定范圍是0--61,這樣可以處理閏秒.這些數字組成的時間是環境變量TZ指定的時區中的一個時 間.由於系統的限制,早於197011日的時間是錯誤的。

 

7. 【命令】:echo

【功能說明】:

display a line of text   #打印輸出

【語法格式】:

echo [SHORT-OPTION]... [STRING]...

echo LONG-OPTION

【選項參數】:

參數

說明

簡解

-n

do not output the trailing newline

不要在最后自動換行

-e

enable interpretation of backslash escapes

若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出

  \a 發出警告聲;
   \b 刪除前一個字符;
   \c 最后不加上換行符號;
   \f 換行但光標仍舊停留在原來的位置;
   \n 換行且光標移至行首;
   \r 光標移至行首,但不換行;
   \t 插入tab
   \v \f相同;
   \\ 插入\字符;
   \nnn 插入nnn(八進制)所代表的ASCII字符;

-E

disable interpretation of backslash escapes (default)

不解釋轉意字符

【示例】:

1、基本用法:打印內容

[root@oldboy data]# echo "I am studying linux."

I am studying linux.

 

2、打印變量內容

[root@oldboy data]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

 

 

8. 【命令】:xargs

【功能說明】:

build and execute command lines from standard input  #從標准輸入獲取內容創建和執行命令

【語法格式】:

xargs  [-0prtx]  [-E  eof-str]  [-e[eof-str]]  [--eof[=eof-str]] [--null] [-d delimiter]

       [--delimiter delimiter]  [-I  replace-str]  [-i[replace-str]]  [--replace[=replace-str]]

       [-l[max-lines]]  [-L max-lines] [--max-lines[=max-lines]] [-n max-args] [--max-args=max-

       args] [-s  max-chars]  [--max-chars=max-chars]  [-P  max-procs]  [--max-procs=max-procs]

       [--interactive]     [--verbose]     [--exit]    [--no-run-if-empty]    [--arg-file=file]

       [--show-limits] [--version] [--help] [command [initial-arguments]]

【選項參數】:

參數

說明

簡解

 

 

 

【示例】:

1、基本用法:與find配合使用,刪除/data目錄下所有.txt文件

[root@oldboy ~]# find /data -type f -name "*.txt"|xargs rm -f

[root@oldboy ~]# cd /data

[root@oldboy data]# ll

total 0

 

 

 

9. 【命令】:mv

【功能說明】:

 move (rename) files #移動或重命名文件

【語法格式】:

mv [OPTION]... [-T] SOURCE DEST

mv [OPTION]... SOURCE... DIRECTORY

mv [OPTION]... -t DIRECTORY SOURCE...

【選項參數】:

參數

說明

簡解

--backup[=CONTROL]

make a backup of each existing destination file

備份模式:若需覆蓋文件,則覆蓋前先行備份

-b

like --backup but does not accept an argument

若需覆蓋文件,則覆蓋前先行備份。

-f, --force

do not prompt before overwriting

force 強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋

-i, --interactive

prompt before overwrite

若目標文件 (destination) 已經存在時,就會詢問是否覆蓋

-n, --no-clobber

do not overwrite an existing file

 

-S, --suffix=SUFFIX

override the usual backup suffix

為備份文件指定后綴,而不使用默認的后綴;

-t, --target-directory=DIRECTORY

move all SOURCE arguments into DIRECTORY

指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的情況,此時目標目錄在前,源文件在后

-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

若目標文件已經存在,且 source 比較新,才會更新(update)

-v, --verbose

explain what is being done

顯示程序執行過程

【示例】:

1、基本用法:移動文件

[root@oldboy ~]# mv ett.txt /root/data

[root@oldboy ~]# cd data

[root@oldboy data]# ll

total 4

-rw-r--r--. 1 root root 292 Mar  9 13:28 ett.txt

 

2、基本用法:移動目錄

[root@oldboy ~]# mv /data /root

[root@oldboy ~]# ll

total 56

-rw-------. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 2 root root  4096 Mar  9 13:56 data

-rw-r--r--. 1 root root 22179 Mar  3 23:14 install.log

 

3、更改文件名

[root@oldboy data]# ll

total 4

-rw-r--r--. 1 root root 292 Mar  9 13:28 ett.txt

[root@oldboy data]# mv ett.txt oldboy.txt

[root@oldboy data]# ll

total 4

-rw-r--r--. 1 root root 292 Mar  9 13:28 oldboy.txt

 

4、多個文件移動到同一個目錄

[root@localhost test3]# mv -t /opt/soft/test/test4/ log1.txt log2.txt  log3.txt 

[root@localhost test3]# cd ..

[root@localhost test]# cd test4/

[root@localhost test4]# ll

總計 12

-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt

-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

 

 

10. 【命令】:rm

【功能說明】:

 remove files or directories #移除文件或目錄

【語法格式】:

rm [OPTION]... FILE...

【選項參數】:

參數

說明

簡解

-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 recursively

指示rm將參數中列出的全部目錄和子目錄均遞歸地刪除

-v, --verbose

explain what is being done

顯示命令執行過程

【示例】:

1、基本用法:刪除文件

[root@oldboy ~]# rm log1.txt

rm: remove regular file `log1.txt'? y

[root@oldboy ~]# ll

total 52

-rw-------. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 2 root root  4096 Mar  9 13:56 data

-rw-r--r--. 1 root root 22179 Mar  3 23:14 install.log

-rw-r--r--. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r--r--. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r--r--. 1 root root    18 Mar  9 13:19 oldboy.txt

 

2、基本用法:刪除目錄

[root@oldboy data]# rm -r a

rm: descend into directory `a'? y

rm: descend into directory `a/b'? y

rm: remove regular empty file `a/b/oldboy.txt'? y

rm: remove directory `a/b/c'? y

rm: remove directory `a/b'? y

rm: remove regular empty file `a/oldboy.txt'? y

rm: remove directory `a'? y

 

11. 【命令】:find

【功能說明】:

 search for files in a directory hierarchy #查找文檔

【語法格式】:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

【選項參數】:

參數或選項

說明

簡解

-atime n

File was last accessed n*24 hours ago.  When find figures out  how  many  24-hour periods  ago  the  file  was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

根據最后一次的訪問時間查找文件

-ctime n

Files status was last changed n*24 hours ago.  See the comments  for  -atime  to understand how rounding affects the interpretation of file status change times.

根據最后一次的改變時間查找文件

-mtime n

Files data was last modified n*24 hours ago.  See the  comments  for  -atime  to understand how rounding affects the interpretation of file modification times.

根據最后一次的修改時間查找文件

-type c

File is of type c:

b      block (buffered) special

c      character (unbuffered) special

d      directory

p      named pipe (FIFO)

f      regular file

l      symbolic  link;  this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken.  If you want  to  search for symbolic links when -L is in effect, use -xtype.

s      socket

D      door (Solaris)

查找某一類型的文件

b   塊設備文件

d   目錄

c   字符設備文件

p   管道文件

l    符號鏈接文件

f   普通文件

s   socket文件

-name pattern

Base  of  file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharacters (*, ?, and [])  match  a  .’  at  the start  of  the  base name (this is a change in findutils-4.2.2; see section STAN-DARDS CONFORMANCE below).  To ignore a directory and  the  files  under  it,  use -prune; see an example in the description of -path.  Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns.  The filename matching is performed with the use of the fnmatch(3) library function.   Dont forget to enclose the pattern  in quotes in order to protect it from expansion by the shell.

按文件名查找文件

-perm mode

Files  permission  bits  are  exactly  mode (octal or symbolic).  Since an exact match is required, if you want to use this form for symbolic modes, you may  have  to specify a rather complex mode string.  For example -perm g=wwill only match files which have mode 0020 (that is, ones for which group write permission is the only permission set).  It is more likely that you will want to use the /or -forms, for example -perm -g=w, which matches any file with group write  permis- sion.  See the EXAMPLES section for some illustrative examples.

按照文件權限來查找文件

-size n[cwbkMG]

File  uses  n  units  of space.  The following suffixes can be used:

b’    for 512-byte blocks (this is the default if  no  suffix is used)

c’    for bytes

w’    for two-byte words

k’    for Kilobytes (units of 1024 bytes)

M’    for Megabytes (units of 1048576 bytes)

G’    for Gigabytes (units of 1073741824 bytes)

 

按大小查找

【示例】:

1、基本用法:按文件名查找文件

[root@oldboy ~]# find /root/data -name "*.txt"

/root/data/oldboy.txt

 

2、基本用法:按文件類型查找文件

[root@oldboy ~]# find /root/data -type f

/root/data/oldboy.txt

 

3、查找文件名稱不為oldboy.txt的所有文件

[root@oldboy ~]# ll

total 52

-rw-------. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 2 root root  4096 Mar  9 15:09 data

-rw-r--r--. 1 root root 22179 Mar  3 23:14 install.log

-rw-r--r--. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r--r--. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r--r--. 1 root root    18 Mar  9 13:19 oldboy.txt

[root@oldboy ~]# find /root ! -name "oldboy.txt"

/root

/root/.bash_history

/root/.bashrc

/root/.bash_logout

/root/.bash_profile

/root/data

/root/install.log

/root/log2.txt

/root/.lesshst

/root/.cshrc

/root/anaconda-ks.cfg

/root/install.log.syslog

/root/.tcshrc

 

4、利用find查找刪除擴展名為txt的所有文件

[root@oldboy data]# ll

total 4

-rw-r--r--. 1 root root   0 Mar  9 16:13 1.txt

-rw-r--r--. 1 root root   0 Mar  9 16:13 2.txt

-rw-r--r--. 1 root root   0 Mar  9 16:13 3.txt

-rw-r--r--. 1 root root 292 Mar  9 13:28 oldboy.txt

[root@oldboy data]# find /root/data -type f -name "*.txt"|xargs rm -f

[root@oldboy data]# ll

total 0

 

【說明】:

1、按修改時間查找文件,+7表示距今天7天以前,7表示距今天整7天,-7表示最近7

2、-o表示或者的意思,-a表示並且的意思,!表示取反

 

 

12. 【命令】:grep

【功能說明】:

 print lines matching a pattern #用於過濾、搜索特定字符

【語法格式】:

grep [OPTIONS] PATTERN [FILE...]

grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

【選項參數】:

參數

說明

簡解

-a, --text

Process   a  binary  file  as  if  it  were  text;  this  is  equivalent  to  the --binary-files=text option.

不要忽略二進制的數據

-A NUM , --after-context=NUM

Print  NUM  lines  of  trailing  context  after  matching  lines.   Places a line containing  a  group  separator  (described  under   --group-separator)   between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

除了顯示符合范本樣式的那一列之外,並顯示該行之后的內容

-b, --byte-offset

Print  the  0-based byte offset within the input file before each line of output. If -o (--only-matching) is specified, print  the  offset  of  the  matching  part  itself.

在顯示符合樣式的那一行之前,標示出該行第一個字符的編號

-B NUM, --before-context=NUM

Print NUM lines  of  leading  context  before  matching  lines.   Places  a  line containing   a   group  separator  (described  under  --group-separator)  between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

除了顯示符合樣式的那一行之外,並顯示該行之前的內容

-c, --count

Suppress  normal  output;  instead print a count of matching lines for each input  file. With the -v, --invert-match option (see below), count non-matching  lines. (-c is specified by POSIX.)

計算符合樣式的列數

-C NUM, -NUM, --context=NUM

Print  NUM  lines  of output context.  Places a line containing a group separator (described under --group-separator) between contiguous groups of  matches.   With the -o or --only-matching option, this has no effect and a warning is given.

除了顯示符合樣式的那一行之外,並顯示該行之前后各n行內容

-d ACTION, --directories=ACTION

If an input file is a directory, use ACTION to process it.  By default, ACTION is read,  i.e.,  read directories just as if they were ordinary files.  If ACTION is skip, silently skip directories.  If ACTION is recurse, read all files under each directory,  recursively, following symbolic links only if they are on the command line.  This is equivalent to the -r option.

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

-e PATTERN, --regexp=PATTERN

Use  PATTERN  as  the  pattern.   This  can  be  used  to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-).  (-e is  specified by POSIX.)

使用正則表達式

-E, --extended-regexp

Interpret PATTERN as an extended regular expression (ERE,  see  below).   (-E is specified by POSIX.)

使用拓展的正則表達式

-f FILE, --file=FILE

Obtain  patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.  (-f is specified by POSIX.)

指定規則文件,其內容含有一個或多個規則樣式,讓grep查找符合規則條件的文件內容,格式為每行一個規則樣式。

-h, --no-filename

Suppress the prefixing of file names on output.  This is the default  when  there is only one file (or only standard input) to search.

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

-H, --with-filename

Print  the file name for each match.  This is the default when there is more than one file to search.

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

-i, --ignore-case

Ignore case distinctions in both  the  PATTERN  and  the  input  files.   (-i is specified by POSIX.)

忽略字符大小寫的差別。

-l, --files-with-matches

Suppress normal output; instead print the name of  each  input  file  from  which output  would  normally  have  been printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)

列出文件內容符合指定的樣式的文件名稱。

-L, --files-without-match

Suppress normal output; instead print the name of each input file from  which  no output  would  normally  have  been printed.  The scanning will stop on the first match.

列出文件內容不符合指定的樣式的文件名稱。

-n, --line-number

Prefix  each  line  of output with the 1-based line number within its input file. (-n is specified by POSIX.)

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

-q, --quiet, --silent

Quiet; do not write anything to standard  output.   Exit  immediately  with  zero status  if any match is found, even if an error was detected.  Also see the -s or --no-messages option.  (-q is specified by POSIX.)

不顯示任何信息。

-r, --recursive

Read  all  files under each directory, recursively, following symbolic links only if they are on the command line.  This is equivalent to the -d recurse option.

如果文件參數是目錄,該選項將遞歸搜索該目錄下的所有子目錄和文件。同-R

-s, --no-messages

Suppress error messages about nonexistent or unreadable files.  Portability note: unlike  GNU  grep,  7th  Edition  Unix  grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU greps -q  option.   USG-style  grep also  lacked  -q but its -s option behaved like GNU grep.  Portable shell scripts should avoid both -q and -s and should redirect  standard  and  error  output  to /dev/null instead.  (-s is specified by POSIX.)

不顯示錯誤信息。

-v, --invert-match

Invert  the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)

顯示不包含匹配文本的所有行。

-V, --version

Print  the  version  number  of grep to the standard output stream.  This version number should be included in all bug reports (see below).

顯示版本信息。

-w, --word-regexp

Select only those lines containing matches that form whole words.   The  test  is that  the  matching  substring  must  either  be at the beginning of the line, or preceded by a non-word constituent character.  Similarly, it must  be  either  at  the  end  of  the  line  or  followed by a non-word constituent character.  Word- constituent characters are letters, digits, and the underscore.

只顯示完整單詞的匹配

-x, --line-regexp

Select only those matches that exactly match the whole line.  (-x is specified by POSIX.)

只顯示完整行的匹配

【示例】:

1、基本用法:顯示文件中包含字符串的行

[root@oldboy ~]# cat oldboy.txt

test

liyao

oldboy

[root@oldboy ~]# grep "oldboy" oldboy.txt

oldboy

[root@oldboy ~]#

 

2、基本用法:顯示文件中不包含字符串的行

[root@oldboy ~]# grep -v "oldboy" oldboy.txt

test

liyao

 

3、除了顯示匹配的一行外,並顯示該行之后的n

[root@oldboy ~]# grep 20 -A 10 test.txt

20

21

22

23

24

25

26

27

28

29

30

 

 

【說明】:

grep的正則表達式:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 

POSIX字符:

    為了在不同國家的字符編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符類,如[:alnum:][A-Za-z0-9]的另一個寫法。要把它們放到[]號內才能成為正則表達式,如[A- Za-z0-9][[:alnum:]]。在linux下的grepfgrep外,都支持POSIX的字符類。

[:alnum:]    #文字數字字符   

[:alpha:]    #文字字符   

[:digit:]    #數字字符   

[:graph:]    #非空字符(非空格、控制字符)   

[:lower:]    #小寫字符   

[:cntrl:]    #控制字符   

[:print:]    #非空字符(包括空格)   

[:punct:]    #標點符號   

[:space:]    #所有空白字符(新行,空格,制表符)   

[:upper:]    #大寫字符   

[:xdigit:]   #十六進制數字(0-9a-fA-F

 

13. 【命令】:head

【功能說明】:

output the first part of files  #打印文件的前幾行

【語法格式】:

head [OPTION]... [FILE]...

【選項參數】:

參數

說明

簡解

-c, --bytes=[-]K

print the first K bytes of each file; with  the  leading  -,print all but the last K bytes of each file

按前K字節打印文件

-n, --lines=[-]K

print  the  first  K  lines  instead of the first 10; with the leading -, print all but the last K lines of each file

按前K行打印文件

-q, --quiet, --silent

never print headers giving file names

隱藏文件名

-v, --verbose

always print headers giving file names

顯示文件名

 

 

 

【示例】:

1、基本用法:打印文件前幾行

[root@oldboy ~]# head -n 13 test.txt

1

2

3

4

5

6

7

8

9

10

11

12

13

 

2、打印文件前K個字節

[root@oldboy ~]# head -c 20 test.txt

1

2

3

4

5

6

7

8

9

10[root@oldboy ~]#

 

3、顯示文件名

[root@oldboy ~]# head -v test.txt

==> test.txt <==

1

2

3

4

5

6

7

8

9

10

 

14. 【命令】:cp

【功能說明】:

copy files and directories  #復制文件或目錄

【語法格式】:

cp [OPTION]... [-T] SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...

【選項參數】:

參數

說明

簡解

-a, --archive

same as -dR --preserve=all

保持源文件的原有結構和屬性,與選項“-dpR”相同

--backup[=CONTROL]

make a backup of each existing destination file

做一個已存在文件的備份

-b

like --backup but does not accept an argument

覆蓋已存在的目標文件前將目標文件備份

--copy-contents

copy contents of special files when recursive

 

-d

same as --no-dereference --preserve=links

如果復制的源文件是符號鏈接,僅復制符號鏈接本身,而且保留符號鏈接所指向的目標文件或目錄

-f, --force

if an existing destination file cannot be  opened,  remove  it and try again (redundant if the -n option is used)

強制覆蓋已經存在的目標文件,不提示用戶確認

-i, --interactive

prompt before overwrite (overrides a previous -n option)

在覆蓋已存在的目標文件前提示用戶進行確認

-H

follow command-line symbolic links in SOURCE

 

-l, --link

link files instead of copying

為源文件創建硬鏈接

-L, --dereference

always follow symbolic links in SOURCE

 

-n, --no-clobber

do  not  overwrite  an  existing file (overrides a previous -i option)

 

-P, --no-dereference

never follow symbolic links in SOURCE

 

-p

same as --preserve=mode,ownership,timestamps

復制文件時保持源文件的所有者、權限信息以及時間屬性

--preserve[=ATTR_LIST]

preserve  the  specified  attributes   (default:   mode,owner-ship,timestamps),  if possible additional attributes: context, links, xattr, all

 

-c

same as --preserve=context

 

--no-preserve=ATTR_LIST

dont preserve the specified attributes

 

--parents

use full source file name under DIRECTORY

 

-R, -r, --recursive

copy directories recursively

對目錄進行復制操作,采用遞歸的操作方式

--reflink[=WHEN]

control clone/CoW copies. See below.

 

--remove-destination

remove each existing destination  file  before  attempting  to  open it (contrast with --force)

 

--sparse=WHEN

control creation of sparse files. See below.

 

--strip-trailing-slashes

remove any trailing slashes from each SOURCE argument

 

-s, --symbolic-link

make symbolic links instead of copying

為源文件創建符號鏈接

-S, --suffix=SUFFIX

override the usual backup suffix

在備份文件時,用指定的后綴“SUFFIX”代替文件名的默認后綴

-t, --target-directory=DIRECTORY

copy all SOURCE arguments into DIRECTORY

 

-T, --no-target-directory

treat DEST as a normal file

 

-u, --update

copy  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

詳細顯示指令執行的操作

-x, --one-file-system

stay on this file system

復制的文件或目錄存放的文件系統,必須與cp指令執行時所處的文件系統相同,否則不復制,亦不處理位於其他分區的文件

-Z, --context=CONTEXT

set security context of copy to CONTEXT

 

【示例】:

1、基本用法:復制文件到指定位置

[root@oldboy ~]# cp test.txt data

[root@oldboy ~]# cat data/test.txt

1

2

3

4

5

6

7

 

2、基本用法:復制目錄到指定位置

[root@oldboy ~]# cp -r /root/data /tmp

[root@oldboy ~]# cd /tmp/data

[root@oldboy data]#

 

3、復制時保持文件屬性

[root@oldboy ~]# ll

total 56

-rw-------. 1 root root  1079 Mar  3 23:14 anaconda-ks.cfg

drwxr-xr-x. 7 root root  4096 Mar 13 16:25 data

-rw-r--r--. 1 root root 22179 Mar  3 23:14 install.log

-rw-r--r--. 1 root root  5890 Mar  3 23:13 install.log.syslog

-rw-r--r--. 1 root root    44 Mar  7 17:53 log2.txt

-rw-r--r--. 1 root root    18 Mar  9 13:19 oldboy.txt

-rw-r--r--. 1 root root   292 Mar 13 15:13 test.txt

[root@oldboy ~]# cp -p test.txt /tmp

[root@oldboy ~]# ll /tmp

total 108

drwxr-xr-x  7 root root   4096 Mar 13 16:28 data

-rw-r--r--  1 root root    292 Mar 13 15:13 test.txt

-rw-------. 1 root root      0 Mar  3 23:09 yum.log

-rw-------. 1 root root 100203 Mar  4 00:39 yum_save_tx-2016-03-04-00-39i_5ZVm.yumtx

 

 

15. 【命令】:tail

【功能說明】:

output the last part of files  #打印文件末尾幾行

【語法格式】:

tail [OPTION]... [FILE]...

【選項參數】:

參數

說明

簡解

-c, --bytes=K

output the last K bytes; alternatively, use -c  +K  to  output bytes starting with the Kth of each file

輸出文件尾部的N個字節內容

-f, --follow[={name|descriptor}]

output  appended  data  as  the  file grows; -f, --follow, and --follow=descriptor are equivalent

顯示文件最新追加的內容

-F

same as --follow=name --retry

與選項“--follow=name”和“--retry”連用時的功能相同

-n, --lines=K

output the last K lines, instead of the last 10; or use -n  +K  to output lines starting with the Kth

輸出文件尾部K行內容

--max-unchanged-stats=N

with  --follow=name,  reopen a FILE which has not changed size after N (default 5) iterations to see if it has been  unlinked or  renamed  (this  is  the  usual case of rotated log files). With inotify, this option is rarely useful.

參看texinfo文檔(默認為5)

--pid=PID

with -f, terminate after process ID, PID dies

-f合用,表示在進程ID,PID死掉之后結束

-q, --quiet, --silent

never output headers giving file names

不顯示文件名

--retry

keep trying to open a file even when it is or becomes inacces-sible; useful when following by name, i.e., with --follow=name

即使文件不可訪問或者文件稍后變得不可訪問,都始終嘗試打開文件

-s, --sleep-interval=N

with -f, sleep  for  approximately  N  seconds  (default  1.0) between iterations.

 

With  inotify and --pid=P, check process P at least once every N seconds.

與“-f”連用,指定監視文件變化時間所間隔的秒數

-v, --verbose

always output headers giving file names

總在開頭顯示文件名

【示例】:

1、基本用法:顯示文件末尾的n

[root@oldboy ~]# tail -8 test.txt

93

94

95

96

97

98

99

100

 

2、在開頭顯示文件名

[root@oldboy ~]# tail -v -n 8 test.txt

==> test.txt <==

93

94

95

96

97

98

99

100

 

16. 【命令】:alias

【功能說明】:

Define or display aliases.  #顯示或設置別名

【語法格式】:

alias [-p] [name[=value] ... ]

【選項參數】:

參數

說明

簡解

-p

Print all defined aliases in a reusable format

打印已經設置的命令別名

【示例】:

1、基本用法:打印已經設置的別名(-p可以省略)

[root@oldboy ~]# alias -p

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'

alias rm='rm -i'

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

 

2、基本用法:設置新別名

[root@oldboy ~]# alias rm='echo "Do not use rm!"'

[root@oldboy ~]# rm -f test.txt

Do not use rm! -f test.txt

[root@oldboy ~]# 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'

alias rm='echo "Do not use rm!"'

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

 

 

17. 【命令】:unalias

【功能說明】:

Remove each NAME from the list of defined aliases.  #取消別名

【語法格式】:

unalias [-a] name [name ...]

【選項參數】:

參數

說明

簡解

-a

remove all alias definitions.

取消所有別名

【示例】:

1、基本用法:取消別名

[root@oldboy ~]# 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'

alias rm='echo "Do not use rm!"'

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

[root@oldboy ~]# unalias rm

[root@oldboy ~]# 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'

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

 

2、取消所有別名

[root@oldboy ~]# 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'

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

[root@oldboy ~]# unalias -a

[root@oldboy ~]# alias

[root@oldboy ~]#

 

18. 【命令】:seq

【功能說明】:

print a sequence of numbers  #生成數字序列

【語法格式】:

seq [OPTION]... LAST

seq [OPTION]... FIRST LAST

seq [OPTION]... FIRST INCREMENT LAST

【選項參數】:

參數

說明

簡解

-f, --format=FORMAT

use printf style floating-point FORMAT

格式化輸出

-s, --separator=STRING

use STRING to separate numbers (default: \n)

指定分隔符,默認是換行符

-w, --equal-width

equalize width by padding with leading zeroes

輸出同寬數列,不足的位數用0補齊

【示例】:

1、基本用法:生成數字序列

[root@oldboy ~]# seq 10

1

2

3

4

5

6

7

8

9

10

 

2、格式化輸出

[root@oldboy ~]# seq -f 100%g 10

1001

1002

1003

1004

1005

1006

1007

1008

1009

10010

3、指定分隔符

[root@oldboy ~]# seq -s "..." 10

1...2...3...4...5...6...7...8...9...10

[root@oldboy ~]# 

 

4、輸出同寬數列

[root@oldboy ~]# seq -w 10

01

02

03

04

05

06

07

08

09

10

 

19. 【命令】: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

直接在命令列模式上進行sed的動作編輯

-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 lcommand

 

--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

load minimal amounts of data from the input  files  and  flush  the output buffers more often

 

 

【示例】:

1、基本用法:僅列出 /etc/passwd 文件內的第 5-7

[root@oldboy ~]# nl /etc/passwd | sed -n '5,7p'

     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

 

2、利用sed替換取IP地址

[root@oldboy ~]# ifconfig eth0      

eth0      Link encap:Ethernet  HWaddr 00:0C:29:9C:9A:32  

          inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe9c:9a32/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:641 errors:0 dropped:0 overruns:0 frame:0

          TX packets:321 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:56546 (55.2 KiB)  TX bytes:40576 (39.6 KiB)

 

[root@oldboy ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's#^.*addr:##g' | sed 's#Bcast.*$##g'

10.0.0.8

 

3、直接修改文件內容

[root@oldboy ~]# cat test.txt

1

2

3

4

5

6

7

8

9

10

[root@oldboy ~]# sed -i 's#5#500#g' test.txt

[root@oldboy ~]# cat test.txt

1

2

3

4

500

6

7

8

9

10

 

【特別說明】:

動作說明: [n1[,n2]]function

n1, n2 :不見得會存在,一般代表『選擇進行動作的行數』,舉例來說,如果我的動作是需要在 10 20 行之間進行的,則『 10,20[動作行為]

function

a :新增, a 的后面可以接字串,而這些字串會在新的一行出現(目前的下一行)

c :取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行!

d :刪除,因為是刪除啊,所以 d 后面通常不接任何咚咚;

i :插入, i 的后面可以接字串,而這些字串會在新的一行出現(目前的上一行)

p :打印,亦即將某個選擇的數據印出。通常 p 會與參數 sed -n 一起運行~

s :取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正則表達式!例如 1,20s#old#new#g 就是啦!

 

20. 【命令】:man

【功能說明】:

format and display the on-line manual pages  #查看在線幫助手冊

【語法格式】:

man [-acdDfFhkKtwW] [--path] [-m system] [-p string] [-C config_file]

       [-M pathlist]  [-P  pager]  [-B  browser]  [-H  htmlpager]  [-S  sec-

       tion_list] [section] name ...

【選項參數】:

參數

說明

簡解

 

 

 

 

 

 

 

 

 

【示例】:

1、基本用法:查看在線幫助

[root@oldboy ~]# man ls

LS(1)                            User Commands                           LS(1)

 

NAME

       ls - list directory contents

 

SYNOPSIS

       ls [OPTION]... [FILE]...

以下省略

 

 

21. 【命令】:useradd

【功能說明】:

create a new user or update default new user information   #創建新用戶或更新默認新用戶的信息

【語法格式】:

useradd [options] LOGIN

useradd -D

useradd -D [options]

【選項參數】:

參數

說明

簡解

-c, --comment COMMENT

Any text string. It is generally a short description of the login, and is currently used as the field for the user's full name.

加上備注文字

-d, --home HOME_DIR

The new user will be created using HOME_DIR as the value for the user's login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name. The parent directory of HOME_DIR must exist otherwise the home directory cannot be created.

指定用戶登入時的起始目錄

-D,  defaults

See below, the subsection "Changing the default values".

變更預設值

-e, --expiredate EXPIRE_DATE

The date on which the user account will be disabled.

The date is specified in the format YYYY-MM-DD.

 

If not specified, useradd will use the default  expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default.

指定賬號的有效期限

-f, --inactive INACTIVE

The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.

 

If not specified, useradd will use the default  inactivity period specified by the INACTIVE variable  in /etc/default/useradd, or -1 by default.

指定在密碼過期后多少天即關閉該賬號

-g, --gid GROUP

The group name or number of the user's initial login group. The group name must exist. A group number must refer to an already existing group.

指定用戶所屬的群組

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]

A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for  the user to belong only to the initial group.

指定用戶所屬的附加群組

-m, --create-home

Create the user's home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory.

 

自動建立用戶的登入目錄

-M

Do not create the user's home directory, even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.

不要自動建立用戶的登入目錄

-N, --no-user-group

Do not create a group with the same name as the user, but add the user to the group specified by the -g option or by the GROUP variable in /etc/default/useradd.

取消建立以用戶名稱為名的群組

-r, --system

Create a system account.

建立系統賬號

-s, --shell SHELL

The name of the user's login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default.

指定用戶登入后使用的shell

-u, --uid UID

The numerical value of the user's ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user.

指定用戶ID

【示例】:

1、基本用法:創建新用戶

[root@oldboy ~]# useradd test

[root@oldboy ~]# cat /etc/passwd|tail -1

test:x:501:501::/home/test:/bin/bash

 

22. 【命令】:passwd

【功能說明】:

update users authentication tokens  #為用戶設置或修改密碼

【語法格式】:

passwd  [-k]  [-l]  [-u [-f]] [-d] [-e] [-n mindays] [-x

       maxdays] [-w warndays] [-i inactivedays] [-S]  [--stdin]

       [username]

【選項參數】:

參數

說明

簡解

-d

This  is  a quick way to delete a password for an account. It will set the named account  password-less. Available to root only.

刪除密碼,僅有系統管理者才能使用

-k

The option -k,  is  used  to  indicate  that  the update  should only be for expired authentication tokens (passwords); the user wishes to keep their non-expired tokens as before.

設置只有在密碼過期失效后,方能更新

-l

This option is used to lock the specified account and it is available to root only. The locking  is performed  by  rendering  the  encrypted password into  an  invalid  string   (by   prefixing   the  encrypted string with an !).

鎖住密碼

-S

This will output a short  information  about  the  status  of  the  password  for  a  given account.Available to root user only.

列出密碼的相關信息,僅有系統管理者才能使用

-u

This  is  the  reverse of the -l option - it will unlock the account password  by  removing  the  !prefix. This option is available to root only. By default passwd will refuse to create a  password-less  account (it will not unlock an account that has only "!" as a password). The force option  -f  will override this protection.

解開已上鎖的賬號

【示例】:

1、基本用法:設置用戶密碼

[root@oldboy ~]# passwd oldboy

Changing password for user oldboy.

New password:

BAD PASSWORD: it is too simplistic/systematic

BAD PASSWORD: is too simple

Retype new password:

passwd: all authentication tokens updated successfully.

 

 

23. 【命令】:uname

【功能說明】:

print system information  #打印系統信息

【語法格式】:

uname [OPTION]...

【選項參數】:

參數

說明

簡解

-a, --all

print all information, in  the  following  order, except omit -p and -i if unknown:

顯示全部的信息

-s, --kernel-name

print the kernel name

顯示操作系統類型

-n, --nodename

print the network node hostname

顯示主機名

-r, --kernel-release

print the kernel release

顯示核心版本號

-v, --kernel-version

print the kernel version

顯示操作系統的版本

-m, --machine

print the machine hardware name

顯示機器架構類型

-p, --processor

print the processor type or "unknown"

 

-i, --hardware-platform

print the hardware platform or "unknown"

顯示硬件平台

-o, --operating-system

print the operating system

顯示操作系統

【示例】:

1、基本用法:顯示全部信息

[root@oldboy ~]# uname -a

Linux oldboy 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

 

2、基本用法:顯示主機名

[root@oldboy ~]# uname -n

oldboy

 

3、顯示核心版本號

[root@oldboy ~]# uname -r

2.6.32-573.el6.x86_64

 

4、顯示機器架構類型

[root@oldboy ~]# uname -m

x86_64

 

 

24. 【命令】:hostname

【功能說明】:

show or set the systems host name  #顯示和設置主機名

【語法格式】:

hostname   [-v]  [-a]  [--alias]  [-d]  [--domain]  [-f]

       [--fqdn] [-A]  [--all-fqdns]  [-i]  [--ip-address]  [-I]

       [--all-ip-addresses] [--long] [-s] [--short] [-y] [--yp]

       [--nis]

hostname [-v] [-F filename] [--file filename] [hostname]

【選項參數】:

參數

說明

簡解

-v, --verbose

Be verbose and tell whats going on.

詳細信息模式

-a, --alias

Display the alias name of the host (if used)

顯示主機別名

-d, --domain

Display the name of the DNS domain. Dont use the  command  domainname  to  get  the DNS domain name because it will show the NIS domain name and  not  the DNS domain name. Use dnsdomainname instead.

顯示DNS域名

-f, --fqdn, --long

Display the FQDN (Fully Qualified Domain Name). A FQDN  consists  of  a short host name and the DNS domain name. Unless you are using bind or NIS for host  lookups you can change the FQDN and the DNS domain name (which is part of the  FQDN)  in  the /etc/hosts  file. See the warnings in section THE FQDN above, and  avoid  using  this  option;  use hostname --all-fqdns instead

顯示FQDN名稱

-i, --ip-address

Display the IP address(es) of the host. Note that this works only if the host name can be resolved.

Avoid using this option; use  hostname  --all-ip- addresses instead

顯示主機的ip地址

-s, --short

Display  the  short  host  name. This is the host name cut at the first dot.

顯示短主機名稱,在第一個點處截斷

-y, --yp, --nis

Display  the  NIS  domain name. If a parameter is given (or --file name ) then root can also set  a new NIS domain

顯示NIS域名

【示例】:

1、基本用法:顯示主機名

[root@oldboy ~]# hostname

oldboy

 

2、基本用法:修改主機名

[root@oldboy ~]# hostname oldgirl

[root@oldboy ~]# hostname

oldgirl

 

 

25. 【命令】:runlevel

【功能說明】:

output previous and current runlevel  #查看系統運行狀態

【語法格式】:

runlevel [OPTION]...  [UTMP]

【選項參數】:

參數

說明

簡解

--quiet

Does  not  output  the  current and previous run-level, nor does it output unknown in the case  of error (but it will exit with an error code).

 

This  may  be  used to test for the presence of a runlevel entry, or to check  for  errors  reading from the file.

不顯示當前運行狀態

【示例】:

1、基本用法:查看系統運行狀態

[root@oldboy ~]# runlevel

N 3

 

 

26. 【命令】:init

【功能說明】:

Upstart process management daemon  #設置系統運行狀態

【語法格式】:

init [OPTION]...

【選項參數】:

參數

說明

簡解

-v, --verbose

Outputs  verbose messages about job state changes and event emissions to the system console or log,useful for debugging boot.

顯示詳細信息

-q, --quiet

reduce output to errors only

只顯示錯誤

【示例】:

1、基本用法:關機

[root@oldboy ~]# init 0

 

2、基本用法:重啟

[root@oldboy ~]# init 6

 

 

 

27. 【命令】:shutdown

【功能說明】:

bring the system down  #關機

【語法格式】:

shutdown [OPTION]...  TIME [MESSAGE]

【選項參數】:

參數

說明

簡解

-r

Requests that the system be rebooted after it has been brought down

重啟系統

-h

Requests that the system be either halted or pow-ered off after it has been brought down, with the choice as to which left up to the system.

關機

-H

Requests that the system be halted after  it  has  been brought down

 

-P

Requests  that the system be powered off after it has been brought down.

 

-c

Cancels a running shutdown.  TIME is  not  specified with this option, the first argument is MES-SAGE.

當執行“shutdown -h 11:50”指令時,只要按+鍵就可以中斷關機的指令

-k

Only send out the warning  messages  and  disable  logins, do not actually bring the system down

只發送警告信息,不真關機

【示例】:

1、基本用法:立即關機

[root@oldboy ~]# shutdown -h now

 

2、指定12點關機

[root@oldboy ~]# shutdown -h 12:00

 

Broadcast message from root@oldboy

        (/dev/pts/0) at 11:54 ...

 

The system is going down for halt in 6 minutes!

 

 

 

28. 【命令】:reboot

【功能說明】:

reboot the system  #重啟系統

【語法格式】:

reboot [OPTION]...

【選項參數】:

參數

說明

簡解

-f, --force

Does  not invoke shutdown(8) and instead performs the actual action you would expect from the name.

強制重新開機

-p, --poweroff

Instructs  the  halt command to instead behave as poweroff

 

-w, --wtmp-only

Does not call shutdown(8) or the reboot(2) system call  and instead only writes the shutdown record to /var/log/wtmp

僅做測試,並不真正將系統重新開機,只會把重開機的數據寫入/var/log目錄下的wtmp記錄文件。

--verbose

Outputs  slightly  more  verbose  messages   when rebooting,  useful  for  debugging  problems with shutdown.

 

-n, --no-sync

don't sync before reboot or halt

 

【示例】:

1、基本用法:重啟系統

[root@oldboy ~]# reboot

 

 

29. 【命令】:history

【功能說明】:

Display or manipulate the history list.  #查看命令歷史

【語法格式】:

history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]

【選項參數】:

參數

說明

簡解

-c

clear the history list by deleting all of the entries

清空當前歷史命令

-d

offset delete the history entry at offset OFFSET

 

-a

append history lines from this session to the history file

將歷史命令緩沖區中命令寫入歷史命令文件中

-n

read all history lines not already read from the history file

 

-r

read the history file and append the contents to the history list

將歷史命令文件中的命令讀入當前歷史命令緩沖區

-w

write the current history to the history file and append them to the history list

 

-p

perform history expansion on each ARG and display the result without storing it in the history list

 

-s

append the ARGs to the history list as a single entry

 

【示例】:

1、基本用法:查看命令歷史

[root@oldboy ~]# history

    1  cd /usr/local/

    2  cd ..

    3  pwd

    4  cd ../..

    5  cd -

    6  cd ~

    7  cd /

    8  ll

9  ls -la

......

 

 

30. 【命令】:dmesg

【功能說明】:

print or control the kernel ring buffer  #查看系統故障信息

【語法格式】:

dmesg [-c] [-r] [-n level] [-s bufsize]

【選項參數】:

參數

說明

簡解

-c

Clear the ring buffer contents after printing.

顯示信息后,清除ring buffer中的內容

-r

Print  the  raw message buffer, i.e., dont strip  the log level prefixes.

 

-s bufsize

Use a buffer of size bufsize to query the  kernel ring  buffer.   This  is  16392 by default.  (The default kernel syslog buffer  size  was  4096  at  first,  8192  since 1.3.54, 16384 since 2.1.113.) If you have set the kernel buffer  to  be  larger than  the default then this option can be used to view the entire buffer

設置ring buffer大小,預設置為8196,剛好等於ring buffer的大小

-n level

Set the level at which  logging  of  messages  is done  to the console.  For example, -n 1 prevents all messages, except panic messages, from appear-ing  on  the console.  All levels of messages are still written to /proc/kmsg,  so  syslogd(8)  can

 still  be  used  to  control exactly where kernel messages appear.  When the  -n  option  is  used,dmesg  will  not  print  or clear the kernel ring buffer

設置記錄信息的層級

 

 

 

【示例】:

1、基本用法:查看信息

[root@oldboy ~]# dmesg

Initializing cgroup subsys cpuset

Initializing cgroup subsys cpu

Linux version 2.6.32-573.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Thu Jul 23 15:44:03 UTC 2015

Command line: ro root=UUID=8fc55236-ce36-4cc4-9924-202ed5142065 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

 

31. 【命令】:ifupifdown

【功能說明】:

ifup - bring a network interface up  #開啟網卡

ifdown - take a network interface down  #關閉網卡

【語法格式】:

ifup IFACE [boot]

ifdown IFACE

【選項參數】:

參數

說明

簡解

 

 

 

【示例】:

1、基本用法:關閉、開啟網卡

[root@oldboy ~]# ifdown eth0

[root@oldboy ~]# ifup eth0

Determining if ip address 10.0.0.8 is already in use for device eth0...

 

32. 【命令】:nl

【功能說明】:

number lines of files  #顯示文件行號

【語法格式】:

nl [OPTION]... [FILE]...

【選項參數】:

參數

說明

簡解

-b, --body-numbering=STYLE

use STYLE for numbering body lines

指定行號指定的方式,主要有兩種:

-b a :表示不論是否為空行,也同樣列出行號(類似 cat -n)

-b t :如果有空行,空的那一行不要列出行號(默認值)

-d, --section-delimiter=CC

use CC for separating logical pages

 

-f, --footer-numbering=STYLE

use STYLE for numbering footer lines

 

-h, --header-numbering=STYLE

use STYLE for numbering header lines

 

-i, --line-increment=NUMBER

line number increment at each line

 

-l, --join-blank-lines=NUMBER

group of NUMBER empty lines counted as one

 

-n, --number-format=FORMAT

insert line numbers according to FORMAT

列出行號表示的方法,主要有三種:

-n ln :行號在螢幕的最左方顯示;

-n rn :行號在自己欄位的最右方顯示,且不加 0 

-n rz :行號在自己欄位的最右方顯示,且加 0 

-p, --no-renumber

do not reset line numbers at logical pages

在邏輯定界符處不重新開始計算

-s, --number-separator=STRING

add STRING after (possible) line number

 

-v, --starting-line-number=NUMBER

first line number on each logical page

 

-w, --number-width=NUMBER

use NUMBER columns for line numbers

行號欄位的占用的位數

 

【示例】:

1、基本用法:顯示行號(默認空行不顯示)

[root@oldboy ~]# nl test.txt

     1  10

     2  14

     3  99

       

       

       

     4  8a1

     5  69

 

2、顯示行號(空行也顯示)

[root@oldboy ~]# nl -b a test.txt

     1  10

     2  14

     3  99

     4

     5

     6

     7  8a1

     8  69

 

3、讓行號前自動補上0,統一輸出格式

[root@oldboy ~]# nl -b a -n rz test.txt

000001  10

000002  14

000003  99

000004

000005

000006

000007  8a1

000008  69

 

4、調整統一輸出格式為三位數字

[root@oldboy ~]# nl -b a -n rz -w 3 test.txt

001     10

002     14

003     99

004

005

006

007     8a1

008     69

 

 

33. 【命令】:less

【功能說明】:

opposite of more  #分屏查看文本(可回退)

【語法格式】:

       less -?

       less --help

       less -V

       less --version

       less [-[+]aBcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]

            [-b space] [-h lines] [-j line] [-k keyfile]

            [-{oO} logfile] [-p pattern] [-P prompt] [-t tag]

            [-T tagsfile] [-x tab,...] [-y lines] [-[z] lines]

            [-# shift] [+[+]cmd] [--] [filename]...

【選項參數】:

參數

說明

簡解

-bn

Specifies the amount of buffer  space  less  will use  for  each  file, in units of kilobytes (1024 bytes).  By default 64K of buffer space  is  used for each file (unless the file is a pipe; see the -B option).  The -b option specifies instead that n  kilobytes  of  buffer space should be used for each file.  If n is -1, buffer  space  is  unlimited;  that  is, the entire file can be read into memory.

設置緩沖區的大小

-e

Causes less to automatically exit the second time it reaches end-of-file.  By default, the only way to exit less is via the "q" command.

當文件顯示結束后,自動離開

-f

Forces non-regular files to be opened.   (A  non-regular  file  is a directory or a device special file.)  Also suppresses the warning message  when a  binary  file is opened.  By default, less will refuse to open non-regular files.  Note that some operating  systems  will not allow directories to be read, even if -f is set

強迫打開特殊文件,例如外圍設備代號、目錄和二進制文件

-g

Normally,  less  will highlight ALL strings which match the last search  command.   The  -g  option changes  this behavior to highlight only the particular string which was found by the last search command.   This  can  cause  less to run somewhat  faster than the default

只標志最后搜索的關鍵詞

-i

Causes searches to ignore case; that  is,  uppercase  and  lowercase  are  considered  identical. This option is ignored if any  uppercase  letters appear  in the search pattern; in other words, if a pattern contains uppercase letters,  then  that  search does not ignore case

忽略搜索時的大小寫

-m

Causes less to prompt verbosely (like more), with the  percent  into  the  file.   By default, less prompts with a colon

顯示類似more命令的百分比

-N

Causes a line  number  to  be  displayed  at  the beginning of each line in the display.

顯示每行的行號

-ofilename

Causes  less  to copy its input to the named file as it is being viewed.  This  applies  only  when the  input  file is a pipe, not an ordinary file. If the file already exists,  less  will  ask  for confirmation before overwriting it

less 輸出的內容在指定文件中保存起來

-Q

Causes  totally  "quiet"  operation: the terminal bell is never rung.

不使用警告音

-s

Causes  consecutive  blank  lines  to be squeezed into a single blank line.  This  is  useful  when viewing nroff output

顯示連續空行為一行

-S

Causes  lines  longer than the screen width to be chopped rather than folded.  That is, the portion of  a  long  line that does not fit in the screen width is not shown.  The default is to fold  long lines; that is, display the remainder on the next line

行過長時間將超出部分舍棄

-xn

Sets  tab stops.  If only one n is specified, tab stops are set at multiples  of  n.   If  multiple  values  separated  by  commas  are specified, tab stops are set at those positions, and  then  continue with the same spacing as the last two.  For example, -x9,17 will set tabs at positions 9, 17,25, 33, etc. The default for n is 8

將“tab”鍵顯示為規定的數字空格

【示例】:

1、基本用法:查看文件

[root@oldboy ~]# less test.txt

10

14

99

 

 

 

8a1

69

38

 

【特別說明】

1.全屏導航

ctrl + F - 向前移動一屏

ctrl + B - 向后移動一屏

ctrl + D - 向前移動半屏

ctrl + U - 向后移動半屏

 

2.單行導航

j - 向前移動一行

k - 向后移動一行

 

3.其它導航

G - 移動到最后一行

g - 移動到第一行

q / ZZ - 退出 less 命令

 

4.其它有用的命令

v - 使用配置的編輯器編輯當前文件

h - 顯示 less 的幫助文檔

&pattern - 僅顯示匹配模式的行,而不是整個文件

 

5.標記導航

當使用 less 查看大文件時,可以在任何一個位置作標記,可以通過命令導航到標有特定標記的文本位置:

ma - 使用 a 標記文本的當前位置

'a - 導航到標記 a 

 

34. 【命令】:more

【功能說明】:

file perusal filter for crt viewing  #分屏查看文件(不可回退)

【語法格式】:

more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...]

【選項參數】:

參數

說明

簡解

+num

Start at line number num

從笫n行開始顯示

-num

This option specifies an integer which is the screen size (in lines)

定義屏幕大小為n

+/pattern

The +/ option specifies a string that will be searched for before each file is displayed

在每個檔案顯示前搜尋該字串(pattern),然后從該字串前兩行之后開始顯示

-c

Do not scroll.  Instead, paint each screen from the top, clearing the remainder of each line as it is displayed.

從頂部清屏,然后顯示

-d

more will prompt the user with the message "[Press space to continue, qto quit.]" and will display"[Press hfor instructions.]" instead of ringing the bell when an illegal key is pressed

提示“Press space to continue,’q’ to quit(按空格鍵繼續,按q鍵退出)”,禁用響鈴功能

-l

more usually treats ^L (form feed) as a special character, and will pause after any line that contains a form feed.  The -l option will prevent this behavior.

忽略Ctrl+l(換頁)字符

-p

Do not scroll.  Instead, clear the whole screen and then display the text

通過清除窗口而不是滾屏來對文件進行換頁,與-c選項相似

-s

Squeeze multiple blank lines into one.

把連續的多個空行顯示為一行

-u

Suppress underlining

把文件內容中的下畫線去掉

【示例】:

1、基本用法:查看文件

[root@oldboy ~]# more test.txt

10

14

99

 

 

 

8a1

69

38

【特別說明】

Enter    向下n行,需要定義。默認為1

Ctrl+F   向下滾動一屏

空格鍵  向下滾動一屏

Ctrl+B  返回上一屏

=         輸出當前行的行號

f      輸出文件名和當前行的行號

V        調用vi編輯器

!命令   調用Shell,並執行命令 

q         退出more

 

35. 【命令】:wc

【功能說明】:

print newline, word, and byte counts for each file  #打印行數、字數

【語法格式】:

wc [OPTION]... [FILE]...

wc [OPTION]... --files0-from=F

【選項參數】:

參數

說明

簡解

-c, --bytes

print the byte counts

打印字節數

-m, --chars

print the character counts

打印字符數

-l, --lines

print the newline counts

打印行數

--files0-from=F

read input from the files specified by NUL-terminated names in file F; If F is - then read  names from standard input

 

-L, --max-line-length

print the length of the longest line

打印最長行的長度

-w, --words

print the word counts

打印單詞數

【示例】:

1、基本用法:打印行數、單詞數、字節數及文件名

[root@oldboy ~]# wc test.txt

 9  6 22 test.txt

 

2、只打印統計數字不打印文件名

[root@oldboy ~]# cat test.txt|wc -l

9

 

 

36. 【命令】:chkconfig

【功能說明】:

updates and queries runlevel information for system services  #管理開機自啟動程序

【語法格式】:

chkconfig [--list] [--type type][name]

chkconfig --add name

chkconfig --del name

chkconfig --override name

chkconfig   [--level   levels]   [--type   type]    name<on|off|reset|resetpriorities>

       chkconfig [--level levels] [--type type] name

【選項參數】:

參數

說明

簡解

--level levels

Specifies the run levels an operation should pertain  to. It is given as a string of numbers from 0 to 6. For example, --level  35  specifies  run- levels 3 and 5

指定系統服務要在哪一個執行等級中開啟或關畢

--add name

This  option adds a new service for management by chkconfig.  When a new service is added,  chkconfig  ensures  that the service has either a start or a kill entry in every runlevel.  If  any  run-level is missing such an entry, chkconfig creates the appropriate entry as specified by the default values  in  the  init  script.  Note that default entries in  LSB-delimited  INIT  INFO’  sections take precedence over the default runlevels in the initscript; if any  Required-Start  or  Required-Stop entries are present, the start and stop priorities of the script will be adjusted to account for these dependencies

增加所指定的系統服務,讓chkconfig指令得以管理它,並同時在系統啟動的敘述文件內增加相關數據

--del name

The service is removed from chkconfig management,and any symbolic links  in  /etc/rc[0-6].d  which pertain to it are removed.Note  that  future package installs for this service may run chkconfig --add, which  will  re-add such  links.  To disable a service, run chkconfig name off.

刪除所指定的系統服務,不再由chkconfig指令管理,並同時在系統啟動的敘述文件內刪除相關數據

--list name

This option lists all of the services which  chkconfig  knows about, and whether they are stopped or started in each runlevel. If  name  is  specified,  information  in only display about service name.

羅列所有服務在各執行等級上的開啟、關閉情況

【示例】:

1、基本用法:查看所有服務在各執行等級上的開啟、關閉情況

[root@oldboy ~]# chkconfig --list

abrt-ccpp       0:off   1:off   2:off   3:off   4:off   5:off   6:off

abrtd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

acpid           0:off   1:off   2:off   3:off   4:off   5:off   6:off

atd             0:off   1:off   2:off   3:off   4:off   5:off   6:off

auditd          0:off   1:off   2:off   3:off   4:off   5:off   6:off

blk-availability        0:off   1:on    2:off   3:off   4:off   5:off6:off

cpuspeed        0:off   1:on    2:off   3:off   4:off   5:off   6:off

crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off

haldaemon       0:off   1:off   2:off   3:off   4:off   5:off   6:off

......

 

2、基本用法:在指定執行等級上關閉(開啟)一個服務

[root@oldboy ~]# chkconfig --level 2345 atd off

[root@oldboy ~]# chkconfig --list|grep "atd"

atd             0:off   1:off   2:off   3:off   4:off   5:off   6:off

 

 

37. 【命令】:tar

【功能說明】:

manual page for tar 1.23  #打包命令

【語法格式】:

tar [OPTION...] [FILE]...

【選項參數】:

參數

說明

簡解

-A

append tar files to an archive

新增壓縮文件到已存在的壓縮

-c

create a new archive

建立新的壓縮文件

-d

find differences between archive and file system

記錄文件的差別

-r

append files to the end of an archive

添加文件到已經壓縮的文件結尾

-u

only append files newer than copy in archive

添加改變了和現有的文件到已經存在的壓縮文件

-x

extract files from an archive

從壓縮的文件中提取文件

-t

list the contents of an archive

顯示壓縮文件的內容

-z

filter the archive through gzip

支持gzip解壓文件

-j

filter the archive through bzip2

支持bzip2解壓文件

-v

verbosely list files processed

顯示操作過程

-W

attempt to verify the archive after writing it

確認壓縮文件的正確性

-C

change to directory DIR

切換到指定目錄

-f

use archive file or device ARCHIVE

指定壓縮文件

【示例】:

1、基本用法:打包文件zcvf

[root@oldboy ~]# tar zcvf data.tar.gz ./data

./data/

./data/d.txt

./data/oldboy.txt

./data/dir5/

./data/dir1/

./data/test.txt

./data/c.txt

./data/dir2/

./data/dir3/

./data/dir4/

./data/e.txt

[root@oldboy ~]# ls

anaconda-ks.cfg  install.log         oldboy         test.txt

data             install.log.syslog  oldboy.tar.gz

data.tar.gz      log2.txt            oldboy.txt

 

2、基本用法:解包zxvf

[root@oldboy tmp]# tar zxvf data.tar.gz

./data/

./data/d.txt

./data/oldboy.txt

./data/dir5/

./data/dir1/

./data/test.txt

./data/c.txt

./data/dir2/

./data/dir3/

./data/dir4/

./data/e.txt

[root@oldboy tmp]# ls

data  data.tar.gz  file1.txt  file2.txt  oldboy  test.txt

 

3、批量排除打包

[root@oldboy opt]# cat paichu.log
stu05  
stu06  
stu07
[root@oldboy opt]# tar zcvfX paichuX.tar.gz paichu.log ./test 
./test/
./test/stu03
./test/stu01
./test/stu02
./test/stu09
./test/stu08
./test/stu10
./test/stu04
[root@oldboy opt]# tar tf paichuX.tar.gz 
./test/
./test/stu03
./test/stu01
./test/stu02
./test/stu09
./test/stu08
./test/stu10
./test/stu04

 

38. 【命令】:cut

【功能說明】:

remove sections from each line of files  

【語法格式】:

cut OPTION... [FILE]...

【選項參數】:

參數

說明

簡解

-b, --bytes=LIST

select only these bytes

以字節為單位進行分割

-c, --characters=LIST

select only these characters

以字符為單位進行分割

-d, --delimiter=DELIM

use DELIM instead of TAB for field delimiter

自定義分隔符,默認為制表符

-f, --fields=LIST

select  only these fields;  also print any line that contains  no  delimiter  character,  unless  the  -soption is specified

-d一起使用,指定顯示哪個區域

-n

with -b: dont split multibyte characters

取消分割多字節字符僅和 -b 標志一起使用。如果字符的最后一個字節落在由 -b 標志的 List 參數指示的范圍之內,該字符將被寫出;否則,該字符將被排除。

--complement

complement  the set of selected bytes, characters or fields

補足被選擇的字節、字符或字段

-s, --only-delimited

do not print lines not containing delimiters

不打印不含分隔符的行

--output-delimiter=STRING

use STRING as the output delimiter the default is to use the input delimiter

指定分隔符打印樣式

【示例】:

1、基本用法:以字節為單位取列

[root@oldboy ~]# cat oldboy.txt

inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0

[root@oldboy ~]# cut -b6-18 oldboy.txt

addr:10.0.0.8

 

2、基本用法:以字符為單位取列

[root@oldboy ~]# cat oldboy.txt

inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0

[root@oldboy ~]# cut -c6-18 oldboy.txt

addr:10.0.0.8

 

3、基本用法:以分隔符為單位取列

[root@oldboy ~]# cat /etc/passwd|head -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

[root@oldboy ~]# cat /etc/passwd|head -5|cut -d':' -f5

root

bin

daemon

adm

lp

 

 

39. 【命令】:tr

【功能說明】:

translate or delete characters  #轉換或刪除字符

【語法格式】:

tr [OPTION]... SET1 [SET2]

【選項參數】:

參數

說明

簡解

-c, -C, --complement

use the complement of SET1

取代所有不屬於第一字符集的字符

-d, --delete

delete characters in SET1, do not translate

刪除所有屬於第一字符集的字符

-s, --squeeze-repeats

replace each input sequence of a repeated  character that  is  listed in SET1 with a single occurrence of that character

把連續重復的字符以單獨一個字符表示

-t, --truncate-set1

first truncate SET1 to length of SET2

先刪除第一字符集較第二字符集多出的字符

【示例】:

1、基本用法:替換字符

[root@oldboy ~]# echo abcdef|tr 'a-f' 'x-za-c'

xyzabc

 

2、刪除字符

[root@oldboy ~]# echo abcdef|tr -d 'a-c'

def

 

3、連續重復的字符以單獨一個字符表示

[root@oldboy ~]# echo aaabbbccc|tr -s 'abc'

abc

[root@oldboy ~]# echo aaabbbccc|tr -s 'a'

abbbccc

 

 

40. 【命令】:stat

【功能說明】:

display file or file system status  #顯示文件和文件系統狀態(查看文件屬性)

【語法格式】:

stat [OPTION]... FILE...

【選項參數】:

參數

說明

簡解

-L, --dereference

follow links

如果是鏈接文件,則獲取原文件信息

-Z, --context

print the SELinux security context

打印SELinux安全上下文信息

-f, --file-system

display file system status instead of file status

顯示文件系統狀態而非文件狀態

-c  --format=FORMAT

use  the  specified  FORMAT  instead of the default; output a newline after each use of FORMAT

自定義輸出格式,結尾有換行

--printf=FORMAT

like --format, but interpret backslash escapes,  and do  not output a mandatory trailing newline.  If you want a newline, include \n in FORMAT.

自定義輸出格式,結尾無換行符,需手動添加

-t, --terse

print the information in terse form

以簡潔方式輸出信息

【示例】:

1、基本用法:查看文件信息

[root@oldboy ~]# stat test.txt

  File: `test.txt'

  Size: 22              Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d      Inode: 11628       Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-03-19 16:08:55.616298620 +0800

Modify: 2016-03-19 16:08:44.504300222 +0800

Change: 2016-03-19 16:08:44.505300149 +0800

 

2、利用stat取文件權限屬性

[root@oldboy ~]# stat test.txt

  File: `test.txt'

  Size: 22              Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d      Inode: 11628       Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

......

[root@oldboy ~]# stat -c %a test.txt

644

 

41. 【命令】:file

【功能說明】:

determine file type  #顯示文件類型

【語法格式】:

file [-bchikLNnprsvz0] [--apple] [--mime-encoding]

          [--mime-type] [-e testname] [-F separator] [-f namefile]

          [-m magicfiles] file ...

file -C [-m magicfiles]

file [--help]

【選項參數】:

參數

說明

簡解

-b

Do not prepend filenames to output lines (brief mode)

列出文件辨識結果時,不顯示文件名稱。

-c

Cause a checking printout of the parsed form of the magic file.  This is usually used in conjunction with the -m flag to debug a new magic file before installing it

詳細顯示指令執行過程,便於排錯或分析程序執行的情形

-f

Read the names of the files to be examined from namefile (one per line) before the argument list.Either namefile or at least one filename argument must be present; to test the standard input, use -as a filename argument.

列出文件中文件名的文件類型

-F

Use the specified string as the separator between the filename and the file result returned. Defaults to:.

使用指定分隔符號替換輸出文件名后的默認的“:”分隔符

-i

Causes the file command to output mime type strings rather than the more traditional human readable ones.Thus it may say text/plain; charset=us-asciirather than ASCII text.  In order for this option to work,file changes the way it handles files recognized by the command itself (such as many of the text file types, directories etc), and makes use of an alternative magicfile.  (See the FILES section, below)

輸出mime類型的字符串

-L

option causes symlinks to be followed, as the like-named option in ls(1) (on systems that support symbolic links).  This is the default if the environment variable POSIXLY_CORRECT is defined

查看對應軟鏈接對應文件的文件類型

-z

Try to look inside compressed files.

嘗試去解讀壓縮文件的內容

【示例】:

1、基本用法:顯示文件類型

[root@oldboy ~]# file test.txt

test.txt: ASCII text

[root@oldboy ~]# file /data

/data: directory

 

 

42. 【命令】:last

【功能說明】:

show listing of last logged in users  #查看用戶登錄信息

【語法格式】:

last  [-R]  [-num]  [  -n num ] [-adFiowx] [ -f file ] [ -t

       YYYYMMDDHHMMSS ] [name...]  [tty...]

       lastb [-R] [-num] [  -n  num  ]  [  -f  file  ]  [-adFiowx]

       [name...]  [tty...]

【選項參數】:

參數

說明

簡解

-a

Display  the  hostname in the last column. Useful in combination with the next flag

將登錄系統的的主機名稱或IP地址,顯示在最后一行

-d

For non-local logins, Linux stores not only the host name  of  the remote host but its IP number as well.This option translates the IP  number  back  into  a hostname

IP地址轉換成主機名稱

-f

Specifies a file to search other than /var/log/wtmp

指定記錄文件,默認是顯示/var/log目錄下的wtmp文件的記錄,但/var/log目錄下得btmp能顯示的內容更豐富,可以顯示遠程登錄,例如ssh登錄 ,包括失敗的登錄請求

-i

This  option  is  like -d in that it displays the IP number of the remote host, but it  displays  the  IP  number in numbers-and-dots notation.

-i顯示特定ip登錄的情況。跟蹤用 -i顯示特定ip登錄的情況。跟蹤用

-o

Read  an  old-type wtmp file (written by linux-libc5 applications).

 

-n

This is a count telling last how many lines to show.

-n <顯示列數>或-<顯示列數>  設置列出名單的顯示列數

-w

Display full user and domain names in the output

 

-R

Suppresses the display of the hostname field

不顯示登入系統的主機名稱或IP(省略 hostname 的欄位)

-t

Display  the  state  of  logins  as of the specified time.  This is useful, e.g., to determine easily who was  logged  in at a particular time -- specify that time with -t and look for "still logged in"

顯示YYYYMMDDHHMMSS之前的信息

-x

Display the system shutdown entries  and  run  level  changes.

顯示系統關閉、用戶登錄和退出的歷史

【示例】:

1、基本用法:查看用戶登錄信息

[root@oldboy ~]# last

oldboy   pts/0        10.0.0.1         Sun Mar 20 12:15   still logged in   

reboot   system boot  2.6.32-573.el6.x Sun Mar 20 12:15 - 15:08  (02:53)    

oldboy   pts/1        10.0.0.1         Sat Mar 19 15:36 - down   (06:49)    

root     tty1                          Sat Mar 19 15:35 - down   (06:50)

 

2、顯示最后登錄系統的N條記錄

[root@oldboy ~]# last -n 10

oldboy   pts/0        10.0.0.1         Sun Mar 20 12:15   still logged in   

reboot   system boot  2.6.32-573.el6.x Sun Mar 20 12:15 - 15:25  (03:10)    

oldboy   pts/1        10.0.0.1         Sat Mar 19 15:36 - down   (06:49)    

root     tty1                          Sat Mar 19 15:35 - down   (06:50)    

oldboy   pts/0        10.0.0.1         Sat Mar 19 10:28 - 15:52  (05:24)    

reboot   system boot  2.6.32-573.el6.x Sat Mar 19 10:28 - 22:25  (11:57)    

root     tty1                          Sat Mar 19 10:27 - down   (00:00)    

oldboy   pts/0        10.0.0.1         Sat Mar 19 10:20 - down   (00:07)    

oldboy   pts/0        10.0.0.1         Sat Mar 19 10:19 - 10:20  (00:00)    

reboot   system boot  2.6.32-573.el6.x Sat Mar 19 10:19 - 10:27  (00:08)    

 

wtmp begins Thu Mar  3 23:14:45 2016

 

3、將IP地址轉換成主機名稱

[root@oldboy ~]# last -n 5 -d

oldboy   pts/0        localhost        Sun Mar 20 12:15   still logged in   

reboot   system boot  0.0.0.0          Sun Mar 20 12:15 - 15:27  (03:11)    

oldboy   pts/1        localhost        Sat Mar 19 15:36 - down   (06:49)    

root     tty1         0.0.0.0          Sat Mar 19 15:35 - down   (06:50)    

oldboy   pts/0        localhost        Sat Mar 19 10:28 - 15:52  (05:24)

 

4、指定/var/log/btmp文件,查看登錄系統的用戶相關信息

[root@oldboy ~]# last -n 5 -f /var/log/btmp

 

btmp begins Thu Mar  3 23:14:22 2016

 

 

43. 【命令】:lastlog

【功能說明】:

reports the most recent login of all users or of a given user  #查看最近登錄的用戶信息

【語法格式】:

lastlog [options]

【選項參數】:

參數

說明

簡解

-b, --before DAYS

Print only lastlog records older than DAYS

顯示指定天數前的登錄信息

-h, --help

Display help message and exit.

顯示幫助

-t, --time DAYS

Print the lastlog records more recent than DAYS

顯示指定天數以來的登錄信息

-u, --user LOGIN|RANGE

Print the lastlog record of the specified user(s)

顯示指定用戶的最近登錄信息

【示例】:

1、基本用法:顯示最近登錄的用戶信息

[root@oldboy ~]# lastlog

Username         Port     From             Latest

root             tty1                      Sat Mar 19 15:35:02 +0800 2016

bin                                        **Never logged in**

daemon                                     **Never logged in**

adm                                        **Never logged in**

lp                                         **Never logged in**

sync                                       **Never logged in**

shutdown                                   **Never logged in**

halt                                       **Never logged in**

mail                                       **Never logged in**

......

oldboy           pts/0    10.0.0.1         Sun Mar 20 12:15:35 +0800 2016

test                                       **Never logged in**

 

2、顯示某一個用戶最近登錄信息

[root@oldboy ~]# lastlog -u oldboy

Username         Port     From             Latest

oldboy           pts/0    10.0.0.1         Sun Mar 20 12:15:35 +0800 2016

 

 

44. 【命令】:df

【功能說明】:

report file system disk space usage  #查看系統磁盤空間的使用情況

【語法格式】:

df [OPTION]... [FILE]...

【選項參數】:

參數

說明

簡解

-a

include dummy file systems

包含全部的文件系統

-B

use SIZE-byte blocks

以指定的區塊大小來顯示區塊數目

-h

print sizes in human readable format (e.g., 1K 234M 2G)

以可讀性較高的方式來顯示信息

-H

likewise, but use powers of 1000 not 1024

-h相同,但在計算時是以1000Bytes為換算單位而非1024 Bytes

-i

list inode information instead of block usage

顯示inode的信息

-k

like --block-size=1K

指定區塊大小為1024字節

-l

limit listing to local file systems

僅顯示本地的文件系統

--no-sync

do not invoke sync before getting usage info (default)

在取得磁盤使用信息前,不要執行sync指令,此為預設值

-P

use the POSIX output format

使用POSIX的輸出格式

--sync

invoke sync before getting usage info

在取得磁盤使用信息前,先執行sync指令

-t

limit listing to file systems of type TYPE

僅顯示指定文件系統類型的磁盤信息

-T

print file system type

顯示文件系統的類型

-x

limit listing to file systems not of type TYPE

不要顯示指定文件系統類型的磁盤信息

【示例】:

1、基本用法:查看磁盤空間使用情況

[root@oldboy ~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/sda3        7149156 1494028   5285312  23% /

tmpfs             243320       0    243320   0% /dev/shm

/dev/sda1         194241   40498    143503  23% /boot

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       6.9G  1.5G  5.1G  23% /

tmpfs           238M     0  238M   0% /dev/shm

/dev/sda1       190M   40M  141M  23% /boot

 

2、基本用法:查看inode的消耗情況

[root@oldboy ~]# df -hi

Filesystem     Inodes IUsed IFree IUse% Mounted on

/dev/sda3        452K   55K  397K   13% /

tmpfs             60K     1   60K    1% /dev/shm

/dev/sda1         50K    39   50K    1% /boot

[root@oldboy ~]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      462384 56221 406163   13% /

tmpfs           60830     1  60829    1% /dev/shm

/dev/sda1       51200    39  51161    1% /boot

 

 

45. 【命令】:dumpe2fs

【功能說明】:

dump ext2/ext3/ext4 filesystem information  #查看文件系統內部信息(元數據)

【語法格式】:

dumpe2fs [ -bfhixV ] [ -o superblock=superblock ] [ -o blocksize=block-

       size ] device

【選項參數】:

參數

說明

簡解

-b

print the blocks which are reserved as bad in the filesystem

打印文件系統中預留的塊信息

-o superblock=superblock

use the block superblock when examining  the  filesystem.   This option  is  not usually needed except by a filesystem wizard who is examining the remains of a very badly corrupted filesystem

指定檢查文件系統時使用的超級塊

-o blocksize=blocksize

use blocks of blocksize bytes  when  examining  the  filesystem.This  option is not usually needed except by a filesystem wizard who is examining the remains of a very badly corrupted  filesystem

檢查文件系統時使用指定的塊大小

-f

force  dumpe2fs  to display a filesystem even though it may have some filesystem feature flags which dumpe2fs may not  understand(and  which can cause some of dumpe2fss display to be suspect)

 

-h

only display the superblock information and not any of the block group descriptor detail information

僅顯示超級塊信息

-i

display  the  filesystem  data  from  an  image  file created by e2image, using device as the pathname to the image file

從指定的文件系統映像文件中讀取文件系統信息

-x

print the detailed group information block numbers in  hexadecimal format

16進制格式打印信息塊成員

-V

print the version number of dumpe2fs and exit.

 

【示例】:

1、基本用法:查看文件系統內部信息

[root@oldboy ~]# dumpe2fs /dev/sda1

dumpe2fs 1.41.12 (17-May-2010)

Filesystem volume name:   <none>

Last mounted on:          /boot

Filesystem UUID:          3e368f7e-0775-4c00-afba-f8880d916366

Filesystem magic number:  0xEF53

Filesystem revision #:    1 (dynamic)

Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize

Filesystem flags:         signed_directory_hash

Default mount options:    user_xattr acl

......

Group 0: (Blocks 1-8192) [ITABLE_ZEROED]

  Checksum 0x303a, unused inodes 2009

  Primary superblock at 1, Group descriptors at 2-2

  Reserved GDT blocks at 3-258

  Block bitmap at 259 (+258), Inode bitmap at 275 (+274)

  Inode table at 291-546 (+290)

  3785 free blocks, 2009 free inodes, 6 directories, 2009 unused inodes

  Free blocks: 4408-8192

  Free inodes: 40-2048

Group 1: (Blocks 8193-16384) [INODE_UNINIT, ITABLE_ZEROED]

......

 

 

46. 【命令】:tree

【功能說明】:

list contents of directories in a tree-like format  #以樹的形式羅列目錄內容

【語法格式】:

tree  [-adfghilnopqrstuvxACDFNS] [-L level [-R]] [-H baseHREF] [-T title] [-o

       filename]  [--nolinks]  [-P  pattern]  [-I  pattern]  [--inodes]   [--device]

       [--noreport]  [--dirsfirst]  [--version]  [--help] [--filelimit #] [directory

       ...]

【選項參數】:

參數

說明

簡解

-a

All files are printed.  By default tree does not  print  hidden  files(those  beginning  with  a  dot .).  In no event does tree print the file system constructs .’  (current  directory)  and  ..’  (previous directory)

顯示所有文件和目錄

-A

Turn on ANSI line graphics hack when printing the indentation lines

使用ASNI繪圖字符顯示樹狀圖而非以ASCII字符組合

-C

Turn colorization on always, using  built-in  color  defaults  if  the LS_COLORS  environment variable is not set.  Useful to colorize output to a pipe

在文件和目錄清單加上色彩,便於區分各種類型

-d

List directories only

只羅列目錄

-D

Print the date of the last modification time for the file listed

列出文件或目錄的更改時間

-L

Max display depth of the directory tree

羅列的層次深度

-f

Prints the full path prefix for each file

在每個文件或目錄之前,顯示完整的相對路徑名稱

-F

Append  a  /for directories, a =for socket files, a *for executable files and a |for FIFOs, as per ls -F

在執行文件,目錄,Socket,符號連接,管道名稱名稱,各自加上"*","/","=","@","|"

-g

Print the group name, or GID # if no group name is available,  of  the file.

列出文件或目錄的所屬群組名稱,沒有對應的名稱時,則顯示群組識別碼

-i

Makes  tree  not print the indentation lines, useful when used in conjunction with the -f option

不以階梯狀列出文件或目錄名稱

-I

Do not list those files that match the wild-card pattern

不顯示符合范本樣式的文件或目錄名稱

-l

Follows symbolic links if they point to directories, as if  they  were directories.  Symbolic links that will result in recursion are avoided when detected

如遇到性質為符號連接的目錄,直接列出該連接所指向的原始目錄

-n

Turn colorization off always, over-ridden by the -C option

不在文件和目錄清單加上色彩

-N

Print  non-printable  characters  as  is  instead of the default caret notation

直接列出文件和目錄名稱,包括控制字符

-p

Print the file type and permissions for each file (as per ls -l)

列出權限標示

-P

List only those files that match the  wild-card  pattern.   Note:  you must  use  the -a option to also consider those files beginning with a

 dot .for matching.  Valid wildcard operators are *(any  zero  or more  characters),  ?’  (any  single  character), [...](any single character listed between brackets (optional  -  (dash)  for  character range  may be used: ex: [A-Z]), and [^...](any single character not listed in brackets) and |separates alternate patterns

只顯示符合范本樣式的文件或目錄名稱

-q

Print non-printable characters in filenames as question marks  instead of the default caret notation

"?"號取代控制字符,列出文件和目錄名稱

-s

Print the size of each file in bytes along with the name

列出文件或目錄大小

-t

Sort the output by last modification time instead of alphabetically

用文件和目錄的更改時間排序

-u

Print the username, or UID # if no username is available, of the file

列出文件或目錄的擁有者名稱,沒有對應的名稱時,則顯示用戶識別碼

-x

Stay on the current file-system only.  Ala find -xdev

將范圍局限在現行的文件系統中,若指定目錄下的某些子目錄,其存放於另一個文件系統上,則將該子目錄予以排除在尋找范圍外

【示例】:

1、基本用法:以樹形羅列目錄

[root@oldboy ~]# tree oldboy

oldboy

├── ext

   └── oldboy

├── jeacen

├── oldboy

├── test

├── wodi.gz

├── xiaodong

├── xiaofan

├── xingfujie

└── yingsui.gz

 

2、只羅列目錄

[root@oldboy ~]# tree -d

.

├── data

   ├── dir1

   ├── dir2

   ├── dir3

   ├── dir4

   └── dir5

└── oldboy

    ├── ext

    ├── test

    ├── xiaodong

    ├── xiaofan

    └── xingfujie

 

3、只羅列目錄,顯示深度為1

[root@oldboy ~]# tree -Ld 1

.

├── data

└── oldboy

 

4、顯示完整的相對路徑

[root@oldboy ~]# tree -f data

data

├── data/c.txt

├── data/dir1

├── data/dir2

├── data/dir3

├── data/dir4

├── data/dir5

├── data/d.txt

├── data/e.txt

├── data/oldboy.txt

└── data/test.txt

 

5、不以階梯狀形式顯示

[root@oldboy ~]# tree -i data

data

c.txt

dir1

dir2

dir3

dir4

dir5

d.txt

e.txt

oldboy.txt

test.txt

 

6、為顯示內容添加提示符

[root@oldboy ~]# tree -F oldboy

oldboy

├── ext/

   └── oldboy

├── jeacen

├── oldboy

├── test/

├── wodi.gz

├── xiaodong/

├── xiaofan/

├── xingfujie/

└── yingsui.gz

 

 

 

47. 【命令】:id

【功能說明】:

print real and effective user and group IDs  #顯示用戶ID及組ID

【語法格式】:

id [OPTION]... [USERNAME]

【選項參數】:

參數

說明

簡解

-a

ignore, for compatibility with other versions

 

-Z

print only the security context of the current user

 

-g

print only the effective group ID

顯示用戶所屬群組的ID

-G

print all group IDs

顯示用戶所有群組的ID

-n

print a name instead of a number, for -ugG

顯示用戶所屬群組的名稱

-r

print the real ID instead of the effective ID, with -ugG

顯示實際ID

-u

print only the effective user ID

顯示用戶ID

【示例】:

1、基本用法:顯示用戶ID及組ID

[oldboy@oldboy ~]$ id oldboy

 

2、顯示用戶ID

[oldboy@oldboy ~]$ id -u oldboy

500

 

 

48. 【命令】:ln

【功能說明】:

make links between files  #創建鏈接

【語法格式】:

ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)

ln [OPTION]... TARGET                  (2nd form)

ln [OPTION]... TARGET... DIRECTORY     (3rd form)

ln [OPTION]... -t DIRECTORY TARGET...  (4th form)

【選項參數】:

參數

說明

簡解

-f

remove existing destination files

鏈接時先將與dist同檔案名的檔案刪除

-d

allow the superuser to attempt to hard link  directories  (note:  will probably fail due to system restrictions, even for the superuser)

允許系統管理者硬鏈接自己的目錄

-i

prompt whether to remove destinations

在刪除與dist同名的檔案時先進行詢問

-n

treat  destination  that  is  a symlink to a directory as if it were a normal file

在進行軟鏈接時,將dist視為一般的檔案

-s

make symbolic links instead of hard links

創建軟鏈接

-v

print name of each linked file

在鏈接之前顯示其檔案名

-b

like --backup but does not accept an argument

將在鏈接時會被覆寫或刪除的檔案進行備份

-S

override the usual backup suffix

將備份的檔案都加上SUFFIX的字尾

【示例】:

1、基本用法:創建文件硬鏈接

[root@oldboy ~]# ln oldboy.txt oldboy_hard_link

[root@oldboy ~]# ls

anaconda-ks.cfg  install.log         log2.txt  oldboy_hard_link  oldboy.txt

data             install.log.syslog  oldboy    oldboy.tar.gz     test.txt

 

2、基本用法:創建文件軟鏈接

[root@oldboy ~]# ln -s oldboy.txt oldboy_soft_link

[root@oldboy ~]# ls

anaconda-ks.cfg  install.log         log2.txt  oldboy_hard_link  oldboy.tar.gz  test.txt

data             install.log.syslog  oldboy    oldboy_soft_link  oldboy.txt

 

 

49. 【命令】:du

【功能說明】:

estimate file space usage  #查看文件和目錄大小

【語法格式】:

du [OPTION]... [FILE]...

du [OPTION]... --files0-from=F

【選項參數】:

參數

說明

簡解

-a

write counts for all files, not just directories

顯示目錄中所有文件大小

-b

equivalent to --apparent-size --block-size=1

顯示目錄或文件大小時,以byte為單位

-c

produce a grand total

顯示一個大小總和

-k

like --block-size=1K

KB為單位輸出

-m

like --block-size=1M

MB為單位輸出

-s

display only a total for each argument

僅顯示總計,只列出最后加總的值

-h

print sizes in human readable format (e.g., 1K 234M 2G)

人類可讀

-x

skip directories on different file systems

以一開始處理時的文件系統為准,若遇上其它不同的文件系統目錄則略過

-L

dereference all symbolic links

顯示選項中所指定符號鏈接的源文件大小

-S

do not include size of subdirectories

顯示個別目錄的大小時,並不含其子目錄的大小

-X

exclude files that match any pattern in FILE

 

--exclude=PATTERN

exclude files that match PATTERN

略過指定的目錄或文件

-D

dereference only symlinks that are listed on the command line

顯示指定符號鏈接的源文件大小

-H

equivalent to --dereference-args (-D)

-h參數相同,但是是以1000為換算單位

-l

count sizes many times if hard linked

重復計算硬鏈接的文件

【示例】:

1、基本用法:顯示目錄或文件所占空間

[root@oldboy ~]# du

4       ./oldboy/xiaofan

8       ./oldboy/ext

4       ./oldboy/xingfujie

4       ./oldboy/xiaodong

4       ./oldboy/test

40      ./oldboy

4       ./data/dir5

4       ./data/dir1

4       ./data/dir2

4       ./data/dir3

4       ./data/dir4

28      ./data

172     .

 

2、顯示指定文件所占空間

[root@oldboy ~]# du oldboy.txt

4       oldboy.txt

 

3、顯示指定目錄所占空間

[root@oldboy ~]# du oldboy

4       oldboy/xiaofan

8       oldboy/ext

4       oldboy/xingfujie

4       oldboy/xiaodong

4       oldboy/test

40      oldboy

 

4、以易讀模式顯示

[root@oldboy ~]# du -h

4.0K    ./oldboy/xiaofan

8.0K    ./oldboy/ext

4.0K    ./oldboy/xingfujie

4.0K    ./oldboy/xiaodong

4.0K    ./oldboy/test

40K     ./oldboy

4.0K    ./data/dir5

4.0K    ./data/dir1

4.0K    ./data/dir2

4.0K    ./data/dir3

4.0K    ./data/dir4

28K     ./data

172K    .

 

 

50. 【命令】:which

【功能說明】:

shows the full path of (shell) commands  #從PATH變量所在路徑查找二進制命令

【語法格式】:

which [options] [--] programname [...]

【選項參數】:

參數

說明

簡解

 

 

 

 

 

 

 

 

 

 

 

 

【示例】:

1、基本用法:查找命令

[root@oldboy ~]# which cat

/bin/cat

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM