博客地址:https://www.hz-bin.cn/Linux-cmd
echo
- 作用:在終端輸出字符串或變量提取后的值,格式為
echo [字符串 | $變量]。 - 參數:無
root@ubuntu:~# echo HelloWorld
HelloWorld
root@ubuntu:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
date
- 作用:顯示及設置系統(tǒng)的時間或日期,格式為
date [選項] [+指定的格式],+開頭表示按指定格式輸出。 - 參數:
-
%H小時(00 ~ 23) -
%I小時(00 ~ 12) -
%M分鐘(00 ~ 59) -
%S秒(00 ~ 59) -
%j今年中的第幾天
-
root@ubuntu:~# date
Tue Aug 10 10:50:07 CST 2021
root@ubuntu:~# date "+%Y-%m-%d %H:%M:%S"
2021-08-10 10:50:10
root@ubuntu:~# date "+%j"
222
reboot
- 作用:重啟系統(tǒng)
poweroff
- 作用:關閉系統(tǒng)
wget
- 作用:在終端中下載網絡文件,格式為
wget [參數] 下載地址。 - 參數:
-
-b后臺下載模式 -
-P下載到指定的目錄 -
-t最大嘗試次數 -
-c斷點續(xù)傳 -
-p下載頁面內的所有資源,包括圖片、視頻等 -
-r遞歸下載
-
-
wget http://www.linuxprobe.com/docs/LinuxProbe.pdf,下載LinuxProbe.pdf。
ps
- 作用:查看系統(tǒng)中的進程狀態(tài),格式為
ps [參數]。 - 參數:
-
-a顯示所有進程(包括其他用戶的進程) -
-u用戶以及其他信息 -
-x顯示沒有控制終端的進程
-
root@ubuntu:~# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 159724 3464 ? Ss Aug09 0:01 /sbin/init maybe-ubiquity
root 2 0.0 0.0 0 0 ? S Aug09 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< Aug09 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? I< Aug09 0:00 [mm_percpu_wq]
root 7 0.0 0.0 0 0 ? S Aug09 0:14 [ksoftirqd/0]
root 8 0.0 0.0 0 0 ? I Aug09 1:27 [rcu_sched]
root 9 0.0 0.0 0 0 ? I Aug09 0:00 [rcu_bh]
root 10 0.0 0.0 0 0 ? S Aug09 0:00 [migration/0]
...... 省略
VSZ:虛擬內存使用量,單位KB
RSS:占用的固定內存量,單位KB
STAT:進程狀態(tài),R(運行),S(終端),D(不可中斷),Z(僵死),T(停止)
TIME:實際使用CPU的時間
top
- 作用:動態(tài)地監(jiān)視進程活動與系統(tǒng)負載等信息。
root@ubuntu:~# top
top - 11:01:58 up 1 day, 1:30, 3 users, load average: 0.00, 0.00, 0.00
系統(tǒng)時間 運行時間 登錄終端數 系統(tǒng)負載:1分鐘,5分鐘,15分鐘內的平均值
Tasks: 101 total, 1 running, 58 sleeping, 0 stopped, 0 zombie
進程總數 運行中 睡眠中 已停止 僵死
%Cpu(s): 0.3 us, 0.0 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 2040924 total, 1500768 free, 290220 used, 249936 buff/cache
物理內存總量 內存空閑兩 內存使用量 作為內核緩存的內存量
KiB Swap: 2097148 total, 2089968 free, 7180 used. 1569476 avail Mem
虛擬內存總量
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 159724 3464 1116 S 0.0 0.2 0:01.28 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
7 root 20 0 0 0 0 S 0.0 0.0 0:14.30 ksoftirqd/0
8 root 20 0 0 0 0 I 0.0 0.0 1:27.94 rcu_sched
9 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_bh
10 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
...... 省略
pidof
- 作用:查詢某個指定服務進程的PID值。
root@ubuntu:~# pidof mysqld
1103
kill
- 作用:終止某個指定PID的服務進程
killall
- 作用:終止某個指定名稱的服務所對應的全部進程,如
killall httpd
ifconfig
- 作用:獲取網卡配置與網絡狀態(tài)等信息
uname
- 作用:查看系統(tǒng)內核與系統(tǒng)版本等信息
root@ubuntu:~# uname
Linux
root@ubuntu:~# uname -a
Linux ubuntu 4.15.0-153-generic #160-Ubuntu SMP Thu Jul 29 06:54:29 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
uptime
- 作用:查看系統(tǒng)的負載信息。
root@ubuntu:~# uptime
11:19:31 up 1 day, 1:48, 3 users, load average: 0.00, 0.00, 0.00
free
- 作用:顯示當前系統(tǒng)中內存的使用量信息。
root@ubuntu:~# free -h
total used free shared buff/cache available
Mem: 1.9G 262M 1.4G 724K 274M 1.6G
Swap: 2.0G 0B 2.0G
who
- 作用:查看當前登入主機的用戶終端信息。
last
- 作用:查看所有系統(tǒng)的登錄記錄。
history
- 作用:顯示歷史執(zhí)行過的命令??梢允褂?
!編碼數字的方式來重復執(zhí)行某一次的命令。
root@ubuntu:~# history
...... 省略
162 cd ~
163 clear
164 uptime
165 free -h
166 who
167 last
168 history
root@ubuntu:~# !164
uptime
11:27:19 up 6 min, 1 user, load average: 0.00, 0.00, 0.00
sosreport
- 作用:收集系統(tǒng)配置及架構信息并輸出診斷文檔
root@ubuntu:~# sosreport
sosreport (version 3.9)
This command will collect system configuration and diagnostic
information from this Ubuntu system.
For more information on Canonical visit:
https://www.ubuntu.com/
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 the case id that you are generating this report for []:
Setting up archive ...
Setting up plugins ...
[plugin:networking] skipped command 'nft list ruleset': required kmods missing: nf_tables.
[plugin:networking] skipped command 'ip -s macsec show': required kmods missing: macsec. Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'ss -peaonmi': required kmods missing: tcp_diag, af_packet_diag, inet_diag, udp_diag, netlink_diag, unix_diag. Use '--allow-system-changes' to enable collection.
Running plugins. Please wait ...
Finishing plugins [Running: ubuntu]
Finished running plugins
Creating compressed archive...
Your sosreport has been generated and saved in:
/tmp/sosreport-ubuntu-2021-08-10-shnpxye.tar.xz
Size 2.76MiB
Owner root
md5 7b43aab2e9a6110e4e8a188e39133440
Please send this file to your support representative.
pwd
- 作用:顯示用戶當前所處的工作目錄。
cd
- 作用:切換工作路徑。
-
cd -表示返回到上一次的目錄,cd ~表示切換到用戶的家目錄。
ls
- 作用:顯示目錄中的文件信息。
- 參數:
-
-a查看全部文件,包括隱藏文件 -
-l查看文件的屬性、大小等詳細信息
-
cat
- 作用:查看純文本文件(內容較少的)。
more
- 作用:查看純文本文件(內容較多的),使用空格鍵或回車鍵向下翻頁。
head
- 作用:查看純文本文檔的前N行,
head -n 20 a.txt
tail
- 作用:查看純文本文檔的后N行或持續(xù)刷新內容,
-
tail -n 20 a.txt查看a.txt的最后20行,tail -f a.txt持續(xù)查看文件的內容
tr
- 作用:替換文本文件中的字符,格式為
tr [原始字符] [目標字符]。 -
cat a.txt | tr [a-z] [A-Z]將文件中的小寫字母替換為大寫字母。
wc
- 作用:統(tǒng)計指定文本的行數、字數、字節(jié)數。
- 參數:
-
-l只顯示行數 -
-w只顯示單詞數 -
-c只顯示字節(jié)數
-
root@ubuntu:~# wc -l /etc/passwd
32 /etc/passwd
stat
- 作用:查看文件的具體存儲信息和時間等信息,格式為
stat文件名稱。
root@ubuntu:~# stat /etc/passwd
File: /etc/passwd
Size: 1673 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 416695 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-08-09 16:02:09.921445530 +0800
Modify: 2021-08-09 16:02:09.893445577 +0800
Change: 2021-08-09 16:02:09.897445570 +0800
Birth: -
cut
- 作用:按列提取文本字符,格式為
cut [參數] 文本。 - 參數:
-
-d設置間隔符號 -
-f設置需要看的列數
-
root@ubuntu:~# cut -d: -f1 /etc/passwd
root
daemon
bin
sys
...... 省略
diff
- 作用:比較多個文本文件的差異,格式為
diff [參數] [文件]。 - 參數:
-
--brief顯示比較后的結果 -
-c顯示文件內容具體的不同
-
touch
- 作用:創(chuàng)建空白文件或設置文件的時間,格式為
touch [選項] [文件]。 - 參數:
-
-a僅修改“讀取時間”(atime) -
-m僅修改“修改時間”(mtime) -
-d同時修改 atime 和 mtime
-
mkdir
- 作用:創(chuàng)建空白的目錄,
-p參數遞歸穿件目錄
cp
- 作用:復制文件或目錄,格式為
cp [選項] 源文件 目標文件。 - 參數:
-
-p保留原始文件的屬性 -
-d若文件為鏈接文件,則保留該鏈接文件的屬性 -
-r遞歸復制,用于目錄 -
-i若目標文件存在則詢問是否覆蓋 -
-a相當于 -pdr
-
mv
- 作用:剪切文件或將文件重命名,格式為
mv [選項] 源文件 [目標路徑|目標文件名]。
rm
- 作用:刪除文件或目錄,格式為
rm [選項] [文件|目錄]。
file
- 作用:查看文件的類型,
file a.txt
tar
- 作用:用于對文件進行打包壓縮或解壓,格式為
tar [選項] [文件]。 - 參數:
-
-c創(chuàng)建壓縮文件 -
-x解壓縮文件 -
-z用Gzip壓縮或解壓 -
-v顯示壓縮或解壓過程 -
-f指定目標文件名 -
-C指定解壓到的目錄
-
- 壓縮:
tar -czvf etc.tar.gz /etc,將/etc目錄壓縮為etc.tar.gz。 - 解壓:
tar -xzvf etc.tar.gz -C /root/etc,將etc.tar.gz解壓到/root/etc目錄。
grep
- 作用:在文本中執(zhí)行關鍵詞搜索,并顯示匹配的結果,格式為
grep [選項] [文件]。 - 參數:
-
-b將可執(zhí)行文件當做文本文件來搜索 -
-c顯示找到的行數 -
-i忽略大小寫 -
-n顯示行號 -
-v反向選擇
-
-
grep "/sbin/nologin" /etc/passwd,在/etc/passwd中搜索關鍵詞/sbin/nologin。
find
- 作用:按照指定條件來查找文件,格式為
find [查找路徑] 尋找條件 操作。 - 參數:
-
-name匹配名稱 -
-user匹配所有者 -
-mtime -n +n匹配修改內容的時間(-n 指 n 天以內,+n 指 n 天以前) -
-exec ...... {} \;后面可跟用于進一步處理搜索結果的命令,{}表示搜索出的每一個文件,\;表示命令的結尾。
-
-
find /etc -name "host*",在/etc目錄下查找以host開頭的文件。 -
find /etc -name "host*" -exec cp -a {} /root/findresult/ \;,將搜索到的結果拷貝到/root/findresult/目錄中
參考文檔:新手必須掌握的Linux命令