1. 程式人生 > >【Linux就該這麽學 20期培訓筆記 02】新手必須掌握的 Linux 命令

【Linux就該這麽學 20期培訓筆記 02】新手必須掌握的 Linux 命令

clearing pcs 批處理腳本 eas 睡眠 大小 zone 其命令 5.0

本章知識點
  1. 強大好用的SHELL
  2. 執行查看幫助命令
  3. 常用系統工作命令
  4. 系統狀態檢測命令
  5. 工作目錄切換命令
  6. 文本文件編輯命令
  7. 文件目錄管理命令
  8. 打包壓縮與搜索命令

Linux命令大全(手冊)

強大好用的SHELL

  • Shell (也稱為終端)充當的是人與內核(硬件)之間的翻譯官

技術分享圖片

  • 主流 Linux 系統默認使用的 ShellBash
    1:通過上下方向鍵來調取過往執行過的Linux命令;
    2:命令或參數僅需輸入前幾位就可以用Tab鍵補全;
    3:具有強大的批處理腳本;
    4:具有實用的環境變量功能。

執行查看幫助命令

  • 常見執行Linux命令的格式:

    命令名稱 [命令參數] [命令對象]

    註意,命令名稱、命令參數、命令對象之間請用空格鍵分隔。

  • 命令參數的長格式與短格式

技術分享圖片

短格式+短格式:可合並
長格式+短橋式、長格式+長格式:不可以合並

  • man 命令可查看關於命令的幫助信息
man <需要幫助的命令>

技術分享圖片

常用系統工作命令

1. echo命令

echo命令用於在終端輸出字符串或變量提取後的值,格式為“echo [字符串 | $變量]”

[[email protected] ~]# echo $SHELL
/bin/bash

2.date命令

date命令用於顯示及設置系統的時間或日期,格式為“date [選項] [+指定的格式]”

  • 按照“年-月-日 小時:分鐘:秒”的格式查看當前系統時間

    [[email protected] ~]# date "+%Y-%m-%d %H:%M:%S"
    2017-08-24 16:29:12
  • 將系統的當前時間設置為2017年9月1日8點30分

    [[email protected] ~]# date -s "20170901 8:30:00"
    Fri Sep 1 08:30:00 CST 2017
  • 使用date命令並按照默認的格式查看當前的系統時間

    [[email protected] ~]# date
    Fri Sep 1 08:30:01 CST 2017
  • date命令中的參數%j可用來查看今天是當年中的第幾天
    [[email protected] ~]# date "+%j"
    244

3.reboot命令

reboot命令用於重啟系統,其格式為reboot。

  • 由於重啟計算機這種操作會涉及硬件資源的管理權限,因此默認只能使用root管理員來重啟,其命令如下
    [[email protected] ~]# reboot

4.poweroff命令

poweroff命令用於關閉系統,其格式為poweroff。

  • 該命令與reboot命令相同,都會涉及硬件資源的管理權限,因此默認只有root管理員才可以關閉電腦,其命令如下:
    [[email protected] ~]# poweroff

5.wget命令

wget命令用於在終端中下載網絡文件,格式為“wget [參數] 下載地址”

技術分享圖片

  • 嘗試使用wget命令從本書的配套站點中下載本書的最新pdf格式電子文檔,這個文件的完整路徑為https://www.linuxprobe.com/docs/LinuxProbe.pdf, 執行該命令後的下載效果如下:
    [[email protected] ~]# wget https://www.linuxprobe.com/docs/LinuxProbe.pdf
    --2017-08-24 19:30:12 -- https://www.linuxprobe.com/docs/LinuxProbe.pdf
    Resolving www.linuxprobe.com (www.linuxprobe.com)... 220.181.105.185
    Connecting to www.linuxprobe.com (www.linuxprobe.com)|220.181.105.185|:80...connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 45948568 (44M) [application/pdf]
    Saving to: ‘LinuxProbe.pdf’
    100%[===========================================>] 45,948,568 32.9MB/s in 1.3s
    2017-08-24 19:30:14 (32.9 MB/s) - ‘LinuxProbe.pdf’ saved [45948568/45948568]

6.ps命令

ps命令用於查看系統中的進程狀態,格式為“ps [參數]”

技術分享圖片

進程狀態含義如下所示:
R(運行):進程正在運行或在運行隊列中等待。
S(中斷):進程處於休眠中,當某個條件形成後或者接收到信號時,則脫離該 狀態。
D(不可中斷):進程不響應系統異步信號,即便用kill命令也不能將其中斷。
Z(僵死):進程已經終止,但進程描述符依然存在, 直到父進程調用wait4()系統函數後將進程釋放。
T(停止):進程收到停止信號後停止運行。

