1. 程式人生 > >Linux基礎命令總結(CentOS6.9, CentOS7.3)

Linux基礎命令總結(CentOS6.9, CentOS7.3)

Linux基礎命令學習總結,隨著學習的前進,將不斷完善。網友們,有錯誤之處,請給我留言改正喔,謝謝!

基礎操作

檔案、目錄、磁碟、檔案系統

cd pwd ls cat tac nl head tail less more hexdump od cp mv rm mkdir rmdir dd gedit nano file type iconv

使用者和組

BASHELL 特性

網路屬性

系統啟動,關機

程序

free lsblk lscpu

程式包管理

clear

clear the terminal screen 清空螢幕內容

Display the hostname 顯示主機名
* hostname [-s|–short]
-s | –short Display the shourt host name, This is the hostname cut at the firs dot. 顯示短主機名,取第一個點號之前的字串

[centos6@root ~]# hostname
centos6.zhubiao.science
# -s 取第一個點號之前的字串作為短主機名
[centos6@root ~]# hostname -s 
centos6

tty

print the file name of the terminal connected to standard input 顯示當前工作終端下的終端的名字
* 終端分為:
* 物理終端
物理控制檯:表示為 /dev/console, Linux單使用者模式下顯示為物理終端
* 虛擬終端
系統自帶6個虛擬終端,表示為/dev/tty#, #為1-6
* 圖形終端
* 偽終端
圖形介面開啟的命令列介面,通過ssh或telnet協議開啟的命令列介面均為偽終端,表現為/dev/pts/#

# 將執行級別切換為單使用者模式後,使用的將是物理終端,如下所示,當前執行級別為3的多使用者模式,將其切換為1後檢視終端。
[[email protected] ~]# tty /dev/tty1 [[email protected] ~]# runlevel #檢視執行級別 S 3 [[email protected] ~]# init 1 ... [[email protected] /]# runlevel 1 S [[email protected] /]# tty /dev/console

who

Who is logged on 顯示當前登入的使用者
* who [options]
-b #系統登入的時間
-r #當前執行級別

[centos7@root dir]# who
root     tty1         2017-07-15 16:20
root     pts/0        2017-07-15 07:24 (gateway)
[centos7@root dir]# who -b
         system boot  2017-07-14 20:54
[centos7@root dir]# who -r
         run-level 3  2017-07-14 20:54                   last=5

顯示當前登入的使用者名稱
* who

[centos7@root dir]# whoami
root

w

Show who is logged on and what they are doing. 顯示當前登入的使用者和正在執行的命令
* w

[[email protected] dir]# w 
 17:08:16 up 12:09,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      16:20   47:20   0.04s  0.04s -bash
root     pts/0    gateway          07:24    0.00s  3.05s  0.01s w

查詢外部命令的man手冊幫助文件所在的章節
* whatis command #等同於man -f

[[email protected] ~]# whatis ls
ls (1)               - list directory contents
ls (1p)              - list directory contents

[[email protected] ~]# whatis cp
cp (1)               - copy files and directories
cp (1p)              - copy files

[[email protected] ~]# whatis ifconfig
ifconfig (8)         - configure a network interface

[[email protected] ~]# man -f ls
ls (1)               - list directory contents
ls (1p)              - list directory contents

[[email protected] ~]# man -f cp
cp (1)               - copy files and directories
cp (1p)              - copy files

which

按照PATH路徑查詢命令後顯示其完整路徑
* which [options] [command]
-a 顯示命令所有PATH路徑
–skip-alias 不顯示別名

[centos6@root test]# which --skip-alias ls
/bin/ls
[centos6@root test]# which -a ls
alias ls='ls --color=auto'
    /bin/ls

查詢命令所在路徑,原始碼路徑,幫助手冊路徑
whereis [options] [command]

[[email protected] test]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

[[email protected] test]# whereis -b mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql

[[email protected] test]# whereis -m mysql
mysql: /usr/share/man/man1/mysql.1.gz

cd

