本章知識點(diǎn)
強(qiáng)大好用的SHELL 執(zhí)行查看幫助命令 常用系統(tǒng)工作命令 系統(tǒng)狀態(tài)檢測命令 工作目錄切換命令 文本文件編輯命令 文件目錄管理命令 打包壓縮與搜索命令
Linux命令大全(手冊)
強(qiáng)大好用的SHELL
Shell (也稱為終端)充當(dāng)?shù)氖侨伺c內(nèi)核(硬件)之間的翻譯官
主流 Linux 系統(tǒng)默認(rèn)使用的 Shell 是 Bash
1:通過上下方向鍵來調(diào)取過往執(zhí)行過的Linux命令; 2:命令或參數(shù)僅需輸入前幾位就可以用Tab鍵補(bǔ)全; 3:具有強(qiáng)大的批處理腳本; 4:具有實(shí)用的環(huán)境變量功能。
執(zhí)行查看幫助命令
常見執(zhí)行Linux命令的格式:
命令名稱 [命令參數(shù)] [命令對象]
注意,命令名稱、命令參數(shù)、命令對象之間請用空格鍵分隔。
命令參數(shù)的長格式與短格式
短格式+短格式:可合并
man 命令可查看關(guān)于命令的幫助信息
man <需要幫助的命令>
常用系統(tǒng)工作命令
1. echo命令
echo命令用于在終端輸出字符串或變量提取后的值,格式為“echo [字符串 | $變量]”
[root@linuxprobe ~]# echo $SHELL /bin/bash
2.date命令
date命令用于顯示及設(shè)置系統(tǒng)的時間或日期,格式為“date [選項(xiàng)] [+指定的格式]”
按照“年-月-日 小時:分鐘:秒”的格式查看當(dāng)前系統(tǒng)時間
[root@linuxprobe ~]# date "+%Y-%m-%d %H:%M:%S" 2017-08-24 16:29:12
將系統(tǒng)的當(dāng)前時間設(shè)置為2017年9月1日8點(diǎn)30分
[root@linuxprobe ~]# date -s "20170901 8:30:00" Fri Sep 1 08:30:00 CST 2017
使用date命令并按照默認(rèn)的格式查看當(dāng)前的系統(tǒng)時間
[root@linuxprobe ~]# date Fri Sep 1 08:30:01 CST 2017
date命令中的參數(shù)%j可用來查看今天是當(dāng)年中的第幾天
[root@linuxprobe ~]# date "+%j" 244
3.reboot命令
reboot命令用于重啟系統(tǒng),其格式為reboot。
由于重啟計(jì)算機(jī)這種操作會涉及硬件資源的管理權(quán)限,因此默認(rèn)只能使用root管理員來重啟,其命令如下
[root@linuxprobe ~]# reboot
4.poweroff命令
poweroff命令用于關(guān)閉系統(tǒng),其格式為poweroff。
該命令與reboot命令相同,都會涉及硬件資源的管理權(quán)限,因此默認(rèn)只有root管理員才可以關(guān)閉電腦,其命令如下:
[root@linuxprobe ~]# poweroff
5.wget命令
wget命令用于在終端中下載網(wǎng)絡(luò)文件,格式為“wget [參數(shù)] 下載地址”
嘗試使用wget命令從本書的配套站點(diǎn)中下載本書的最新pdf格式電子文檔,這個文件的完整路徑為https://www.linuxprobe.com/docs/LinuxProbe.pdf, 執(zhí)行該命令后的下載效果如下:
[root@linuxprobe ~]# 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命令用于查看系統(tǒng)中的進(jìn)程狀態(tài),格式為“ps [參數(shù)]”
進(jìn)程狀態(tài)含義如下所示:
7.top命令
top命令用于動態(tài)地監(jiān)視進(jìn)程活動與系統(tǒng)負(fù)載等信息,其格式為top
第1行:系統(tǒng)時間、運(yùn)行時間、登錄終端數(shù)、系統(tǒng)負(fù)載(三個數(shù)值分別為1分鐘、5分鐘、15分鐘內(nèi)的平均值,數(shù)值越小意味著負(fù)載越低)。 第2行:進(jìn)程總數(shù)、運(yùn)行中的進(jìn)程數(shù)、睡眠中的進(jìn)程數(shù)、停止的進(jìn)程數(shù)、僵死的進(jìn)程數(shù)。 第3行:用戶占用資源百分比、系統(tǒng)內(nèi)核占用資源百分比、改變過優(yōu)先級的進(jìn)程資源百分比、空閑的資源百分比等。其中數(shù)據(jù)均為CPU數(shù)據(jù)并以百分比格式顯示,例如“97.1 id”意味著有97.1%的CPU處理器資源處于空閑。 第4行:物理內(nèi)存總量、內(nèi)存使用量、內(nèi)存空閑量、作為內(nèi)核緩存的內(nèi)存量。 第5行:虛擬內(nèi)存總量、虛擬內(nèi)存使用量、虛擬內(nèi)存空閑量、已被提前加載的內(nèi)存量。
8.pidof命令
pidof命令用于查詢某個指定服務(wù)進(jìn)程的PID值,格式為“pidof [參數(shù)] [服務(wù)名稱]”
每個進(jìn)程的進(jìn)程號碼值(PID)是唯一的,因此可以通過PID來區(qū)分不同的進(jìn)程。例如,可以使用如下命令來查詢本機(jī)上sshd服務(wù)程序的PID:
[root@linuxprobe ~]# pidof sshd 2156
9.kill命令
kill命令用于終止某個指定PID的服務(wù)進(jìn)程,格式為“kill [參數(shù)] [進(jìn)程PID]”
接下來,我們使用kill命令把上面用pidof命令查詢到的PID所代表的進(jìn)程終止掉,其命令如下所示。這種操作的效果等同于強(qiáng)制停止sshd服務(wù)
[root@linuxprobe ~]# kill 2156
10.killall命令
killall命令用于終止某個指定名稱的服務(wù)所對應(yīng)的全部進(jìn)程,格式為:“killall [參數(shù)] [服務(wù)名稱]”
[root@linuxprobe ~]# pidof httpd 13581 13580 13579 13578 13577 13576 [root@linuxprobe ~]# killall httpd [root@linuxprobe ~]# pidof httpd [root@linuxprobe ~]#
系統(tǒng)狀態(tài)檢測命令
1.ifconfig命令
ifconfig命令用于獲取網(wǎng)卡配置與網(wǎng)絡(luò)狀態(tài)等信息,格式為“ifconfig [網(wǎng)絡(luò)設(shè)備] [參數(shù)]”
[root@linuxprobe ~]# 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命令用于查看系統(tǒng)內(nèi)核與系統(tǒng)版本等信息,格式為“uname [-a]”
[root@linuxprobe ~]# 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
順帶一提,如果要查看當(dāng)前系統(tǒng)版本的詳細(xì)信息,則需要查看redhat-release文件,其命令以及相應(yīng)的結(jié)果如下:
[root@linuxprobe ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.0 (Maipo)
3.uptime命令
uptime用于查看系統(tǒng)的負(fù)載信息,格式為uptime
[root@linuxprobe ~]# uptime 22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18
4.free命令
free用于顯示當(dāng)前系統(tǒng)中內(nèi)存的使用量信息,格式為“free [-h]”
[root@linuxprobe ~]# free -h
5.who命令
who用于查看當(dāng)前登入主機(jī)的用戶終端信息,格式為“who [參數(shù)]”
[root@linuxprobe ~]# who
6.last命令
last命令用于查看所有系統(tǒng)的登錄記錄,格式為“l(fā)ast [參數(shù)]”
[root@linuxprobe ~]# 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命令用于顯示歷史執(zhí)行過的命令,格式為“history”
執(zhí)行history命令能顯示出當(dāng)前用戶在本地計(jì)算機(jī)中執(zhí)行過的最近 1000 條命令記錄。如果覺得1000不夠用,還可以自定義 /etc/profile 文件中的HISTSIZE變量值。 在使用history命令時,如果使用 -c 參數(shù)則會清空所有的命令歷史記錄。 還可以使用 !編碼數(shù)字 的方式來重復(fù)執(zhí)行某一次的命令。
8.sosreport命令
sosreport命令用于收集系統(tǒng)配置及架構(gòu)信息并輸出診斷文檔,格式為sosrepor
[root@linuxprobe ~]# 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. 此處敲擊回車來確認(rèn)收集信息 Please enter your first initial and last name [linuxprobe.com]:此處敲擊回車來確認(rèn)主機(jī)編號 Please enter the case number that you are generating this report for:此處敲擊回車來確認(rèn)主機(jī)編號 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命令用于顯示用戶當(dāng)前所處的工作目錄,格式為“pwd [選項(xiàng)]”
[root@linuxprobe etc]# pwd /etc
2.cd命令
cd命令用于切換工作路徑,格式為“cd [目錄名稱]”
使用 cd – 命令返回到上一次所處的目錄 使用 cd.. 命令進(jìn)入上級目錄 使用 cd ~ 命令切換到當(dāng)前用戶的家目錄
3.ls命令
ls命令用于顯示目錄中的文件信息,格式為“l(fā)s [選項(xiàng)] [文件]
使用ls命令的 -a 參數(shù)看到全部文件(包括隱藏文件) 使用 -l 參數(shù)可以查看文件的屬性、大小等詳細(xì)信息
文本文件編輯命令
1.cat命令
cat命令用于查看純文本文件(內(nèi)容較少的),格式為“cat [選項(xiàng)] [文件]”
如果在查看文本內(nèi)容時還想順便顯示行號的話,不妨在cat命令后面追加一個-n參數(shù):
[root@linuxprobe ~]# 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命令用于查看純文本文件(內(nèi)容較多的),格式為“more [選項(xiàng)]文件
[root@linuxprobe ~]# 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 [選項(xiàng)] [文件]”
查看文本中前20行的內(nèi)容:
[root@linuxprobe ~]# 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 [root@linuxprobe ~]#
4.tail命令
tail命令用于查看純文本文檔的后N行或持續(xù)刷新內(nèi)容,格式為“tail [選項(xiàng)] [文件]”
tail命令最強(qiáng)悍的功能是可以持續(xù)刷新一個文件的內(nèi)容,當(dāng)想要實(shí)時查看最新日志文件時,這特別有用,此時的命令格式為“tail -f 文件名”:
[root@linuxprobe ~]# 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 [原始字符] [目標(biāo)字符]”
例如,把某個文本內(nèi)容中的英文全部替換為大寫:
[root@linuxprobe ~]# 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命令用于統(tǒng)計(jì)指定文本的行數(shù)、字?jǐn)?shù)、字節(jié)數(shù),格式為“wc [參數(shù)] 文本”
[root@linuxprobe ~]# wc -l /etc/passwd 38 /etc/passwd
7.stat命令
stat命令用于查看文件的具體存儲信息和時間等信息,格式為“stat 文件名稱”
[root@linuxprobe ~]# stat anaconda-ks.cfg File: ‘a(chǎn)naconda-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 [參數(shù)] 文本”
例如,提取以冒號(:)為間隔符的第一列的內(nèi)容:
[root@linuxprobe ~]# 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 [參數(shù)] 文件”
使用cat命令分別查看diff_A.txt和diff_B.txt文件的內(nèi)容,然后進(jìn)行比較:
[root@linuxprobe ~]# cat diff_A.txt Welcome to linuxprobe.com Red Hat certified Free Linux Lessons Professional guidance Linux Course [root@linuxprobe ~]# cat diff_B.txt Welcome tooo linuxprobe.com Red Hat certified Free Linux LeSSonS ////////.....//////// Professional guidance Linux Course
使用帶有-c參數(shù)的diff命令來描述文件內(nèi)容具體的不同:
[root@linuxprobe ~]# 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