學習linux命令,看這篇2W多字的linux命令詳解

用心分享,共同成長css

沒有什麼比天天進步一點點更重要了java

本文已收錄到個人github:github.com/midou-tech/… ,歡迎star和issues。node

序言

 本篇文章主要講解了一些linux經常使用命令,主要講解模式是,命令介紹、命令參數格式、命令參數、命令經常使用參數示例。因爲linux命令較多,我還特地選了一些平常使用較爲頻繁的命令進行講解,但仍是免不了文章很長,建議你們收藏起來,用到的時候不會了再來閱讀。固然學習linux命令最好的方法是學會使用linux自帶的man手冊,全部linux命令規範和使用細則都會在該手冊中講解的很清楚,我在書寫的過程當中也是參考該手冊和平常使用狀況。linux

若是還有哪些是你們但願再補充的命令,能夠留言給我,我會持續更新該文章,同時也做爲我學習和工做的手冊。git

Linux初級指令

ls ——List

ls 介紹

 這是我學Linux的第一個命令,相信也是不少人學習Linux的第一個命令。ls全稱list.github

List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.web

列出有關文件的信息(默認爲當前目錄)。若是沒有指定-cftuvSUX或——Sort,則按字母順序排序。編程

 官方的說的很清楚,默認列出當前目錄,因此能夠列出其餘目錄或者路徑下的文件信息或者目錄信息。vim

eg:安全

$ls /etc/hosts
/etc/hosts
複製代碼

 ls還能夠列出指定目錄下的文件列表。

image-20191221161638964
image-20191221161638964

ls 參數格式

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

ls命令參數

-a 列出指定目錄下的全部文件,包括隱藏文件

-c 使用最後一次更改文件狀態以進行排序(-t)或長時間打印(-l)的時間

-h 與-l選項一塊兒使用時,請使用單位後綴:Byte、Kilobyte、mete、gb、tb和Petabyte,以便使用以2爲基數的大小將數字減小到3或更少

-l 長格式列表。(見下文)。若是輸出到終端,則全部文件大小的總和將輸出到長清單前面的一行中

-n 以數字形式顯示用戶和組id,而不是在長(-l)輸出中轉換爲用戶或組名。這個選項默認打開-l選項

-o 以長格式列出,但省略組id

-s 顯示每一個文件實際使用的文件系統塊的數量,以512字節爲單位,其中部分單元四捨五入爲下一個整數值

-t 在按照字典順序對操做數排序以前,先按修改的時間排序(最近修改的是first)

-u 使用最後一次訪問的時間,而不是最後一次修改文件進行排序

ls 用法示例:

$ ls
test  tmp
$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  test  tmp
#能夠看到經過ls -a 能夠查看當前目錄影藏的文件,Linux下(.)開頭的文件是隱藏文件。
$ ls -l
total 4
-rw-r--r-- 1 test hero    0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4096 Dec 21 19:54 tmp
$ ls -lh
total 4.0K
-rw-r--r-- 1 test hero    0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4.0K Dec 21 19:54 tmp
$ ls -ll
total 4
-rw-r--r-- 1 test hero    0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4096 Dec 21 19:54 tmp
$ ls -alh
total 28K
drwx------   3 test hero 4.0K Dec 21 19:54 .
drwxr-xr-x. 19 root  root 4.0K Aug  1 10:41 ..
-rw-------   1 test hero  226 Dec 21 19:54 .bash_history
-rw-r--r--   1 test hero   18 Aug  3  2016 .bash_logout
-rw-r--r--   1 test hero  193 Aug  3  2016 .bash_profile
-rw-r--r--   1 test hero  231 Aug  3  2016 .bashrc
-rw-r--r--   1 test hero    0 Dec 21 19:54 test
drwxr-xr-x   2 test hero 4.0K Dec 21 19:54 tmp
$ ls -o
total 4
-rw-r--r-- 1 test    0 Dec 21 19:54 test
drwxr-xr-x 2 test 4096 Dec 21 19:54 tmp
$ ls -oh
total 4.0K
-rw-r--r-- 1 test    0 Dec 21 19:54 test
drwxr-xr-x 2 test 4.0K Dec 21 19:54 tmp
複製代碼

pwd —— Print Working Directory

pwd介紹

 打印當前工做目錄的完整路徑名。(print name of current/working directory)