Change the shell working dirctory 切換工作目錄
* cd [-P] DIR
-P 若DIR為符號連結目錄,cd DIR切換到實際的工作目錄,而不是符號連結目錄
cd - #切換到上一個工作目錄
cd ~ #切換到當前使用者的家目錄
cd ~USER #切換到USER使用者的家目錄,只有root有許可權切換到任意使用者的家目錄
cd .. #切換到上一級目錄

[centos6@root test]# pwd
/root/test
[centos6@root test]# cd ~  
[centos6@root ~]# pwd
/root
[centos6@root ~]# cd -
/root/test
[centos6@root test]# pwd
/root/test
[centos6@root test]# cd ~zb10
[centos6@root zb10]# pwd
/home/zb10
[centos6@root zb10]# cd ..
[centos6@root home]# pwd
/home
[centos6@root home]# 
# -P 切換到實際路徑的工作目錄下
[centos6@root test]# ll dir
lrwxrwxrwx. 1 root root 6 Jul 15 17:16 dir -> a/b/c/
[centos6@root test]# cd dir
[centos6@root dir]# pwd
/root/test/dir
[centos6@root dir]# cd -
/root/test
[centos6@root test]# cd -P dir
[centos6@root c]# pwd
/root/test/a/b/c

pwd

print the name of the current working dirctory 顯示當前工作目錄
* pwd [-P]
-P - 若當前工作目錄為符號連結路徑,加-P選項顯示實際路徑

[centos7@root test]# pwd
/root/test  #顯示當前工作目錄
# -P 顯示出實際路徑而不是符號連結的路徑
[centos7@root test]# mkdir -p a/b/c 
[centos7@root test]# ln -s a/b/c dir  
[centos7@root test]# ll dir
lrwxrwxrwx. 1 root root 5 Jul 15 15:33 dir -> a/b/c
[centos7@root test]# cd dir 
[centos7@root dir]# pwd  
/root/test/dir
[centos7@root dir]# pwd -P  #加了-P選項,顯示實際路徑
/root/test/a/b/c

ls

List dirctory contents 列出當前或指定目錄(或所有子目錄)下的內容
* ls [options] [dirs]
- l 列出詳細內容
-a | –all 列出所有內容包括. ..
-A 列出所有內容,不包括. ..
-d 僅列出指定的目錄
–full-time 列出完整的修改時間格式

[[email protected] test]# ls
a dir
[[email protected] test]# ls -l
total 4
drwxr-xr-x. 3 root root 4096 Jul 15 17:15 a
lrwxrwxrwx. 1 root root 6 Jul 15 17:16 dir -> a/b/c/
[[email protected] test]# ll
total 4
drwxr-xr-x. 3 root root 4096 Jul 15 17:15 a
lrwxrwxrwx. 1 root root 6 Jul 15 17:16 dir -> a/b/c/
[[email protected] test]# ls -a
. .. a dir
[[email protected] test]# ls -A
a dir
[[email protected] test]# ls /root/test/
a dir
[[email protected] test]# ls --full-time /root/test/
total 4
drwxr-xr-x. 3 root root 4096 2017-07-15 17:15:41.550009093 +0800 a
lrwxrwxrwx. 1 root root 6 2017-07-15 17:16:44.490009154 +0800 dir -> a/b/c/

cat

Concatenate files and print on the standard output # 讀取一個或多個檔案(也可以從鍵盤輸入) 並輸出
* cat [options] [files]
-n # 在每行(包括空行)前面加上行號
-b # 在非空行前面加上行號

[[email protected] dir]# cat /etc/resolv.conf 
# Generated by NetworkManager
search magedu.com zhubiao.science

nameserver 192.168.17.1

nameserver 172.18.0.1
# -n 加入行號,包括空行
[[email protected] dir]# cat -n /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
     3  
     4  nameserver 192.168.17.1
     5  
     6  nameserver 172.18.0.1
