Linux基礎(chǔ)命令總結(jié)(CentOS6.9, CentOS7.3)

Linux基礎(chǔ)命令學(xué)習(xí)總結(jié),隨著學(xué)習(xí)的前進(jìn)吹泡,將不斷完善。網(wǎng)友們经瓷,有錯誤之處爆哑,請給我留言改正喔,謝謝舆吮!

基礎(chǔ)操作

<a href="#clear">clear</a> <a href="#hostname">hostname</a> <a href="#tty">tty</a> <a href="#who">who</a> <a href="#whoami">whoami</a> <a href="#w">w</a> <a href="#whatis">whatis</a> <a href="#which">which</a> <a href="#whereis">whereis</a> <a href="#hash">hash</a> <a href="#enable">enable</a> <a href="#alias">alias</a> <a href="#unalias">unalias</a> <a href="#date">date</a> <a href="#clock">clock</a> <a href="#hwclock">hwclock</a> <a href="#cal">cal</a> <a href="#ldd">ldd</a> <a href="#uname">uname</a> <a href="#timedatectl">timedatectl</a> <a href="#screen">screen</a> <a href="#script">script</a> <a href="#chvt">chvt</a> <a href="#man">man</a>


文件揭朝、目錄、磁盤色冀、文件系統(tǒng)

<a href="#cd">cd</a> <a href="#pwd">pwd</a> <a href="#ls">ls</a> <a href="#cat">cat</a> <a href="#tac">tac</a> <a href="#nl">nl</a> <a href="#head">head</a> <a href="#tail">tail</a> <a href="#less">less</a> <a href="#more">more</a> <a href="#hexdump">hexdump</a> <a href="# od"> od</a> <a href="#cp">cp</a> <a href="#mv">mv</a> <a href="#rm">rm</a> <a href="#mkdir">mkdir</a> <a href="#rmdir">rmdir</a> <a href="#dd">dd</a> <a href="#gedit">gedit</a> <a href="#nano">nano</a> <a href="#file">file</a> <a href="#type">type</a> <a href="#iconv">iconv</a>


用戶和組

<a href="#id">id</a> <a href="#getent">getent</a>


BASHELL 特性

<a href="#echo">echo</a> <a href="#history">history</a> <a href="#hash">hash</a> <a href="#history">history</a>


網(wǎng)絡(luò)屬性

<a href="#ifconfig">ifconfig</a> <a href="#ping">ping</a> <a href="#rz">rz</a> <a href="#sz">sz</a>


系統(tǒng)啟動萝勤,關(guān)機(jī)

<a href="#exit">exit</a> <a href="#logout">logout</a> <a href="#init">init</a> <a href="#quit">quit</a> <a href="#reboot">reboot</a> <a href="#shutdown">shutdown</a> <a href="#runlevel">runlevel</a> <a href="#halt">halt</a> <a href="#poweroff">poweroff</a>


進(jìn)程

<a href="#free">free</a> <a href="#lsblk">lsblk</a> <a href="# lscpu"> lscpu</a>


程序包管理

<a href="#rpm">rpm</a> <a href="#.">.</a> <a href="#source">source</a>



<a id="clear">clear</a>

clear the terminal screen 清空屏幕內(nèi)容

<a id="hostname">hostname</a>

Display the hostname 顯示主機(jī)名

  • hostname [-s|--short]
    -s | --short Display the shourt host name, This is the hostname cut at the firs dot. 顯示短主機(jī)名,取第一個點(diǎn)號之前的字符串
[centos6@root ~]# hostname
centos6.zhubiao.science
# -s 取第一個點(diǎn)號之前的字符串作為短主機(jī)名
[centos6@root ~]# hostname -s 
centos6

<a id="tty">tty</a>

print the file name of the terminal connected to standard input 顯示當(dāng)前工作終端下的終端的名字

  • 終端分為:
    • 物理終端
      物理控制臺:表示為 /dev/console呐伞, Linux單用戶模式下顯示為物理終端
    • 虛擬終端
      系統(tǒng)自帶6個虛擬終端,表示為/dev/tty#, #為1-6
    • 圖形終端
    • 偽終端
      圖形界面打開的命令行接口慎式,通過ssh或telnet協(xié)議打開的命令行界面均為偽終端伶氢,表現(xiàn)為/dev/pts/#
# 將運(yùn)行級別切換為單用戶模式后,使用的將是物理終端瘪吏,如下所示癣防,當(dāng)前運(yùn)行級別為3的多用戶模式,將其切換為1后查看終端掌眠。
[root@zb01 ~]# tty
/dev/tty1
[root@zb01 ~]# runlevel #查看運(yùn)行級別
S 3
[root@zb01 ~]# init 1
...
[root@zb01 /]# runlevel
1 S
[root@zb01 /]# tty
/dev/console

<a id="who">who</a>

Who is logged on 顯示當(dāng)前登錄的用戶

  • who [options]
    -b #系統(tǒng)登錄的時間
    -r #當(dāng)前運(yùn)行級別