參數格式

pwd [OPTION]...

pwd 用法展現

[test@Mfate171193 /home/test] 20:06
pwd
/home/test
複製代碼

touch (change file timestamps)

touch介紹

Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h is supplied.

 將每一個文件的訪問和修改時間更新爲當前時間。除非提供-c或-h,不然將不存在的FILE參數建立爲空。

touch參數格式

touch [OPTION]... FILE...

touch命令參數

-a 或--time=atime或--time=access或--time=use 只更改存取時間。

-c 或--no-create 不創建任何文檔。

-d 使用指定的日期時間,而非如今的時間。

-f 此參數將忽略不予處理,僅負責解決BSD版本touch指令的兼容性問題。

-m 或--time=mtime或--time=modify 只更改變更時間。

-r 把指定文檔或目錄的日期時間,通通設成和參考文檔或目錄的日期時間相同。

-t 使用指定的日期時間,而非如今的時間。

用法示例

#建立三個文件
$ touch test1 test2 test3
#不建立文檔
$ touch -c test5  
$ ls
test1  test2  test3
#能夠看到只建立了test一、test二、test3 , -c不創建任何文件
$ touch -t 201911110000 test1
stat test*
#stat命令能夠查看文件的詳細變動時間,能夠test1文件的最後修改時間爲201911110000,說明touch -t能夠修改文件最後訪問時間。這個參數仍是頗有用的,你能夠把你最近訪問的時間修改成一個很早的時間。能夠作一些有趣的事情,哈哈。
  File: ‘test1’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 360736      Links: 1
Access: (0644/-rw-r--r--)  Uid: (14060/   localhost)   Gid: ( 1001/    hero)
Access: 2019-11-11 00:00:00.000000000 +0800
Modify: 2019-11-11 00:00:00.000000000 +0800
Change: 2019-12-21 20:58:11.290761038 +0800
 Birth: -
  File: ‘test2’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 360738      Links: 1
Access: (0644/-rw-r--r--)  Uid: (14060/   localhost)   Gid: ( 1001/    hero)
Access: 2019-12-21 20:56:34.523761038 +0800
Modify: 2019-12-21 20:56:34.523761038 +0800
Change: 2019-12-21 20:56:34.523761038 +0800
 Birth: -
  File: ‘test3’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 360740      Links: 1
Access: (0644/-rw-r--r--)  Uid: (14060/   localhost)   Gid: ( 1001/    hero)
Access: 2019-12-21 20:56:34.523761038 +0800
Modify: 2019-12-21 20:56:34.523761038 +0800
Change: 2019-12-21 20:56:34.523761038 +0800
 Birth: -
複製代碼

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

cat介紹

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

將FILE或標準輸入鏈接到標準輸出。

cat 參數格式

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

cat命令參數

-A, --show-all 等價於 -vET

-b, --number-nonblank 對非空輸出行編號

-e 等價於 -vE

-E, --show-ends 在每行結束處顯示

-n, --number 對輸出的全部行編號,由1開始對全部輸出的行數編號

-s, --squeeze-blank 有連續兩行以上的空白行,就代換爲一行的空白行

-t 與 -vT 等價

-T, --show-tabs 將跳格字符顯示爲 ^I

-u (被忽略)

-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 以外

cat經常使用參數示例

$ cat test  #展現文件內容
-A, --show-all      等價於 -vET
-b, --number-nonblank  對非空輸出行編號
-e            等價於 -vE

$ cat -n test  #展現文件內容而且展現行號
     1    -A, --show-all      等價於 -vET
     2    -b, --number-nonblank  對非空輸出行編號
     3    -e            等價於 -vE
複製代碼

tac命令與cat命令展現內容相反,不能帶行號輸出。

$ tac test
-e            等價於 -vE
-b, --number-nonblank  對非空輸出行編號
-A, --show-all      等價於 -vET
複製代碼

mkdir —— Make Directory

mkdir介紹

Create the DIRECTORY(ies), if they do not already exist.

若是目錄不存在,則建立目錄。

mkdir參數格式

mkdir [OPTION]... DIRECTORY...

mkdir命令參數

-m, --mode=模式,設定權限<模式> (相似 chmod),而不是 rwxrwxrwx 減 umask