# -b非空行前加入行號
[[email protected] dir]# cat -b /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science

     3  nameserver 192.168.17.1

     4  nameserver 172.18.0.1

tac

Concatenate and print files #倒敘顯示檔案內容,即從檔案尾頁到首頁的順序顯示內容
* tac

# 如正常順序顯示/etc/resolv.conf 如下:
[[email protected] dir]# cat /etc/resolv.conf 
# Generated by NetworkManager
search magedu.com zhubiao.science

nameserver 192.168.17.1

nameserver 172.18.0.1
#使用tac 將倒序輸出檔案內容,如下:
[[email protected] dir]# tac /etc/resolv.conf 
nameserver 172.18.0.1

nameserver 192.168.17.1

search magedu.com zhubiao.science
# Generated by NetworkManager

nl

讀取檔案內容後輸出左側加入行號的內容
* nl [options] [files]
* -b #與選項a, t一起使用,控制行號的輸出
a #-b a 所有行都有行號
t #-b t 空行不輸出行號
n #-b n 所有行均不輸出行號
* -n #與ln, rn, rz一起使用控制行號位置及補0的輸出
ln #-n ln 行號在所在欄位的左側
rn #-n rn 行號在所在欄位的右側
rz #-n rz 行號在所在欄位的右側,左側補0

[[email protected] dir]# nl /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science

     3  nameserver 192.168.17.1

     4  nameserver 172.18.0.1
[[email protected] dir]# nl -b a /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science
     3  
     4  nameserver 192.168.17.1
     5  
     6  nameserver 172.18.0.1
[[email protected] dir]# nl -b t /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science

     3  nameserver 192.168.17.1

     4  nameserver 172.18.0.1
[[email protected] dir]# nl -b n /etc/resolv.conf 
       # Generated by NetworkManager
       search magedu.com zhubiao.science

       nameserver 192.168.17.1

       nameserver 172.18.0.1
[[email protected] dir]# nl -n ln /etc/resolv.conf 
1       # Generated by NetworkManager
2       search magedu.com zhubiao.science

3       nameserver 192.168.17.1

4       nameserver 172.18.0.1
[[email protected] dir]# nl -n rn /etc/resolv.conf 
     1  # Generated by NetworkManager
     2  search magedu.com zhubiao.science

     3  nameserver 192.168.17.1

     4  nameserver 172.18.0.1
[[email protected] dir]# nl -n rz /etc/resolv.conf 
000001  # Generated by NetworkManager
000002  search magedu.com zhubiao.science

000003  nameserver 192.168.17.1

000004  nameserver 172.18.0.1
[[email protected] dir]# nl -n rz -w2 /etc/resolv.conf 
01  # Generated by NetworkManager
02  search magedu.com zhubiao.science

03  nameserver 192.168.17.1

04  nameserver 172.18.0.1

Out put the first part of files 讀取檔案的前幾行(預設為10行,也可指定)並輸出
* head [options] [files]
-n NUM #輸出前NUM行
-v 行首列印所讀取檔案內容的完整路徑名
-c NUM #輸出前NUM個字元

[centos7@root dir]# head /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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
# 行首輸出檔案路徑名
[centos7@root dir]# head -v /etc/passwd
==> /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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
# 指定輸出前3行
[centos7@root dir]# head -n 3 /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
# 輸出前4個字元
[centos7@root dir]# head -c 4 /etc/passwd
root[centos7@root dir]#

tail

Out put the last part of files
* tail [options] [fies]
-n NUM #輸出檔案後NUM行
-v #輸出首行顯示檔案完整路徑內容
-c NUM #輸出檔案最後NUM個字元

[centos7@root dir]# tail /etc/passwd
setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:990:985::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zb20:x:1000:1000:zb20:/home/zb20:/bin/bash
[centos7@root dir]# tail -n 3 /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zb20:x:1000:1000:zb20:/home/zb20:/bin/bash
[centos7@root dir]# tail -v -n 3 /etc/passwd
==> /etc/passwd <==
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zb20:x:1000:1000:zb20:/home/zb20:/bin/bash
# 輸出檔案末尾9個字元
[centos7@root dir]# tail -c 9 /etc/passwd
bin/bash