技術分享圖片

7.top命令

top命令用於動態地監視進程活動與系統負載等信息,其格式為top

技術分享圖片

第1行:系統時間、運行時間、登錄終端數、系統負載(三個數值分別為1分鐘、5分鐘、15分鐘內的平均值,數值越小意味著負載越低)。
第2行:進程總數、運行中的進程數、睡眠中的進程數、停止的進程數、僵死的進程數。
第3行:用戶占用資源百分比、系統內核占用資源百分比、改變過優先級的進程資源百分比、空閑的資源百分比等。其中數據均為CPU數據並以百分比格式顯示,例如“97.1 id”意味著有97.1%的CPU處理器資源處於空閑。
第4行:物理內存總量、內存使用量、內存空閑量、作為內核緩存的內存量。
第5行:虛擬內存總量、虛擬內存使用量、虛擬內存空閑量、已被提前加載的內存量。

8.pidof命令

pidof命令用於查詢某個指定服務進程的PID值,格式為“pidof [參數] [服務名稱]”

  • 每個進程的進程號碼值(PID)是唯一的,因此可以通過PID來區分不同的進程。例如,可以使用如下命令來查詢本機上sshd服務程序的PID:
    [[email protected] ~]# pidof sshd
    2156

9.kill命令

kill命令用於終止某個指定PID的服務進程,格式為“kill [參數] [進程PID]”

  • 接下來,我們使用kill命令把上面用pidof命令查詢到的PID所代表的進程終止掉,其命令如下所示。這種操作的效果等同於強制停止sshd服務
    [[email protected] ~]# kill 2156

10.killall命令

killall命令用於終止某個指定名稱的服務所對應的全部進程,格式為:“killall [參數] [服務名稱]”

[[email protected] ~]# pidof httpd
13581 13580 13579 13578 13577 13576
[[email protected] ~]# killall httpd
[[email protected] ~]# pidof httpd
[[email protected] ~]# 

系統狀態檢測命令

1.ifconfig命令

ifconfig命令用於獲取網卡配置與網絡狀態等信息,格式為“ifconfig [網絡設備] [參數]”