-p, --parents 能夠是一個路徑名稱。此時若路徑中的某些目錄尚不存在,加上此選項後,系統將自動創建好那些尚不存在的目錄,即一次能夠創建多個目錄;

-v, --verbose 每次建立新目錄都顯示信息

--help 顯示此幫助信息並退出

--version 輸出版本信息並退出

mkdir經常使用參數示例

#建立目錄文件test
$ mkdir test
#連續建立
$ mkdir -p test1/tmp
$ ls
test  test1
#建立時置頂目錄權限
#tmp目錄擁有可執行權限,Linux文件權限問題後期文章會詳細講到,記得關注我
$ mkdir -pm 777 test2/tmp
$ ls -lh
total 12K
drwxr-xr-x 2 localhost hero 4.0K Dec 21 21:39 test
drwxr-xr-x 3 localhost hero 4.0K Dec 21 21:40 test1
drwxr-xr-x 3 localhost hero 4.0K Dec 21 21:40 test2
$ ls
test  test1  test2
#-v 參數可肯定文件是否已經存在,若是不存在則會建立,並顯示以下信息
$ mkdir -v test
mkdir: cannot create directory ‘test’: File exists

$ mkdir -v test7
mkdir: created directory ‘test7’
複製代碼

cd —— Change Directory

cd介紹

 切換當前目錄至指定目錄

經常使用參數示例

#打印當前目錄到標準輸出
pwd
/Users/localhost
#切換到目錄/
$cd /
複製代碼

 cd命令沒什麼參數就是切換目錄到指定路徑下,較爲簡單,可是使用評率極高。

rm&rmdir —— Remove Directory

rm介紹

The rm utility attempts to remove the non-directory type files specified on the command line. If the permissions of the file do not permit writing, and the standard input device is a terminal, the user is prompted (on the standard error output) for confirmation.

rm實用程序嘗試刪除命令行上指定的非目錄類型文件。 若是文件的權限不容許寫入,而且標準輸入設備是終端,則會提示用戶(在標準錯誤輸出上)進行確認。

 rm命令使用時仍是須要注意的,他的刪除恢復比較麻煩,有些系統會自帶-i參數,輸入命令以後還有一個確認步驟,有些是直接刪掉了,是真刪掉,從內存抹掉那種(其實底層是讓該文件指針不指向該文件的內存塊,內存上的內容原則上是存在的,可是恢復會比較複雜,須要掃描整塊內存塊才能拿到內容)。不要輕易刪掉你寫的重要代碼,hh。

rm參數格式

rm [-dfiPRrvW] file ...

rm命令參數

-f, --force 忽略不存在的文件,從不給出提示。

-i, --interactive 進行交互式刪除

-r, -R, --recursive 指示rm將參數中列出的所有目錄和子目錄均遞歸地刪除。

-d, --dir 刪除空目錄

rm經常使用參數示例

# 建立三個文件
$ touch tmp.cc tmp.java tmp.py tmp.go
#建立目錄文件
$ mkdir -p linux/test
#查看文件是否建立成功
$ ls
linux    tmp.cc   tmp.go   tmp.java tmp.py
#刪除文件,並進行提示
$ rm -i tmp.cc
remove tmp.cc? y
#強制刪除
$ rm  -f tmp.go
#刪除目錄
$ rm -f linux  #刪除目錄失敗
rm: linux: is a directory
#循環刪除目錄下全部文件
$ rm -rf linux  #刪除目錄成功,
$ ls
tmp.java tmp.py  
複製代碼

 rmdir==rm -d 刪除空目錄

mv —— Move

mv介紹

In its first form, the mv utility renames the file named by the source operand to the destination path named by the target operand. This form is assumed when the last operand does not name an already existing directory.

In its second form, mv moves each file named by a source operand to a destination file in the existing directory named by the directory operand. The destination path for each operand is the pathname produced by the concatenation of the last operand, a slash, and the final pathname component of the named file.

總結下,就是移動目錄或者文件到置頂目錄下,同時具備重命名的功能。

mv參數格式

mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory

mv命令參數

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

-f :force 強制的意思,若是目標文件已經存在,不會詢問而直接覆蓋;

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

-n:不要覆蓋現有文件。 (-n選項將覆蓋之前的任何-f或-i選項。)

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

mv經常使用參數示例

##修改文件名
$ touch tmp.cc

$ ls
tmp.cc

$ mv tmp.cc tmp.java