less

非常好用的一個瀏覽檔案內容的工具,可以通過快捷鍵向前/後瀏覽檔案內容,可以通過關鍵字搜尋檔案中的字串。對檔案的前後翻屏功能類似VIM, MAN手冊的瀏覽就是通過less命令實現的。
* less FILES
* 翻屏快捷鍵
Ctrl + F #向檔案首部翻一屏
Ctrl + B #向檔案尾部翻一屏
Ctrl + D #向檔案首部翻半屏
Ctrl + U #向檔案尾部翻半屏
j #向檔案尾部滾動一行
k #向檔案首部滾動一行
* 搜尋
* / [patern]
n #向前搜尋下一個匹配的模式或字串
N #向後搜尋上一個匹配的模式或字串
* ?[patern]
n #向後搜尋上一個匹配的模式或字串
N #向前搜尋下一個匹配的模式或字串

more

more功能和less類似,但沒有less功能多,比如不可以向檔案首部翻屏,只能向尾部翻屏,所以建議使用less

通常用來檢視二進位制檔案,但不限於只能看二進位制檔案。
* hexdump [option] [files]
-o #八進位制顯示
-d #十進位制顯示
-x #十六進位制顯示
-C #十六進位制和ASCII字元顯示

以下例項通過hexdump讀取cat命令的二進位制檔案,因內容過多,此處使用head -n 3 來顯示前三行內容

[centos7@root dir]# hexdump /bin/cat | head -n 3
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000010 0002 003e 0001 0000 2644 0040 0000 0000
0000020 0040 0000 0000 0000 cbc0 0000 0000 0000
[centos7@root dir]# hexdump -o /bin/cat | head -n 3
0000000  042577  043114  000402  000001  000000  000000  000000  000000
0000010  000002  000076  000001  000000  023104  000100  000000  000000
0000020  000100  000000  000000  000000  145700  000000  000000  000000
[centos7@root dir]# hexdump -d /bin/cat | head -n 3
0000000   17791   17996   00258   00001   00000   00000   00000   00000
0000010   00002   00062   00001   00000   09796   00064   00000   00000
0000020   00064   00000   00000   00000   52160   00000   00000   00000
[centos7@root dir]# hexdump -x /bin/cat | head -n 3
0000000    457f    464c    0102    0001    0000    0000    0000    0000
0000010    0002    003e    0001    0000    2644    0040    0000    0000
0000020    0040    0000    0000    0000    cbc0    0000    0000    0000
[centos7@root dir]# hexdump -C /bin/cat | head -n 3
00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 3e 00 01 00 00 00  44 26 40 00 00 00 00 00  |..>.....D&@.....|
00000020  40 00 00 00 00 00 00 00  c0 cb 00 00 00 00 00 00  |@...............|

od

讀取二進位制檔案
* od [option] [files]
-o #八進位制顯示
-d #十進位制顯示
-x #十六進位制顯示
以下例項通過od讀取cat命令的二進位制檔案,因內容過多,此處使用head -n 2 來顯示前三行內容

[centos6@root test]# od -o /bin/cat | head -n 2
0000000 042577 043114 000402 000001 000000 000000 000000 000000
0000020 000002 000076 000001 000000 014120 000100 000000 000000
[centos6@root test]# od -d /bin/cat | head -n 2
0000000 17791 17996   258     1     0     0     0     0
0000020     2    62     1     0  6224    64     0     0
[centos6@root test]# od -x /bin/cat | head -n 2
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000020 0002 003e 0001 0000 1850 0040 0000 0000

cp

  • cp [options] [Source_File] [Destination_File]
    -p #same as –preserve=mode,ownership,timestamps
    -u #當原檔案更新時才覆蓋目標檔案
    -R | r #若原檔案有子目錄時,連同目錄一起復制
    -d #當原檔案內容包含符號連結時,選項複製符號連結,不加-d選項是複製符號連結指向的原檔案內容
    -a same as -dr
    -i | –interactive #prompt before overwrite 若目標檔案存在普通使用者複製時,提示是否覆蓋,建議使用cp時加-i選項
    -f | –force