[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

<a id="whoami">whoami</a>

顯示當(dāng)前登錄的用戶名

  • who
[centos7@root dir]# whoami
root

<a id="w">w</a>

Show who is logged on and what they are doing. 顯示當(dāng)前登錄的用戶和正在執(zhí)行的命令

  • w
[centos7@root 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

<a id="whatis">whatis</a>

查找外部命令的man手冊幫助文檔所在的章節(jié)

  • whatis command #等同于man -f
[centos7@root ~]# whatis ls
ls (1)               - list directory contents
ls (1p)              - list directory contents

[centos7@root ~]# whatis cp
cp (1)               - copy files and directories
cp (1p)              - copy files

[centos7@root ~]# whatis ifconfig
ifconfig (8)         - configure a network interface

[centos7@root ~]# man -f ls
ls (1)               - list directory contents
ls (1p)              - list directory contents

[centos7@root ~]# man -f cp
cp (1)               - copy files and directories
cp (1p)              - copy files

<a id="which">which</a>

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

<a id="whereis">whereis</a>

查找命令所在路徑蕾盯,源碼路徑,幫助手冊路徑
whereis [options] [command]

[centos6@root test]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

[centos6@root test]# whereis -b mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql

[centos6@root test]# whereis -m mysql
mysql: /usr/share/man/man1/mysql.1.gz

<a id="cd">cd</a>

Change the shell working dirctory 切換工作目錄

  • cd [-P] DIR
    -P 若DIR為符號鏈接目錄蓝丙,cd DIR切換到實(shí)際的工作目錄级遭,而不是符號鏈接目錄
    cd - #切換到上一個工作目錄
    cd ~ #切換到當(dāng)前用戶的家目錄
    cd ~USER #切換到USER用戶的家目錄,只有root有權(quán)限切換到任意用戶的家目錄
    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 切換到實(shí)際路徑的工作目錄下
[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

<a id="pwd">pwd</a>

print the name of the current working dirctory 顯示當(dāng)前工作目錄

  • pwd [-P]
    -P - 若當(dāng)前工作目錄為符號鏈接路徑渺尘,加-P選項(xiàng)顯示實(shí)際路徑
[centos7@root test]# pwd
/root/test  #顯示當(dāng)前工作目錄
# -P 顯示出實(shí)際路徑而不是符號鏈接的路徑
[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選項(xiàng)挫鸽,顯示實(shí)際路徑
/root/test/a/b/c

<a id="ls">ls</a>

List dirctory contents 列出當(dāng)前或指定目錄(或所有子目錄)下的內(nèi)容

  • ls [options] [dirs]
    - l 列出詳細(xì)內(nèi)容
    -a | --all 列出所有內(nèi)容包括. ..
    -A 列出所有內(nèi)容,不包括. ..
    -d 僅列出指定的目錄
    --full-time 列出完整的修改時間格式
    [centos6@root test]# ls
    

a dir
[centos6@root 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/
[centos6@root 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/
[centos6@root test]# ls -a
. .. a dir
[centos6@root test]# ls -A
a dir
[centos6@root test]# ls /root/test/
a dir
[centos6@root 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/


>### <a id="cat">cat</a>

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

[centos7@root dir]# cat /etc/resolv.conf

Generated by NetworkManager

search magedu.com zhubiao.science

nameserver 192.168.17.1

nameserver 172.18.0.1

-n 加入行號鸥跟,包括空行

[centos7@root 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非空行前加入行號

[centos7@root 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

>###<a id="tac">tac</a>

Concatenate and print files #倒敘顯示文件內(nèi)容丢郊,即從文件尾頁到首頁的順序顯示內(nèi)容
* tac

如正常順序顯示/etc/resolv.conf 如下:

[centos7@root dir]# cat /etc/resolv.conf

Generated by NetworkManager

search magedu.com zhubiao.science

nameserver 192.168.17.1

nameserver 172.18.0.1

使用tac 將倒序輸出文件內(nèi)容,如下:

[centos7@root dir]# tac /etc/resolv.conf
nameserver 172.18.0.1

nameserver 192.168.17.1

search magedu.com zhubiao.science

Generated by NetworkManager


>###<a id="nl">nl</a>

讀取文件內(nèi)容后輸出左側(cè)加入行號的內(nèi)容
* nl [options] [files]
  * -b   #與選項(xiàng)a, t一起使用医咨,控制行號的輸出
    a    #-b a  所有行都有行號
    t  #-b t 空行不輸出行號
    n #-b n 所有行均不輸出行號
  * -n  #與ln, rn, rz一起使用控制行號位置及補(bǔ)0的輸出
    ln  #-n ln 行號在所在字段的左側(cè)
    rn  #-n rn 行號在所在字段的右側(cè)
    rz  #-n rz 行號在所在字段的右側(cè)枫匾,左側(cè)補(bǔ)0

[centos7@root 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

[centos7@root 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
[centos7@root 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

[centos7@root 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

[centos7@root 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
[centos7@root 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

[centos7@root 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
[centos7@root 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


>###<a id="head">head</a>

Out put the first part of files 讀取文件的前幾行(默認(rèn)為10行,也可指定)并輸出
* head [options] [files]
  -n NUM  #輸出前NUM行
  -v 行首打印所讀取文件內(nèi)容的完整路徑名
  -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]#


>###<a id="tail">tail</a>

Out put the last part of files
* tail [options] [fies]
-n NUM #輸出文件后NUM行
-v #輸出首行顯示文件完整路徑內(nèi)容
-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


>###<a id="less">less<a>

非常好用的一個瀏覽文件內(nèi)容的工具拟淮,可以通過快捷鍵向前/后瀏覽文件內(nèi)容干茉,可以通過關(guān)鍵字搜索文件中的字符串。對文件的前后翻屏功能類似VIM, MAN手冊的瀏覽就是通過less命令實(shí)現(xiàn)的惩歉。
 * less FILES
   * 翻屏快捷鍵
    Ctrl + F #向文件首部翻一屏
    Ctrl + B #向文件尾部翻一屏
    Ctrl + D #向文件首部翻半屏
    Ctrl + U #向文件尾部翻半屏
    j  #向文件尾部滾動一行
    k #向文件首部滾動一行
  * 搜索
    * / [patern]
      n #向前搜索下一個匹配的模式或字符串
      N #向后搜索上一個匹配的模式或字符串
    * 等脂?[patern]
      n #向后搜索上一個匹配的模式或字符串
      N #向前搜索下一個匹配的模式或字符串

>###<a id="more">more</a>

more功能和less類似俏蛮,但沒有l(wèi)ess功能多,比如不可以向文件首部翻屏上遥,只能向尾部翻屏搏屑,所以建議使用less

>### <a id="hexdump">hexdump</a>

通常用來查看二進(jìn)制文件,但不限于只能看二進(jìn)制文件粉楚。
* hexdump [option] [files]
-o #八進(jìn)制顯示
-d #十進(jìn)制顯示
-x #十六進(jìn)制顯示
-C #十六進(jìn)制和ASCII字符顯示

 以下實(shí)例通過hexdump讀取cat命令的二進(jìn)制文件辣恋,因內(nèi)容過多,此處使用<a href="#head">head</a> -n 3 來顯示前三行內(nèi)容

[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 |@...............|


>### <a id="od">od</a>

讀取二進(jìn)制文件
* od [option] [files]
-o #八進(jìn)制顯示
-d #十進(jìn)制顯示
-x #十六進(jìn)制顯示
以下實(shí)例通過od讀取cat命令的二進(jìn)制文件模软,因內(nèi)容過多伟骨,此處使用<a href="#head">head</a> -n 2 來顯示前三行內(nèi)容

[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


>### <a id="cp">cp</a>

* cp [options] [Source_File] [Destination_File]
  -p #same as --preserve=mode,ownership,timestamps
  -u #當(dāng)原文件更新時才覆蓋目標(biāo)文件
  -R | r #若原文件有子目錄時,連同目錄一起復(fù)制
  -d #當(dāng)原文件內(nèi)容包含符號鏈接時燃异,選項(xiàng)復(fù)制符號鏈接携狭,不加-d選項(xiàng)是復(fù)制符號鏈接指向的原文件內(nèi)容
  -a same as -dr
  -i | --interactive #prompt before overwrite 若目標(biāo)文件存在普通用戶復(fù)制時,提示是否覆蓋回俐,建議使用cp時加-i選項(xiàng)
-f | --force

-p 復(fù)制時目標(biāo)文件保留原文件的屬性(時間戳逛腿,屬主屬組,文件權(quán)限)
[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選項(xiàng)屬性將變化
-rw-rw-r--. 1 zb10 zb10 0 Jul 15 22:25 3.txt #加-p選項(xiàng)的目標(biāo)文件屬性不變

-u 只有當(dāng)原文件比目標(biāo)文件修改時間更新才覆蓋目標(biāo)文件

如下file2 比file1更新仅颇,-u 選項(xiàng)時將file1復(fù)制到file2時不會覆蓋file2, 而反之則覆蓋file1的內(nèi)容

[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āng)原文件內(nèi)容包含符號鏈接時单默,選項(xiàng)復(fù)制符號鏈接,不加-d選項(xiàng)是復(fù)制符號鏈接指向的原文件內(nèi)容

[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
[centos6@root test]# tree
.
├── dir1
│ ├── a
│ │ └── file
│ └── file.ln -> a/file
└── dir2
└── file.ln -> a/file

3 directories, 3 files


>###<a id="mv">mv</a>

Move or rename files 移動或重命名文件
* mv [options] [source] [destination] 
  -f | --force  目標(biāo)文件存在時忘瓦,直接覆蓋
  -i | --interactive 提示用戶是否覆蓋目標(biāo)文件
  -u | --update 只有當(dāng)原文件更新時搁廓,才執(zhí)行移動

如下所示當(dāng)前目錄下有兩個目錄,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


>###<a id="rm">rm</a>

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

[centos6@root test]# rm dir3/dir2/a/file1
rm: remove regular empty file `dir3/dir2/a/file1'? y
[centos6@root test]# tree
.
└── dir3
└── dir2
└── a

3 directories, 0 files
[centos6@root test]# rm -rf dir3 #徹底刪除dir3及其目錄下的所有子目錄、文件


>###<a id="mkdir">mkdir</a>

Make Directories 創(chuàng)建目錄

* mkdir [options] [directories]
  -p | --parents 創(chuàng)建多級目錄時明场,其父目錄不存在則先創(chuàng)建其父目錄
  -m | --mode=MODE 創(chuàng)建目錄時指定目錄的權(quán)限
  -v 顯示創(chuàng)建目錄的過程

創(chuàng)建目錄dir1

[centos6@root test]# mkdir -v dir1
mkdir: created directory `dir1'
[centos6@root test]# ll -d dir1
drwxr-xr-x. 2 root root 4096 Jul 16 10:06 dir1

-m 創(chuàng)建目錄dir2,并指定目錄權(quán)限為rw-rw-rw-(666)

[centos6@root test]# mkdir -m 666 dir2
[centos6@root test]# ll -d dir1 dir2
drwxr-xr-x. 2 root root 4096 Jul 16 10:06 dir1
drw-rw-rw-. 2 root root 4096 Jul 16 10:07 dir2

-p 創(chuàng)建多級目錄

[centos6@root test]# mkdir -pv dir3/a/b/c
mkdir: created directory dir3' mkdir: created directorydir3/a'
mkdir: created directory dir3/a/b' mkdir: created directorydir3/a/b/c'
[centos6@root test]# ll -d dir3/a/b/c
drwxr-xr-x. 2 root root 4096 Jul 16 10:08 dir3/a/b/c


> ###<a id="rmdir">rmdir</a>

remove empty directories 刪除空目錄
* rmdir
  -p | --parents #Remove empty directories and its ancestors 刪除其空目錄及其父輩空目錄

如下所示汽摹,除了file是文件,其它均為空文件夾苦锨,下面練習(xí)是使用rmdir

[centos6@root test]# tree
.
├── dir1
├── dir2
│ └── file
└── dir3
└── a
└── b

5 directories, 1 file

刪除空目錄dir1

[centos6@root test]# rmdir dir1
[centos6@root test]# tree
.
├── dir2
│ └── file
└── dir3
└── a
└── b
4 directories, 1 file

若不是空目錄逼泣,比如文件file無法使用rmdir刪除

[centos6@root test]# rmdir dir2/file
rmdir: failed to remove `dir2/file': Not a directory

-p 連同其父空目錄一起刪除

[centos6@root test]# rmdir dir3/a/b/ #不適用-p僅刪除其自身空目錄
[centos6@root test]# tree
.
├── dir2
│ └── file
└── dir3
└── a

3 directories, 1 file
[centos6@root test]# rmdir -p dir3/a/ #使用-p連同其父空目錄一起刪除
[centos6@root test]# tree
.
└── dir2
└── file

1 directory, 1 file


>### <a id="dd">id</a>

Copy and Convert files 復(fù)制或轉(zhuǎn)換文件格式
dd 功能很強(qiáng)大,可以直接讀取磁盤扇區(qū)的內(nèi)容舟舒,在此列舉一個dd的復(fù)制工具
* dd if=*input_file* of=*output_file* bs=*block_size* count=*number*
  if #Read from file instead of stdin 從文件讀入(包括裝置拉庶,Linux中一切皆文件喔),若無if將從標(biāo)準(zhǔn)輸入(從鍵盤輸入)
  of #Write to file instead of stdout 寫入到文件秃励,無此項(xiàng)將輸出到屏幕
  bs #讀取和寫入的block大小氏仗,默認(rèn)為512bytes
    * ibs #讀取的block大小
    * obs #寫入的block大小

  count #bs的數(shù)量

將/bin/cat文件備份到當(dāng)前目錄下

[centos7@root ~]# dd if=/bin/cat of=./cat.bk bs=512
105+1 records in
105+1 records out
54080 bytes (54 kB) copied, 0.000900543 s, 60.1 MB/s
[centos7@root ~]# ll -h cat.bk
-rw-r--r--. 1 root root 53K Jul 16 11:06 cat.bk


>### <a id="gedit">gedit</a>

gdit是圖形終端的一個文本編輯工具,通過下圖方式打開
![image.png](http://upload-images.jianshu.io/upload_images/6823414-459f37ae79f49fc5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/500)
gedit也可以通過圖形終端上的命令行接口打開`[centos6@root Desktop]# gedit
`
>### <a id="nano">nano</a>

nano #命令行下的文本編輯工具,命令行下面有更強(qiáng)大的文本編輯工具vim皆尔,建議使用vim

>### <a id="file">file</a>

file Determine file types |判斷文件類型

查看cat命令文件類型

[centos7@root ~]# file /bin/cat
/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=fac04659ab9a437b5384c09f4731023373821a39, stripped

查看/etc/passwd文件類型

[centos7@root ~]# file /etc/passwd
/etc/passwd: ASCII text

查看/etc/類型

[centos7@root ~]# file /etc
/etc: directory


>###<a id="type">type</a>

type 判斷命令類型
* type [*options*] *command*
  -a 顯示所有包含該命令的位置
* 命令類型
  * builtin Shell內(nèi)建命令
  * 外部命令呐舔,不包含在shell內(nèi)建命令里的命令
  * alias | 別名
  * keword | 關(guān)鍵字
  * function | 函數(shù)

echo即有內(nèi)建命令,也有外部命令慷蠕,為了演示type珊拼,我們再給echo定義個別名

[centos6@root test]# alias echo
alias echo='echo -e'

不加任何選項(xiàng),使用type查詢流炕,若有別名澎现,只能查到別名

[centos6@root test]# type echo
echo is aliased to `echo -e'

-a 選項(xiàng)

[centos6@root test]# type -a echo
echo is aliased to `echo -e'
echo is a shell builtin
echo is /bin/echo

  
>### <a id="id">id</a>
Print user and group information for specified  the USERNAME or the currentuser
* id [options] [users]
  -g 用戶所在有效組
  -G 用戶所在的所有組

不指定user將顯示當(dāng)前用戶的信息

[centos7@root ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

指定用戶

[centos7@root ~]# id user1
uid=1001(user1) gid=1003(user1) groups=1003(user1),1001(group1),1002(group2)

[centos7@root ~]# id -g user1
1003

[centos7@root ~]# id -G user1
1003 1001 1002


> ### <a id="getent">getent</a>

Get entries from name server switch 從定義的數(shù)據(jù)庫里獲得記錄
數(shù)據(jù)庫定義在/etc/nsswitch.conf 文件中
* getent database [entries]

從/etc/passwd數(shù)據(jù)庫內(nèi)獲取包含root用戶的條目

[centos7@root ~]# getent passwd root
root:x:0:0:root:/root:/bin/bash

從/etc/group 數(shù)據(jù)庫內(nèi)獲取包含zb20的條目

[centos7@root ~]# getent group zb20
zb20:x:1000:zb20


>###<a id="iconv">iconv</a>

Convert text from one character encoding to another | 轉(zhuǎn)換字符編碼
* iconv -f  *from_encoding* -t *to_encoding* *input_file* -o *output_file*
  -f 原文件編碼
  -t 目標(biāo)文件編碼
  -o 輸出文件
  -l 列出iconv能轉(zhuǎn)換的編碼

windows系統(tǒng)上保存默認(rèn)文本編碼格式,在linux系統(tǒng)中無法打開每辟,就可以通過iconv轉(zhuǎn)換成linux系統(tǒng)識別的編碼

[centos7@root ~]# cat source.txt
βo3¤3?????°?S¢?°?C
[centos7@root ~]# iconv -f gb2312 -t UTF-8 source.txt -o destination.txt
[centos7@root ~]# cat destination.txt
萬里長城萬里空剑辫,百世英雄百世夢


>### <a id="echo">echo</a>

打印shell變量,指定的字符串
* echo [options] [string]
-n #輸出字符后末尾不換行渠欺,默認(rèn)換行
-e #輸出字符中的轉(zhuǎn)義字符生效
 * \t #TAB
 * \n #newline

顯示shell變量的值

[centos6@root test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[centos6@root test]# echo $PS1
[][\h@\u \W]$ []
[centos6@root test]# a=50
[centos6@root test]# echo $a
50

-n 末尾不換行

[centos6@root test]# echo "hello everyone"
hello everyone
[centos6@root test]# echo -n "hello everyone"
hello everyone[centos6@root test]#

-e 轉(zhuǎn)義字符生效

[centos6@root test]# echo "hello\t welcome to QuJing\nI believe..."
hello\t welcome to QuJing\nI believe...
[centos6@root test]# echo -e "hello\t welcome to QuJing\nI believe..."
hello welcome to QuJing
I believe...

輸出連續(xù)或不連續(xù)字符

[centos7@root ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[centos7@root ~]# echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
[centos7@root ~]# echo {1..9}
1 2 3 4 5 6 7 8 9
[centos7@root ~]# echo {9..1}
9 8 7 6 5 4 3 2 1
[centos7@root ~]# echo {1..10..2}
1 3 5 7 9
[centos7@root ~]# echo {001..10..2}
001 003 005 007 009
[centos7@root ~]# echo {1,7,q,b}
1 7 q b

 
>###<a id="history">history</a>

讀取歷史命令記錄
妹蔽。。挠将。更多功能待續(xù)

[centos7@root ~]# history | head -n 5
1 clear
2 cat /etc/gdm/custom.conf
3 vim /etc/gdm/custom.conf
4 reboot
5 shutdown -h now


>###<a id="ifconfig">ifconfig</a>

配置或顯示網(wǎng)卡參數(shù)

顯示eth1網(wǎng)卡參數(shù)

[centos6@root test]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:E3:FB:0C
inet addr:172.18.253.214 Bcast:172.18.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fee3:fb0c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:440093 errors:0 dropped:0 overruns:0 frame:0
TX packets:3225 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:43780248 (41.7 MiB) TX bytes:241256 (235.6 KiB)

關(guān)閉eth1網(wǎng)卡

[centos6@root test]# ifconfig eth1 down
[centos6@root test]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:E3:FB:0C
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:440107 errors:0 dropped:0 overruns:0 frame:0
TX packets:3227 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:43781936 (41.7 MiB) TX bytes:241410 (235.7 KiB)

啟動eth1網(wǎng)卡

[centos6@root test]# ifconfig eth1 up


>###<a id="ping">ping</a>

測試網(wǎng)絡(luò)是否連通
* ping [options] [ip/name]
  -c COUNT  發(fā)送ECHO_REQUEST 包的數(shù)量

[centos6@root test]# ping -c 3 192.168.17.20
PING 192.168.17.20 (192.168.17.20) 56(84) bytes of data.
64 bytes from 192.168.17.20: icmp_seq=1 ttl=64 time=3.66 ms
64 bytes from 192.168.17.20: icmp_seq=2 ttl=64 time=0.508 ms
64 bytes from 192.168.17.20: icmp_seq=3 ttl=64 time=0.848 ms

--- 192.168.17.20 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.508/1.673/3.664/1.414 ms


>###<a id="exit">exit</a> <a id="quit">quit</a> <a id="logout">logout</a>

quit, exit, logout 退出登錄

>### <a id="init">init</a>

切換運(yùn)行級別
* init NUM #NUM為:1,2,3,4,5,6
CentOS6 上在/etc/inittabe中定義了各運(yùn)行級別
\# Default runlevel. The runlevels used are:
\#   0 - halt (Do NOT set initdefault to this)  關(guān)機(jī)
\#   1 - Single user mode  單用戶模式
\#   2 - Multiuser, without NFS (The same as 3, if you do not have networking) 無網(wǎng)絡(luò)多用戶模式(命令行界面)
\#   3 - Full multiuser mode 多用戶模式(命令行界面)
\#   4 - unused  未使用讹开,保留
\#   5 - X11  圖形界面
\#   6 - reboot (Do NOT set initdefault to this) 重啟

>###<a id="runlevel">runlevel</a>

查看當(dāng)前系統(tǒng)運(yùn)行級別

可以通過runlevel 或 who -r 查看當(dāng)前系統(tǒng)運(yùn)行級別

[centos6@root test]# runlevel
[centos6@root test]# who -r
run-level 5 2017-07-15 06:26
N 5


>### <a id="shutdown">shutdown</a>

最常用的關(guān)機(jī)或重啟命令
* shutdown [options] [time] [string]
-r reboot after shutdown 重啟
-h 關(guān)機(jī)
-c 取消關(guān)機(jī)或重啟

立即關(guān)機(jī)

shutdown -h now

過5分鐘后關(guān)機(jī)

[centos6@root test]# shutdown -h 5 &
[1] 10237
[centos6@root test]#
Broadcast message from root@centos6.zhubiao.science
(/dev/pts/0) at 15:51 ...

The system is going down for halt in 5 minutes!

取消關(guān)機(jī)

[centos6@root test]# shutdown -c
shutdown: Shutdown cancelled
[1]+ Done shutdown -h 5

5分鐘后重啟

[centos6@root test]# shutdown -r 5

Broadcast message from root@centos6.zhubiao.science
(/dev/pts/0) at 15:53 ...

The system is going down for reboot in 5 minutes!


>###<a id="halt">halt</a> <a id="reboot">reboot</a> <a id="poweroff">pwoeroff</a> 

* reboot 重啟
* halt, poweroff 關(guān)機(jī)

>###<a id="free">free</a>

顯示當(dāng)前內(nèi)存使用情況
* free [options]
-h #human readable output 自動確定最佳單位顯示內(nèi)存使用量
-b, -k, -m, -g 以bytes, KB, MB, GB為單位顯示內(nèi)存使用量
-t 最后一行匯總RAM+SWAP內(nèi)存的使用總量
-l 

[centos7@root ~]# free -h
total used free shared buff/cache available
Mem: 976M 202M 252M 13M 522M 539M
Swap: 2.0G 0B 2.0G

[centos7@root ~]# free -th
total used free shared buff/cache available
Mem: 976M 202M 251M 13M 522M 539M
Swap: 2.0G 0B 2.0G
Total: 3.0G 202M 2.2G

[centos7@root ~]# free -lh
total used free shared buff/cache available
Mem: 976M 202M 251M 13M 522M 539M
Low: 976M 724M 251M
High: 0B 0B 0B
Swap: 2.0G 0B 2.0G

也可通過查看內(nèi)核進(jìn)程數(shù)據(jù)來查看內(nèi)存使用情況

[centos7@root ~]# cat /proc/meminfo
MemTotal: 999964 kB
MemFree: 257648 kB
MemAvailable: 551900 kB
Buffers: 1804 kB
Cached: 390520 kB
SwapCached: 0 kB
Active: 266780 kB
Inactive: 224516 kB
Active(anon): 94740 kB
Inactive(anon): 17896 kB
Active(file): 172040 kB
Inactive(file): 206620 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 99036 kB
Mapped: 35392 kB
Shmem: 13664 kB
Slab: 142844 kB
SReclaimable: 86632 kB
SUnreclaim: 56212 kB
KernelStack: 4832 kB
PageTables: 7260 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2597128 kB
Committed_AS: 634440 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 181824 kB
VmallocChunk: 34359310332 kB
HardwareCorrupted: 0 kB
AnonHugePages: 8192 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 98176 kB
DirectMap2M: 950272 kB


>###<a id="lsblk">lsblk</a>

列出塊狀設(shè)備大小及掛載點(diǎn)

[centos7@root ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
├─sda3 8:3 0 50G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64
[centos7@root ~]# lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64


>###<a id="lscpu">lscpu</a>

顯示CPU信息

[centos7@root ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 58
Model name: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
Stepping: 9
CPU MHz: 2493.686
BogoMIPS: 4988.79
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
NUMA node0 CPU(s): 0,1

通過內(nèi)核進(jìn)程文件/proc/cpuinfo查看cpu信息

[centos7@root ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 58
model name : Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
stepping : 9
microcode : 0x12
cpu MHz : 2493.686
cache size : 3072 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dtherm fsgsbase tsc_adjust smep
bogomips : 4988.79
clflush size : 64
cache_alignment : 64
address sizes : 42 bits physical, 48 bits virtual
power management:

processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 58
model name : Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
stepping : 9
microcode : 0x12
cpu MHz : 2493.686
cache size : 3072 KB
physical id : 2
siblings : 1
core id : 0
cpu cores : 1
apicid : 2
initial apicid : 2
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dtherm fsgsbase tsc_adjust smep
bogomips : 4988.79
clflush size : 64
cache_alignment : 64
address sizes : 42 bits physical, 48 bits virtual
power management:


>###<a id=".">.</a>  <a id="source">source</a>

執(zhí)行shell腳本

在 /etc/profile.d/env.sh中設(shè)置命令提示符,然后使用. DIR或soruce DIR執(zhí)行腳本捐名,后命令提示符將生效

[~] cat /etc/profile.d/env.sh
export PS1="[\h@\u \W]\$ "

[~] . /etc/profile.d/env.sh
[centos6@root ~]#


>###<a id="enable">enable</a>

Enable and disable shell builtins | 啟用或停用SHELL內(nèi)建命令

* enable [options] [command]
  -a 列出所有內(nèi)建命令,不管是否被禁用
  -n *command* 停用命令*command*

停用cd命令

[centos6@root ~]# enable -n cd
[centos6@root ~]# cd
-bash: cd: command not found

[centos6@root ~]# enable -a | grep cd
enable -n cd

啟用cd命令

[centos6@root ~]# enable cd
[centos6@root ~]# cd ..
[centos6@root /]# cd -
/root


>###<a id="hash">hash</a>

緩存已執(zhí)行的外部命令的路徑闹击,以便下次使用節(jié)省搜索命令的時間

* hash [options] [command]
  -d command 從hash緩存中刪除command命令記錄
  -r 清空hash 緩存
  -l 列出路徑及命令名
  -p name #在hash緩存中給路徑起別名

查看已緩存的外部命令

[centos6@root ~]# hash
hits command
1 /sbin/ifconfig
1 /usr/bin/man
1 /bin/ls
8 /usr/bin/clear

列出緩存的路徑及其命令名

[centos6@root ~]# hash -l
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /usr/bin/man man
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/clear clear

從hash緩存中刪除命令man

[centos6@root ~]# hash -d man
[centos6@root ~]# hash -l
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/clear clear

起別名

[centos6@root ~]# hash -p /sbin/ifconfig ifcon
[centos6@root ~]# hash -l
builtin hash -p /sbin/ifconfig ifconfig
builtin hash -p /sbin/ifconfig ifcon
builtin hash -p /bin/ls ls
builtin hash -p /usr/bin/clear clear
[centos6@root ~]# ifcon eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:E3:FB:02
inet addr:192.168.17.10 Bcast:192.168.17.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fee3:fb02/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21356 errors:0 dropped:0 overruns:0 frame:0
TX packets:18035 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1995596 (1.9 MiB) TX bytes:3239265 (3.0 MiB)

清空hash緩存

[centos6@root ~]# hash -r
[centos6@root ~]# hash
hash: hash table empty


>###<a id="alias">alias</a> <a id="unalis">unalis</a>

* alias #Define or display aliases 定義別名
  alias name="value"
* unalias 取消別名
  unalias name

查看以定義的別名

[centos6@root ~]# 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='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

定義別名

[centos6@root ~]# alias grep="grep --color"
[centos6@root ~]# alias
alias cp='cp -i'
alias grep='grep --color'
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'

在配置文件/etc/bashrc中定義別名,所有用戶均生效

[centos6@root ~]# vim /etc/bashrc
[centos6@root ~]# tail -n 3 /etc/bashrc

Define alias

alias grep="grep --color"
[centos6@root ~]# . /etc/bashrc

在自己家目錄配置文件~/.basrc中定義镶蹋,只有當(dāng)前用戶生效

[centos6@root ~]# vim ~/.bashrc
[centos6@root ~]# cat ~/.bashrc

.bashrc

User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'


>###<a id="date">date</a>

Print or set system date or time 顯示或設(shè)置系統(tǒng)時間

* date [option] [+format]
設(shè)定時間的兩種方法
 * date [MMDDhhmm[[CC]YY][.ss]]
 * date -s "*string*"

  -d "*string*"
    * string: 
      "1 year"
      "1 year ago"
      "3 years"
      "3 years ago"
      "1 day"
      "1 day ago"
      "3 days"
      "3 days ago"

  * format
    %Y year
    %m month
    %d day
    %H hour
    %M minute
    %S second
    %F fulldate same as %Y-%m-%d
    %T time,same as %H:%M:%S

以不同的格式顯示時間

[centos6@root ~]# date +%F
2017-07-17
[centos6@root ~]# date +%T
22:57:11
[centos6@root ~]# date "+%F %T"
2017-07-17 22:57:19
[centos6@root ~]# date +"%F %T"
2017-07-17 22:57:27
[centos6@root ~]# date "+%Y-%m-%d %H:%M:%S"
2017-07-17 22:57:55

設(shè)定時間 [MMDDhhmm[[CC]YY][.ss]]

[centos6@root ~]# date 062012121990.33
Wed Jun 20 12:12:33 CDT 1990
[centos6@root ~]# date +"%F %T"
1990-06-20 12:12:48

設(shè)定時間 date -s STRING

[centos6@root ~]# date -s "20170717 23:03:10"
Mon Jul 17 23:03:10 CST 2017
[centos6@root ~]# date +"%F %T"
2017-07-17 23:03:22

當(dāng)前時間

[centos6@root ~]# date +"%F %T"
2017-07-17 23:09:11

顯示4年前的日期

[centos6@root ~]# date -d "4 years ago" +"%F %T"
2013-07-17 23:09:32

顯示10天前的日期

[centos6@root ~]# date -d "10 days ago" +"%F %T"
2017-07-07 23:09:52

顯示10天后的日期

[centos6@root ~]# date -d "10 days" +"%F %T"
2017-07-27 23:10:01


>###<a id=clock>clock</a> <a id="hwclock">hwclock</a>

hwclock,clock功能相同,都是設(shè)定硬件時間
* hwclock [options] 
  * hwclock --set --date="*string*" #按字符串描述的時間設(shè)定硬件時間
  * hwclock --systohc #將系統(tǒng)時間更新到硬件時間
  * hwclock --hctosys #將硬件時間更新到系統(tǒng)時間

clock其實(shí)是hwclock的符號鏈接

[centos6@root ~]# ll /sbin/clock
lrwxrwxrwx. 1 root root 7 Jul 15 01:17 /sbin/clock -> hwclock

[centos7@root ~]# hwclock
Mon 17 Jul 2017 11:20:50 PM CST -0.663348 seconds

設(shè)定硬件時間

[centos7@root ~]# hwclock --set --date="1990-06-20 10:10:10"
[centos7@root ~]# hwclock ; date
Wed 20 Jun 1990 10:10:24 AM CDT -0.100626 seconds
Mon Jul 17 23:21:39 CST 2017

[centos7@root ~]# hwclock --systohc
[centos7@root ~]# hwclock ; date
Mon 17 Jul 2017 11:22:01 PM CST -0.944138 seconds
Mon Jul 17 23:22:00 CST 2017
[centos7@root ~]# date -s "19900620" +%F
1990-06-20

[centos7@root ~]# hwclock --hctosys
[centos7@root ~]# hwclock ; date
Mon 17 Jul 2017 11:22:58 PM CST -0.411873 seconds
Mon Jul 17 23:22:57 CST 2017


>###<a id="cal">cal</a>

Display a calendar | 顯示日歷 
* cal [options] [[[day], month], year]
  -1  顯示一個月
  -3 display prev/current/next month

[centos6@root ~]# cal
July 2017
Su Mo Tu We Th Fr Sa
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
[centos6@root ~]# cal 20 06 1990
June 1990
Su Mo Tu We Th Fr Sa
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


>###<a id="history">history</a>

Display or manipulate the history list | 顯示或操作命令歷史
  從登錄開始所操作的命令歷史若沒有人為追加至命令歷史文件中赏半,則存在于內(nèi)存中贺归,當(dāng)用戶退出登錄時自動追加至命令歷史文件中
命令歷史文件~/.bash_history, m
* 命令歷史大小,顯示受以下變量控制
  這些變量要永久生效需將其寫入配置文件中/etc/profile断箫,~/.bash_profile
  * HISTFILE   #定義了命令歷史文件的位置
  * HISTSIZE  #定義內(nèi)存中命令歷史記錄的最大數(shù)量
  * HISTFILESIZE  #定義命令歷史文件中存放記錄的最大數(shù)量
  * HISTCONTROL
    * ignoredups  #連續(xù)重復(fù)的命令只記錄一條
    * ignorespace  #命令執(zhí)行前有空白的拂酣,不記錄
    * ignoreboth  #上面兩條同時生效
  * HISTTIMEFORMAT ="%F %T"  定義命令歷史記錄中顯示的時間格式
  * HISTIGNORE="str*" #以str開頭的命令不記錄
  

* history [options] [name]
  -c 清空當(dāng)前內(nèi)存中的命令歷史
  -d *NAME* 從內(nèi)存中刪除某條命令歷史記錄
  -a 將內(nèi)存中未從文件中讀取,新產(chǎn)生的命令歷史記錄追加至文件中
  -w 將當(dāng)前內(nèi)存中的命令歷史覆蓋命令歷史文件
  -p 不將當(dāng)前執(zhí)行的擴(kuò)展命令記錄至命令歷史中

默認(rèn)HISTSIZE為1000,在/etc/profile中定義了其大小

[centos6@root ~]# echo $HISTSIZE
1000
[centos6@root ~]# cat /etc/profile | grep HISTSIZE
HISTSIZE=1000
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

[centos6@root ~]# echo $HISTFILESIZE
1000

[centos6@root ~]# history | tail -n 10
445 help history
446 clear
447 history
448 history -d clear
449 history -d 440
450 clear
451 pwd
452 ifconfig
453 hostname
454 history | tail -n 10

[centos6@root ~]# history -d 450
[centos6@root ~]# history | tail -n 10
446 clear
447 history
448 history -d clear
449 history -d 440
450 pwd
451 ifconfig
452 hostname
453 history | tail -n 10
454 history -d 450
455 history | tail -n 10

[centos6@root ~]# history -c
[centos6@root ~]# history
1 history

[centos6@root ~]# cat ~/.bash
.bash_history .bash_logout .bash_profile .bashrc
[centos6@root ~]# cat ~/.bash_history
history -r
仲义。婶熬。。
[centos6@root ~]# cat ~/.bash_history | wc -l
356

-a 追加

[centos6@root ~]# history -a
[centos6@root ~]# cat ~/.bash_history | wc -l
363
[centos6@root ~]# clear
[centos6@root ~]# history -c
[centos6@root ~]# history
1 history

-w 覆蓋

[centos6@root ~]# history -w
[centos6@root ~]# cat ~/.bash_history | wc -l
2


>###<a id="rz">rz</a> <a id="">sz</a> 

rz, sz 是在安裝了lrzsz程序包后產(chǎn)生的兩個工具埃撵,用于與windows系統(tǒng)互傳文件
* rz #從widows系統(tǒng)上選擇需上傳至linux的文件
* sz FILE #將FILE從Liux中下載至windows系統(tǒng)


>###<a id="ldd">ldd</a>

Print shared library dependencies | 顯示命令所依賴的共享庫
* ldd command

[centos6@root ~]# ldd /bin/ls
linux-vdso.so.1 => (0x00007fffccfa2000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00000030d2e00000)
librt.so.1 => /lib64/librt.so.1 (0x00000030d1e00000)
libcap.so.2 => /lib64/libcap.so.2 (0x00000030d8200000)
libacl.so.1 => /lib64/libacl.so.1 (0x00000030dc600000)
libc.so.6 => /lib64/libc.so.6 (0x00000030d1600000)
libdl.so.2 => /lib64/libdl.so.2 (0x00000030d1200000)
/lib64/ld-linux-x86-64.so.2 (0x00000030d0e00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00000030d1a00000)
libattr.so.1 => /lib64/libattr.so.1 (0x00000030dbe00000)

[centos6@root ~]# ldd /bin/cat
linux-vdso.so.1 => (0x00007ffe611f0000)
libc.so.6 => /lib64/libc.so.6 (0x00000030d1600000)
/lib64/ld-linux-x86-64.so.2 (0x00000030d0e00000)



>###<a id="uname">uname</a>

Print system information | 顯示系統(tǒng)版本信息
* uname [option]
  -a 顯示所有
  -r 內(nèi)核版本
* 查看發(fā)現(xiàn)版本號可通過查看以下兩個文件
  /etc/redhat-release
  /etc/centos-release

[centos6@root ~]# uname
Linux

[centos6@root ~]# uname -a
Linux centos6.zhubiao.science 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

[centos6@root ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)

[centos6@root ~]# cat /etc/centos-release
CentOS release 6.9 (Final)


>###<a id="timedatectl">timedatectl</a>

Control the system time and date 顯示赵颅、設(shè)定時間(centos7)
* timedatectl [options]
  set-time *string* 設(shè)定時間
  set-ntp *bool* 是否啟用ntp同步時間
  list-timezones #列出所有時區(qū)
  set-timezone #設(shè)定時區(qū)

對于centos7系統(tǒng),修改時區(qū)其實(shí)是修改了/etc/localtime的符號鏈接指向暂刘,centos6也可以通過直接修改鏈接指向達(dá)到修改時區(qū)的目的

以下為CentOS7上的操作
設(shè)定時間
[centos7@root ~]# timedatectl set-time "2017-07-18 15:35:00"

查看當(dāng)前時區(qū)設(shè)置為上海

[centos7@root ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Jul 17 20:01 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

[centos7@root ~]# timedatectl list-timezones | grep New_York
America/New_York

設(shè)定時區(qū)為New_York

[centos7@root ~]# timedatectl set-timezone America/New_York
[centos7@root ~]# date
Mon Jul 17 21:12:38 EDT 2017
[centos7@root ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 38 Jul 17 21:12 /etc/localtime -> ../usr/share/zoneinfo/America/New_York

對于CentOS6系統(tǒng)直接建立符號鏈接指向相應(yīng)的時區(qū)即可饺谬,操作前備份原文件

[centos6@root ~]# ll /usr/share/zoneinfo/America/New_York
-rw-r--r--. 3 root root 3519 Dec 1 2016 /usr/share/zoneinfo/America/New_York
[centos6@root ~]# ln -s /usr/share/zoneinfo/America/New_York /etc/localtime


>###<a id="screen">screen</a>

要求同一個用戶,同一臺主機(jī)
screen非常有用的一個工具谣拣,可以實(shí)現(xiàn)多人在同一個會話中操作Linux募寨,實(shí)現(xiàn)字符界面的共享族展,當(dāng)你由于各種原因?qū)е峦顺鰰挘瑫捴兴\(yùn)行的程序并不會結(jié)束拔鹰,仍然繼續(xù)運(yùn)行
* screen [options]
  -S *name* #創(chuàng)建名為name的會話
  -x *name* #加入name會話
  ctrl+a, d #脫離會話
  -r #恢復(fù)會話
  exit 結(jié)束會話
  -ls 列出會話

創(chuàng)建會話

[centos6@root ~]# screen -S zb10

列出現(xiàn)發(fā)起的會話

[centos6@root ~]# screen -ls
There is a screen on:
50753.zb10 (Attached)
1 Socket in /var/run/screen/S-root.

加入會話

screen -x zb10

脫離會話

同時按下ctrl+a, 再按d

再加入剛脫離的會話

[centos6@root ~]# screen -r

退出會話

[centos6@root ~]# exit

![2017-07-18_095843.png](http://upload-images.jianshu.io/upload_images/6823414-ba7c81f60e9ad044.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/800)


>###<a id="chvt">chvt</a>

虛擬終端之間互相切換仪缸,或圖形終端上開啟的偽終端與虛擬終端之間的切換
* chvt NUM #NUM終端號1-6

[centos6@root ~]# chvt 1


>###<a id="script">script</a> <a id="scriptreplay">scriptreplay</a>

* script 
  make typescript of terminal session | 錄屏(字符界面)
  -o script_file 將錄制的內(nèi)容存放于文件中
  -t 時間通過標(biāo)準(zhǔn)錯誤輸出到屏幕,可通過重定向到到文件中2>time_file
  exit 退出錄屏
* scriptreplay  回放
  scriptreplay time_file script_file
  playback typescripts, using time information

開始錄屏

[centos6@root ~]# script -a script_file -t 2>time_file
Script started, file is script_file
[centos6@root ~]# pwd
/root
[centos6@root ~]# ls
1.log a Desktop install.log.syslog Pictures test
1.txt anaconda-ks.cfg Documents localtime Public time_file
2.txt a.tar Downloads man.1 script_file typescript
3.txt core.3283 install.log Music Templates Videos
[centos6@root ~]# exit #退出錄屏
exit
Script done, file is script_file

回放格郁,下面除了第一條回放操作命令以外腹殿,其它的操作均是回放

[centos6@root ~]# scriptreplay time_file script_file
[centos6@root ~]# pwd
/root
[centos6@root ~]# ls
1.log a Desktop install.log.syslog Pictures test
1.txt anaconda-ks.cfg Documents localtime Public time_file
2.txt a.tar Downloads man.1 script_file typescript
3.txt core.3283 install.log Music Templates Videos
[centos6@root ~]# exit[centos6@root ~]#


>###<a id="man">man</a>
查看外部命令的幫助文檔

* man *command*
* man 手冊配置文件位置
  centos6 /etc/man.config
  centos7 /etc/man_db.conf


>###<a id=""></a>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市例书,隨后出現(xiàn)的幾起案子锣尉,更是在濱河造成了極大的恐慌,老刑警劉巖决采,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件自沧,死亡現(xiàn)場離奇詭異,居然都是意外死亡树瞭,警方通過查閱死者的電腦和手機(jī)拇厢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晒喷,“玉大人孝偎,你說我怎么就攤上這事×骨茫” “怎么了衣盾?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長爷抓。 經(jīng)常有香客問我势决,道長,這世上最難降的妖魔是什么蓝撇? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任果复,我火速辦了婚禮,結(jié)果婚禮上渤昌,老公的妹妹穿的比我還像新娘虽抄。我一直安慰自己,他們只是感情好独柑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布极颓。 她就那樣靜靜地躺著,像睡著了一般群嗤。 火紅的嫁衣襯著肌膚如雪菠隆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機(jī)與錄音骇径,去河邊找鬼躯肌。 笑死,一個胖子當(dāng)著我的面吹牛破衔,可吹牛的內(nèi)容都是我干的清女。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晰筛,長吁一口氣:“原來是場噩夢啊……” “哼嫡丙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起读第,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤曙博,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后怜瞒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體父泳,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年吴汪,在試婚紗的時候發(fā)現(xiàn)自己被綠了惠窄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡漾橙,死狀恐怖杆融,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情霜运,我是刑警寧澤擒贸,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站觉渴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏徽惋。R本人自食惡果不足惜案淋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望险绘。 院中可真熱鬧踢京,春花似錦、人聲如沸宦棺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽代咸。三九已至蹈丸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逻杖。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工奋岁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人荸百。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓闻伶,卻偏偏與公主長得像,于是被迫代替她去往敵國和親够话。 傳聞我的和親對象是個殘疾皇子蓝翰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容