$ ls
tmp.java
#移動文件或者目錄
pwd
/Users/localhost/test
#移動文件並重命名
$ mv /Users/localhost/logs/tmp.txt ./tmp.log 

$ ls /Users/localhost/logs/
discover-client metabase        tesla

$ ls ./
tmp.java tmp.log
#移動目錄並重命名
$ mv /Users/localhost/logs/tesla  ./tesla.ba 

$ ls
tesla.ba tmp.java tmp.log
複製代碼

 這個命令在寫makefile文件的時候用起來很舒服,能夠把編譯的結果移到指定目錄並重命名。

cp —— Copy

cp介紹

In the first synopsis form, the cp utility copies the contents of the source_file to the target_file. In the second synopsis form, the contents of each named source_file is copied to the destination target_directory. The names of the files themselves are not changed. If cp detects an attempt to copy a file to itself, the copy will fail.

cp實用程序將source_file的內容複製到target_file。 在第二個大綱格式中,每一個命名的source_file的內容都複製到目標target_directory。 文件自己的名稱不會更改。 若是cp檢測到嘗試將文件複製到自身的嘗試,則複製將失敗。

cp參數格式

cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

命令參數

-a, --archive 等於-dR --preserve=all

--backup[=CONTROL 爲每一個已存在的目標文件建立備份

-b 相似--backup 但不接受參數

--copy-contents 在遞歸處理是複製特殊文件內容

-d 等於--no-dereference --preserve=links

-f, --force 若是目標文件沒法打開則將其移除並重試(當 -n 選項

​ 存在時則不需再選此項)

-i, --interactive 覆蓋前詢問(使前面的 -n 選項失效)

-H 跟隨源文件中的命令行符號連接

-l, --link 連接文件而不復制

-L, --dereference 老是跟隨符號連接

-n, --no-clobber 不要覆蓋已存在的文件(使前面的 -i 選項失效)

-P, --no-dereference 不跟隨源文件中的符號連接

-p 等於--preserve=模式,全部權,時間戳

--preserve[=屬性列表 保持指定的屬性(默認:模式,全部權,時間戳),若是

​ 可能保持附加屬性:環境、連接、xattr 等

-R, -r, --recursive 複製目錄及目錄內的全部項目

經常使用參數示例

$ cat tmp.cc
change world

#拷貝文件內容
$ cp tmp.cc tmp.java

$ cat tmp.java
change world
複製代碼

echo

echo介紹

The echo utility writes any specified operands, separated by single blank (') characters and followed by a newline (\n') character, to the standard output.

echo實用程序將任何指定的操做數寫入標準輸出,這些操做數由單個空格()字符分隔,後跟換行符(\ n')字符。

這條命令較爲簡單,經常使用來打印變量、文本內容到,例如:

echo "change world"
change world

#s輸出PWD環境變量的值
echo $PWD
/Users/localhost/test
複製代碼

$PWD 是取當前路徑,而後echo到標準輸出,通常echo \$name 用來查看某個環境變量的值

head&tail

head介紹

This filter displays the first count lines or bytes of each of the specified files, or of the standard input if no files are specified. If count is omitted it defaults to 10.

此過濾器顯示每一個指定文件或標準輸入(若是未指定文件)的前幾行或字節。

If more than a single file is specified, each file is preceded by a header consisting of the string ==> XXX <=='' whereXXX'' is the name of the file.

若是省略count,則默認爲10.若是指定了多個文件,則每一個文件的頭均由字符串==> XXX <==''組成,其中XXX''爲文件名 文件。

head參數格式

head [-n count | -c bytes] [file ...]

head經常使用參數示例

-n 展現前n行

-c 展現前n個字符

head經常使用參數示例

$ cat -n test.txt #這就用上前面的cat命令的-n參數,要學會學以至用哦。
     1    用
     2    心
     3    分
     4    享,
     5    共
     6    同
     7    成
     8    長.
     9
    10    沒
    11    有
    12    什
    13    麼
    14    比
    15    你
    16    每
    17    天
    18    進
    19    步
    20    一
    21    點
    22    更
    23    實
    24    在
    25    了
    26    .

$ head test.txt  #默認展現10行



享,



長.



$ head -n15 test.txt #展現15行



享,



長.








$ head -c23 test.txt #展現前23個字符,中文一個漢字並不是一個字符的(utf-8編碼中文字符長度是可變的)



享,

複製代碼

tail命令徹底和他相反,是從尾部開始展現文本,可是他的參數用法更多。

-f 循環讀取

-q 不顯示處理信息

-v 顯示詳細的處理信息

-c<數目> 顯示的字節數

-n<行數> 顯示行數

--pid=PID 與-f合用,表示在進程ID,PID死掉以後結束.

-q, --quiet, --silent 從不輸出給出文件名的首部

-s, --sleep-interval=S 與-f合用,表示在每次反覆的間隔休眠S秒

tail和head命令常常用來查看日誌,像我如今基本天天都會用,個人日誌文件沒辦法用vim或者cat這樣去看(由於我負責的業務日誌量天天都是幾十個G),要麼用tail,要麼用more&less(下面會講)。

more&less

more介紹

Less is a program similar to more (1), but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1). Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.)

more每次打開文件不是所有把文件讀入內存而是流式讀取,不會由於vi|vim某個大文件而形成系統oom。

more&less最重要的一點就是流式讀取,支持翻頁,像cat命令是所有讀取輸出到標準輸出,若是文件太大會把屏幕刷滿的,根本沒辦法看。

more參數格式

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

more命令參數

+n 從笫n行開始顯示

-n 定義屏幕大小爲n行

+/pattern 在每一個檔案顯示前搜尋該字串(pattern),而後從該字串前兩行以後開始顯示

-c 從頂部清屏,而後顯示

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

-l 忽略Ctrl+l(換頁)字符

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

-s 把連續的多個空行顯示爲一行

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

less 與 more 相似,但使用 less 能夠隨意瀏覽文件,而 more 僅能向前移動,卻不能向後移動,並且 less 在查看以前不會加載整個文件

wc

wc介紹

The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output. A line is defined as a string of characters delimited by a charac-ter. Characters beyond the final character will not be included in the line count.

wc實用程序顯示每一個輸入文件或標準輸入(若是未指定文件)中每一個輸入文件中包含的行數,字數和字節數。 一行定義爲由字符分隔的字符串。 最後一個字符以後的字符將不包括在行數中。

這條命令對我來講仍是比較深入的,我剛學習編程不久的時候,我感受我寫的代碼不少了,那個時候我就很想知道我寫了多少行代碼了,一時興起,說幹就幹,直接寫了個程序去統計了一把,寫完以後還感受本身蠻厲害的,誰知道以後學習到這個wc,而後就以爲本身仍是嫩了點,仍是要多學習。

wc參數格式

wc [-clmw] [file ...]

wc命令參數

-c 統計字節數。

-l 統計行數。

-m 統計字符數。這個標誌不能與 -c 標誌一塊兒使用。

-w 統計字數。一個字被定義爲由空白、跳格或換行字符分隔的字符串。

-L 打印最長行的長度。

經常使用參數示例

$ ls
test.txt

$ wc test.txt
      26      26     103 test.txt

$ wc -l test.txt  #直接一把統計行數
      26 test.txt

$ wc -c test.txt
     103 test.txt
複製代碼

date & cal

date介紹

When invoked without arguments, the date utility displays the current date and time. Otherwise, depending on the options specified, date will set the date and time or print it in a user-defined way.

The date utility displays the date and time read from the kernel clock. When used to set the date and time, both the kernel clock and the hardware clock are updated.

Only the superuser may set the date, and if the system securelevel (see securelevel(7)) is greater than 1, the time may not be changed by more than 1 second.

當不帶參數調用時,date實用程序將顯示當前日期和時間。 不然,根據指定的選項,日期將設置日期和時間或以用戶定義的方式打印日期和時間。

date實用程序顯示從內核時鐘讀取的日期和時間。 當用於設置日期和時間時,內核時鐘和硬件時鐘都將更新。

只有超級用戶能夠設置日期,而且若是系統安全級別(請參閱securelevel(7))大於1,則時間更改不得超過1秒。

date參數格式

date [-jRu] [-r seconds | filename] [-v [+|-]val[ymwdHMS]] ... [+output_fmt] date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss] date [-jnRu] -f input_fmt new_date [+output_fmt] date [-d dst] [-t minutes_west]