-p 複製時目標檔案保留原檔案的屬性(時間戳,屬主屬組,檔案許可權)
[centos6@zb10 ~]$ ll 1.txt
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 1.txt
[centos6@root zb10]# cp 1.txt 2.txt
[centos6@root zb10]# cp -p 1.txt 3.txt
[centos6@root zb10]# ll [[:digit:]].txt
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 1.txt #原檔案
-rw-r--r--. 1 root root 0 Jul 15 22:25 2.txt  #未加-p選項屬性將變化
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 3.txt  #加-p選項的目標檔案屬性不變
# -u 只有當原檔案比目標檔案修改時間更新才覆蓋目標檔案
# 如下file2 比file1更新,-u 選項時將file1複製到file2時不會覆蓋file2, 而反之則覆蓋file1的內容
[centos6@zb10 ~]$ ll file*
-rw-rw-r--. 1 zb10 zb10 15 Jul 15 22:06 file1
-rw-rw-r--. 1 zb10 zb10 15 Jul 15 22:07 file2 
[centos6@zb10 ~]$ head -v file*
==> file1 <==
file1 Contents

==> file2 <==
file2 Contents
[centos6@zb10 ~]$ cp -u file1 file2
[centos6@zb10 ~]$ head -v file*
==> file1 <==
file1 Contents

==> file2 <==
file2 Contents
[centos6@zb10 ~]$ cp -u file2 file1
[centos6@zb10 ~]$ head -v file*
==> file1 <==
file2 Contents

==> file2 <==
file2 Contents
# -d 當原檔案內容包含符號連結時,選項複製符號連結,不加-d選項是複製符號連結指向的原檔案內容
[centos6@root test]# tree
.
├── dir1
│   ├── a
│   │   └── file
│   └── file.ln -> a/file
└── dir2

3 directories, 2 files
[centos6@root test]# cp dir1/file.ln dir2/
[centos6@root test]# tree
.
├── dir1
│   ├── a
│   │   └── file
│   └── file.ln -> a/file
└── dir2
    └── file.ln

3 directories, 3 files
[centos6@root test]# cp -d dir1/file.ln dir2/
cp: overwrite `dir2/file.ln'? y
[[email protected] test]# tree
.
├── dir1
│   ├── a
│   │   └── file
│   └── file.ln -> a/file
└── dir2
    └── file.ln -> a/file

3 directories, 3 files

mv

Move or rename files 移動或重新命名檔案
* mv [options] [source] [destination]
-f | –force 目標檔案存在時,直接覆蓋
-i | –interactive 提示使用者是否覆蓋目標檔案
-u | –update 只有當原檔案更新時,才執行移動

# 如下所示當前目錄下有兩個目錄,dir1,dir2,將dir2 移動到dir1下
[centos6@root test]# tree
.
├── dir1
└── dir2
    └── a
        └── file1

3 directories, 1 file
[centos6@root test]# mv dir2 dir1
[centos6@root test]# tree
.
└── dir1
    └── dir2
        └── a
            └── file1

3 directories, 1 file
[centos6@root test]# 
# 修改dir1目錄名為dir3
[centos6@root test]# mv dir1 ./dir3
[centos6@root test]# ll
total 4
drwxr-xr-x. 3 root root 4096 Jul 16 09:41 dir3

rm

Remove files or directories 刪除檔案或目錄(子目錄)
* rm [options] [files]
-f | –force #刪除時,檔案或目錄不存在時不提示
-r | -R | –recursive #刪除目錄及其下的子目錄或檔案
-i #刪除每一個檔案、目錄時均提示是否刪除

[[email protected] test]# rm dir3/dir2/a/file1 
rm: remove regular empty file `dir3/dir