[[email protected] ~]# ifconfig
eno16777728: flags=4163  mtu 1500
        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fec4:a409  prefixlen 64  scopeid 0x20
        ether 00:0c:29:c4:a4:09  txqueuelen 1000  (Ethernet)
        RX packets 36  bytes 3176 (3.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 38  bytes 4757 (4.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 386  bytes 32780 (32.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 386  bytes 32780 (32.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.uname命令

uname命令用於查看系統內核與系統版本等信息,格式為“uname [-a]”

[[email protected] ~]# uname -a
Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux

順帶一提,如果要查看當前系統版本的詳細信息,則需要查看redhat-release文件,其命令以及相應的結果如下:

[[email protected] ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)

3.uptime命令

uptime用於查看系統的負載信息,格式為uptime

[[email protected] ~]# uptime
22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18

4.free命令

free用於顯示當前系統中內存的使用量信息,格式為“free [-h]”

[[email protected] ~]# free -h

技術分享圖片

5.who命令

who用於查看當前登入主機的用戶終端信息,格式為“who [參數]”

[[email protected] ~]# who

技術分享圖片

6.last命令

last命令用於查看所有系統的登錄記錄,格式為“last [參數]”

[[email protected] ~]# last
root       pts/0       :0               Mon Aug  24 17:52   still  logged in
root       :0          :0               Mon Aug  24 17:52   still  logged in
(unknown   :0          :0               Mon Aug  24 17:50 - 17:52   (00:02)
reboot     system boot 3.10.0-123.el7.x Tue Aug  25 01:49 - 18:17   (-7:-32)
root       pts/0       :0               Mon Aug  24 15:40 - 08:54   (7+17:14)
root       pts/0       :0               Fri Jul  10 10:49 - 15:37   (45+04:47)
………………省略部分登錄信息………………

7.history命令

history命令用於顯示歷史執行過的命令,格式為“history”

  • 執行history命令能顯示出當前用戶在本地計算機中執行過的最近 1000 條命令記錄。如果覺得1000不夠用,還可以自定義 /etc/profile 文件中的HISTSIZE變量值。
  • 在使用history命令時,如果使用 -c 參數則會清空所有的命令歷史記錄。
  • 還可以使用 !編碼數字 的方式來重復執行某一次的命令。

8.sosreport命令

sosreport命令用於收集系統配置及架構信息並輸出診斷文檔,格式為sosrepor

[[email protected] ~]# sosreport
sosreport (version 3.0)
This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp and may be provided to a Red Hat support representative.
Any information provided to Red Hat will be treated in accordance with
the published support policies at:

https://access.redhat.com/support/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.
Press ENTER to continue, or CTRL-C to quit. 此處敲擊回車來確認收集信息

Please enter your first initial and last name [linuxprobe.com]:此處敲擊回車來確認主機編號
Please enter the case number that you are generating this report for:此處敲擊回車來確認主機編號
Running plugins. Please wait ...
Running 70/70: yum...
Creating compressed archive...
Your sosreport has been generated and saved in:

/var/tmp/sosreport-linuxprobe.com-20170905230631.tar.xz
The checksum is: 79436cdf791327040efde48c452c6322
Please send this file to your support representative.

工作目錄切換命令

1.pwd命令

pwd命令用於顯示用戶當前所處的工作目錄,格式為“pwd [選項]”

[[email protected] etc]# pwd
/etc

2.cd命令

cd命令用於切換工作路徑,格式為“cd [目錄名稱]”

  • 使用 cd - 命令返回到上一次所處的目錄
  • 使用 cd.. 命令進入上級目錄
  • 使用 cd ~ 命令切換到當前用戶的家目錄

3.ls命令

ls命令用於顯示目錄中的文件信息,格式為“ls [選項] [文件]

  • 使用ls命令的 -a 參數看到全部文件(包括隱藏文件)
  • 使用 -l 參數可以查看文件的屬性、大小等詳細信息

文本文件編輯命令

1.cat命令

cat命令用於查看純文本文件(內容較少的),格式為“cat [選項] [文件]”

  • 如果在查看文本內容時還想順便顯示行號的話,不妨在cat命令後面追加一個-n參數:
[[email protected] ~]# cat -n initial-setup-ks.cfg 
     1    #version=RHEL7
     2    # X Window System configuration information
     3    xconfig  --startxonboot
     4    
     5    # License agreement
     6    eula --agreed
     7    # System authorization information
     8    auth --enableshadow --passalgo=sha512
     9    # Use CDROM installation media
    10    cdrom
    11    # Run the Setup Agent on first boot
    12    firstboot --enable
    13    # Keyboard layouts
    14    keyboard --vckeymap=us --xlayouts=‘us‘
    15    # System language
    16    lang en_US.UTF-8
………………省略部分輸出信息………………

2.more命令

more命令用於查看純文本文件(內容較多的),格式為“more [選項]文件

[[email protected] ~]# more initial-setup-ks.cfg 
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=‘us‘
# System language
lang en_US.UTF-8

ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
network  --bootproto=dhcp --hostname=linuxprobe.com
--More--(43%)

3.head命令

head命令用於查看純文本文檔的前N行,格式為“head [選項] [文件]”

  • 查看文本中前20行的內容:
[[email protected] ~]# head -n 20  initial-setup-ks.cfg 
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=‘us‘
# System language
lang en_US.UTF-8

ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
[[email protected] ~]#

4.tail命令

tail命令用於查看純文本文檔的後N行或持續刷新內容,格式為“tail [選項] [文件]”

  • tail命令最強悍的功能是可以持續刷新一個文件的內容,當想要實時查看最新日誌文件時,這特別有用,此時的命令格式為“tail -f 文件名”:
[[email protected] ~]# tail -f /var/log/messages
May  4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack
May  4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack
May  4 07:56:38 localhost vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init()
May  4 07:56:50 localhost systemd-logind: Removed session c1.
Aug  1 01:05:31 localhost systemd: Time has been changed
Aug  1 01:05:31 localhost systemd: Started LSB: Bring up/down networking.
Aug  1 01:08:56 localhost dbus-daemon: dbus[1124]: [system] Activating service name=‘com.redhat.SubscriptionManager‘ (using servicehelper)
Aug  1 01:08:56 localhost dbus[1124]: [system] Activating service name=‘com.redhat.SubscriptionManager‘ (using servicehelper)
Aug  1 01:08:57 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service ‘com.redhat.SubscriptionManager‘
Aug  1 01:08:57 localhost dbus[1124]: [system] Successfully activated service ‘com.redhat.SubscriptionManager‘

5.tr命令

tr命令用於替換文本文件中的字符,格式為“tr [原始字符] [目標字符]”

  • 例如,把某個文本內容中的英文全部替換為大寫:
[[email protected] ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
#VERSION=RHEL7
# SYSTEM AUTHORIZATION INFORMATION
AUTH --ENABLESHADOW --PASSALGO=SHA512

# USE CDROM INSTALLATION MEDIA
CDROM
# RUN THE SETUP AGENT ON FIRST BOOT
FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA
# KEYBOARD LAYOUTS
KEYBOARD --VCKEYMAP=US --XLAYOUTS=‘US‘
# SYSTEM LANGUAGE
LANG EN_US.UTF-8

# NETWORK INFORMATION
NETWORK --BOOTPROTO=DHCP --DEVICE=ENO16777728 --ONBOOT=OFF --IPV6=AUTO
NETWORK --HOSTNAME=LOCALHOST.LOCALDOMAIN
# ROOT PASSWORD
ROOTPW --ISCRYPTED $6$PDJJF42G8C6PL069$II.PX/YFAQPO0ENW2PA7MOMKJLYOAE2ZJMZ2UZJ7BH3UO4OWTR1.WK/HXZ3XIGMZGJPCS/MGPYSSOI8HPCT8B/
# SYSTEM TIMEZONE
TIMEZONE AMERICA/NEW_YORK --ISUTC
USER --NAME=LINUXPROBE --PASSWORD=$6$A9V3INSTNBWEIR7D$JEGFYWBCDOOOKJ9SODECCDO.ZLF4OSH2AZ2SS2R05B6LZ2A0V2K.RJWSBALL2FEKQVGF640OA/TOK6J.7GUTO/ --ISCRYPTED --GECOS="LINUXPROBE"
# X WINDOW SYSTEM CONFIGURATION INFORMATION
XCONFIG --STARTXONBOOT
# SYSTEM BOOTLOADER CONFIGURATION
BOOTLOADER --LOCATION=MBR --BOOT-DRIVE=SDA
AUTOPART --TYPE=LVM
# PARTITION CLEARING INFORMATION
CLEARPART --NONE --INITLABEL 

%PACKAGES
@BASE
@CORE
@DESKTOP-DEBUGGING
@DIAL-UP
@FONTS
@GNOME-DESKTOP
@GUEST-AGENTS
@GUEST-DESKTOP-AGENTS
@INPUT-METHODS
@INTERNET-BROWSER
@MULTIMEDIA
@PRINT-CLIENT
@X11

%END

6.wc命令

wc命令用於統計指定文本的行數、字數、字節數,格式為“wc [參數] 文本”

技術分享圖片

[[email protected] ~]# wc -l /etc/passwd
38 /etc/passwd

7.stat命令

stat命令用於查看文件的具體存儲信息和時間等信息,格式為“stat 文件名稱”

[[email protected] ~]# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: 1213 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 68912908 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2017-07-14 01:46:18.721255659 -0400
Modify: 2017-05-04 15:44:36.916027026 -0400
Change: 2017-05-04 15:44:36.916027026 -0400
Birth: -

8.cut命令

cut命令用於按“列”提取文本字符,格式為“cut [參數] 文本”

  • 例如,提取以冒號(:)為間隔符的第一列的內容:
[[email protected] ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
unbound
colord
usbmuxd
avahi
avahi-autoipd
libstoragemgmt
saslauth
qemu
rpc
rpcuser
nfsnobody
rtkit
radvd
ntp
chrony
abrt
pulse
gdm
gnome-initial-setup
postfix
sshd
tcpdump
linuxprobe

9.diff命令

diff命令用於比較多個文本文件的差異,格式為“diff [參數] 文件”

  • 使用cat命令分別查看diff_A.txt和diff_B.txt文件的內容,然後進行比較:
[[email protected] ~]# cat diff_A.txt
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[[email protected] ~]# cat diff_B.txt
Welcome tooo linuxprobe.com

Red Hat certified
Free Linux LeSSonS
////////.....////////
Professional guidance
Linux Course
  • 使用帶有-c參數的diff命令來描述文件內容具體的不同:
[[email protected] ~]# diff -c diff_A.txt diff_B.txt
*** diff_A.txt 2017-08-30 18:07:45.230864626 +0800
--- diff_B.txt 2017-08-30 18:08:52.203860389 +0800
***************
*** 1,5 ****
! Welcome to linuxprobe.com
Red Hat certified
! Free Linux Lessons
Professional guidance
Linux Course
--- 1,7 ----
! Welcome tooo linuxprobe.com
!
Red Hat certified
! Free Linux LeSSonS
! ////////.....////////
Professional guidance
Linux Course

【Linux就該這麽學 20期培訓筆記 02】新手必須掌握的 Linux 命令