date命令參數

%H 小時(以00-23來表示)。

%I 小時(以01-12來表示)。

%K 小時(以0-23來表示)。

%l 小時(以0-12來表示)。

%M 分鐘(以00-59來表示)。

%P AM或PM。

%r 時間(含時分秒,小時以12小時AM/PM來表示)。

%s 總秒數。起算時間爲1970-01-01 00:00:00 UTC。

%S 秒(以本地的慣用法來表示)。

%T 時間(含時分秒,小時以24小時制來表示)。

%X 時間(以本地的慣用法來表示)。

%Z 市區。

%a 星期的縮寫。

%A 星期的完整名稱。

%b 月份英文名的縮寫。

%B 月份的完整英文名稱。

%c 日期與時間。只輸入date指令也會顯示一樣的結果。

%d 日期(以01-31來表示)。

%D 日期(含年月日)。

%j 該年中的第幾天。

%m 月份(以01-12來表示)。

%U 該年中的週數。

%w 該周的天數,0表明週日,1表明週一,異詞類推。

%x 日期(以本地的慣用法來表示)。

%y 年份(以00-99來表示)。

%Y 年份(以四位數來表示)。

%n 在顯示時,插入新的一行。

%t 在顯示時,插入tab。

MM 月份(必要)

DD 日期(必要)

hh 小時(必要)

mm 分鐘(必要)

ss 秒(選擇性)

-d<字符串> 顯示字符串所指的日期與時間。字符串先後必須加上雙引號。

-s<字符串> 根據字符串來設置日期與時間。字符串先後必須加上雙引號。

-u 顯示GMT。

經常使用參數示例

$ date
2019年12月22日 星期日 21時28分29秒 CST

$ date '+%c'
日 12/22 21:28:33 2019

$  date '+%D'
12/22/19

$  date '+%T'
21:28:50

$ date '+%X'
21時29分00秒
複製代碼

cal介紹

cal命令能夠用來顯示公曆(陽曆)日曆。

cal參數格式

cal [-3hjy] [-A number] [-B number] [[month] year] cal [-3hj] [-A number] [-B number] -m month [year] ncal [-3hjJpwy] [-A number] [-B number] [-s country_code] [[month] year] ncal [-3hJeo] [-A number] [-B number] [year] ncal [-CN] [-H yyyy-mm-dd] [-d yyyy-mm]

cal命令參數

-1 顯示一個月的月曆

-3 顯示系統前一個月,當前月,下一個月的月曆

-s 顯示星期天爲一個星期的第一天,默認的格式

-m 顯示星期一爲一個星期的第一天
-j 顯示在當年中的第幾天(一年日期按天算,從1月1號算起,默認顯示當前月在一年中的天數)
-y 顯示當前年份的日曆

cal經常使用參數示例

$ cal
      十二月 2019
日 一 二 三 四 五 六
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31


$ ncal
    十二月 2019
一      2  9 16 23 30
二      3 10 17 24 31
三      4 11 18 25
四      5 12 19 26
五      6 13 20 27
六      7 14 21 28
日   1  8 15 22 29
複製代碼

which

which介紹

The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.

which命令的做用是,在PATH變量指定的路徑中,搜索某個系統命令的位置,而且返回第一個搜索結果。也就是說,使用which命令,就能夠看到某個系統命令是否存在,以及執行的究竟是哪個位置的命令。

which參數格式

-n 指定文件名長度,指定的長度必須大於或等於全部文件中最長的文件名。

-p 與-n參數相同,但此處的包括了文件的路徑。

-w 指定輸出時欄位的寬度。

which經常使用參數示例

which ls
alias ls='ls --color=auto'
    /bin/ls

which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /bin/alias
    /usr/bin/which
複製代碼

whereis

whereis介紹

whereis命令只能用於程序名的搜索,並且只搜索二進制文件(參數-b)、man說明文件(參數-m)和源代碼文件(參數-s)。若是省略參數,則返回全部信息。

whereis參數格式

whereis [program ...]

whereis命令參數

-b 定位可執行文件。

-m 定位幫助文件。

-s 定位源代碼文件。

-u 搜索默認路徑下除可執行文件、源代碼文件、幫助文件之外的其它文件。

-B 指定搜索可執行文件的路徑。

-M 指定搜索幫助文件的路徑。

-S 指定搜索源代碼文件的路徑。

whereis經常使用參數示例

$ whereis ls
/bin/ls

$ whereis whereis
/usr/bin/whereis
複製代碼

nl

nl介紹

nl命令在linux系統中用來計算文件中行號。nl 能夠將輸出的文件內容自動的加上行號!其默認的結果與 cat -n 有點不太同樣, nl 能夠將行號作比較多的顯示設計,包括位數與是否自動補齊 0 等等的功能。

nl參數格式

nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num] [-n format] [-s sep] [-v startnum] [-w width] [file]

nl命令參數

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

-b a :表示不管是否爲空行,也一樣列出行號(相似 cat -n);

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

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

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

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

-n rz :行號在本身欄位的最右方顯示,且加 0 ;

-w :行號欄位的佔用的位數。

-p 在邏輯定界符處不從新開始計算。

nl經常使用參數示例

$ nl test.txt
     1    用
     2    心
     3    分
     4    享,
     5    共
     6    同
     7    成
     8    長.

     9    沒
    10    有
    11    什
    12    麼
    13    比
    14    你
    15    每
    16    天
    17    進
    18    步
    19    一
    20    點
    21    更
    22    實
    23    在
    24    了
    25    .
複製代碼

ps

ps介紹

The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals.

ps實用程序顯示標題行,其後是包含有關具備控制終端的全部進程的信息的行。

ps參數格式

ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]] [-g grp[,grp...]] [-u uid[,uid...]] [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]] ps [-L]

ps命令參數

a 顯示全部進程

-a 顯示同一終端下的全部程序

-A 顯示全部進程

c 顯示進程的真實名稱

-N 反向選擇

-e 等於「-A」

e 顯示環境變量

f 顯示程序間的關係

-H 顯示樹狀結構

r 顯示當前終端的進程

T 顯示當前終端的全部程序

u 指定用戶的全部進程

-au 顯示較詳細的資訊

-aux 顯示全部包含其餘使用者的行程

-C<命令> 列出指定命令的情況

--lines<行數> 每頁顯示的行數

--width<字符數> 每頁顯示的字符數

ps經常使用參數示例

#查看全部進程
$ps -a
#查看進程的環境變量和程序間的關係
$ps -ef
複製代碼

kill&killall

kill介紹

The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

命令kill將指定的信號發送到指定的進程或進程組。 若是未指定信號,則發送TERM信號。 TERM信號將殺死不捕獲該信號的進程。 對於其餘過程,可能須要使用KILL(9)信號,由於沒法捕獲該信號。

kill參數格式

kill [-s signal|-p] [-q sigval] [-a] [--] pid...
kill -l [signal]
複製代碼

kill命令參數

-l 信號,若果不加信號的編號參數,則使用「-l」參數會列出所有的信號名稱

-a 當處理當前進程時,不限制命令名和進程號的對應關係

-p 指定kill 命令只打印相關進程的進程號,而不發送任何信號

-s 指定發送信號

-u 指定用戶

kill經常使用參數示例

#查看當前系統信號
kill -l
 1) SIGHUP     2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT     7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV    12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT    17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN    22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM    27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS    34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4    39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9    44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14    49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11    54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6    59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1    64) SIGRTMAX
複製代碼
解釋

HUP 1 終端斷線
INT 2 中斷(同 Ctrl + C)
QUIT 3 退出(同 Ctrl + \)
TERM 15 終止
KILL 9 強制終止
CONT 18 繼續(與STOP相反, fg/bg命令)
STOP 19 暫停(同 Ctrl + Z)

kill -9 是咱們使用的最多的信號,其實這種方式一點也不優雅,應該使用kill -15信號,大部分程序接收到SIGTERM信號後,會先釋放本身的資源,而後再中止。可是也有程序可能接收信號後,作一些其餘的事情(若是程序正在等待IO,可能就不會立馬作出響應,等到io完成後在結束),也就是說,SIGTERM多半是會被阻塞的。

講到這裏,linux基本指令差很少了夠用了,差很少能夠上你的服務器上隨便搞搞了。可是也只是簡單看看,更多的操做指令請看進階部分。


因爲掘金長度要求,文章進階部分命令詳解 同步發在微信公衆號

微信搜索 DC952721 或者掃描二維碼,便可訂閱。

qrcode_for_gh_c0c6a2272e93_258
qrcode_for_gh_c0c6a2272e93_258
相關文章
相關標籤/搜索