1.LINUX中基本命令整理集合
首先先說一下有些命令在終端輸入會顯示‘-bash: shell: 未找到命令’,不要著急,我教你一步一步配置yum源,安裝命令
(1)編輯文字檔案 vi /etc/yum.repos.d/yum.repo
如下所示:
[local] #軟體源的名稱 name=local #軟體倉庫的名字 baseurl=file:///mnt #軟體源地址,將光碟掛載至/mnt下 gpgcheck=0 #gpg的校驗,確定軟體包的來源安全和有效,1為啟用 enabled=1 #啟用/源
(2)軟體安裝命令 yum install 軟體包名稱,其次 yum clean all 清除所有倉庫快取,這樣命令就可以在終端使用。(如果依舊用不了顯示未找到命令,看第三步)
(3)yum provides 軟體包名稱 ,檢視這個包提供的軟體源 ,如下所示tree命令的軟體源:tree-1.6.0-10.el7.x86_64 : File system tree viewer 源 :local ,將其安裝 yum install 軟體源(複製貼上即可),yum clean all 清除所有倉庫快取,就ok了。
[[email protected] ~]# yum provides tree 已載入外掛:fastestmirror Loading mirror speeds from cached hostfile tree-1.6.0-10.el7.x86_64 : File system tree viewer 源 :local tree-1.6.0-10.el7.x86_64 : File system tree viewer 源 :@local
linux中常用命令
1.echo命令
將指定的字串字串輸出到終端螢幕
eg:輸出hello,world
[[email protected] ~]# echo 'hello,world'
hello,world
2.date命令
顯示時間和設定系統時間
eg:顯示時間
[[email protected] ~]# date
2018年 09月 19日 星期三 01:33:58 CST
eg:修改系統時間(用到一個-s的引數)
[r[email protected] ~]# date -s '20180918 17:35:30' 2018年 09月 18日 星期二 17:35:30 CST
3.reboot命令
reboot 命令用於重啟系統
4.poweroff命令
poweroff 命令用於關閉系統
5.wget命令
wget命令用於在終端中下載網路檔案
eg;下載Python的壓縮包 wget https://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
[[email protected] ~]# wget https://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
--2018-09-19 03:51:40-- https://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
正在解析主機 www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:1a::223
正在連線 www.python.org (www.python.org)|151.101.108.223|:443... 已連線。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:11754834 (11M) [application/octet-stream]
正在儲存至: “Python-2.7.2.tar.bz2”
100%[===================================================>] 11,754,834 2.59MB/s 用時 7.6s
2018-09-19 03:51:55 (1.47 MB/s) - 已儲存 “Python-2.7.2.tar.bz2” [11754834/11754834])
6.ps命令
ps 命令用於檢視系統中的程序狀態
ps命令的引數及作用
引數 | 作用 |
---|---|
-a | 顯示所有程序 (包括其它使用者的程序) |
-u | 使用者以及其他的詳細資訊 |
-x | 顯示沒有控制終端的程序 |
[[email protected] ~]# ps
PID TTY TIME CMD
1502 pts/0 00:00:00 bash
1869 pts/0 00:00:00 ps
[[email protected] ~]# ps -a
PID TTY TIME CMD
1888 pts/0 00:00:00 ps
[[email protected] ~]# ps -u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1471 0.0 0.1 115520 2120 tty1 Ss+ 02:09 0:00 -bash
root 1502 0.0 0.1 115524 2176 pts/0 Ss 02:11 0:00 -bash
root 1889 0.0 0.0 151064 1804 pts/0 R+ 04:13 0:00 ps -u
[[email protected] ~]# ps -x
PID TTY STAT TIME COMMAND
1 ? Ss 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 2
2 ? S 0:00 [kthreadd]
3 ? S 0:00 [ksoftirqd/0]
(ps -x後續有很多沒有控制終端的程序,因為太長沒辦法搞出來讓大家看)
7.top命令
top 命令用於動態地監視程序活動與系統負載等資訊,相當於windows中的工作管理員。
第 1 行:系統時間、執行時間、登入終端數、系統負載(三個數值分別為 1 分鐘、5 分鐘、15 分鐘內的平均值,數值越小意味著負載越低)。
第 2 行:程序總數、執行中的程序數、睡眠中的程序數、停止的程序數、僵死的程序數。
第 3 行:使用者佔用資源百分比、系統核心佔用資源百分比、改變過優先順序的程序資源百分比、空閒的資源百分比等。
第 4 行:實體記憶體總量、記憶體使用量、記憶體空閒量、作為核心快取的記憶體量。
第 5 行:虛擬記憶體總量、虛擬記憶體使用量、虛擬記憶體空閒量、已被提前載入的記憶體量。
8.pidof命令
pidof 命令用於查詢某個指定服務程序的 PID 值
eg:在top中得知有一個程序是systemd 用pidof命令查詢它的PID值
[[email protected] ~]# pidof systemd
1
9.kill 命令
kill 命令用於終止某個指定 PID 的服務程序 引數 -9是最高階強制終止某程序
killall 命令是終止某個指定名稱的服務所對應的全部程序
系統狀態檢測命令
1.ifconfig命令
使用 ifconfig 命令來檢視本機當前的網絡卡配置與網路狀態等資訊時,主要檢視的就是網絡卡名稱、inet 引數後面的 IP 地址、ether 引數後面的網絡卡實體地址(又稱為 MAC 地址),以及 RX、TX 的接收資料包與傳送資料包的個數及累計流量
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.243.128 netmask 255.255.255.0 broadcast 192.168.243.255
inet6 fe80::20c:29ff:fe4b:a67c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4b:a6:7c txqueuelen 1000 (Ethernet)
RX packets 14667 bytes 12648344 (12.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4804 bytes 387374 (378.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.uname命令
檢視系統核心與系統版本等資訊。 uname -a檢視當前系統的核心名稱、主機名、核心發行版本、節點名、系統時間、硬體名稱、硬體平臺、處理器型別以及作業系統名稱等資訊。
(作業系統版本檢視 cat /etc/redhat-release)
[[email protected] ~]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
作業系統版本
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
3.uptime 命令
用於檢視系統的負載資訊
[[email protected] ~]# uptime
05:38:51 up 3 min, 2 users, load average: 0.07, 0.16, 0.07
其中代表的是 系統時間 系統執行時間 以及兩個使用者 平均負載值 ,平均負載值有三個 分別代表的是1分鐘,5分鐘,15分鐘內的壓力情況,負載值越低越好。
4.free命令
free 用於顯示當前系統中記憶體的使用量資訊 ,通常和引數 -h 一起使用。
[[email protected] ~]# free -h 程序共享 磁碟快取的 系統可用
記憶體總量 已用量 可用量 的記憶體量 記憶體量 記憶體
total used free shared buff/cache available
Mem: 1.8G 125M 1.5G 8.7M 159M 1.5G
Swap: 2.0G 0B 2.0G
5.who命令
當前的登入使用者
[[email protected] ~]# who
root tty1 2018-09-20 05:36
root pts/0 2018-09-20 05:37 (192.168.24.1)
6.last命令
用於檢視所有系統的登入記錄
[[email protected] ~]# last
root pts/0 192.168.24.1 Thu Sep 20 05:37 still logged in
root tty1 Thu Sep 20 05:36 still logged in
reboot system boot 3.10.0-693.el7.x Thu Sep 20 05:35 - 05:56 (00:21)
root tty1 Thu Sep 20 05:34 - 05:34 (00:00)
(登入記錄很多,列舉開頭一部分)
7.history命令
用於檢視歷史命令
裡面有幾個引數 -c刪除所有的歷史命令
8.sosreport命令
用於收集系統配置及架構資訊並輸出診斷文件
工作目錄切換命令
1.pwd命令
顯示當前所處的工作目錄
[[email protected] ~]# pwd
/root
2.cd命令
切換工作目錄 cd … 退回上級目錄 cd -命令返回到上一次所處的目錄 cd~ 進入當前使用者的家目錄 進入其他目錄 路徑一定要對 可以用Tab補齊
3.ls命令
令用於顯示目錄中的檔案資訊 引數 -a 看到包括所有的以·開頭的隱藏檔案,-l可以顯示檔案屬性,大小資訊等。
eg:(usr目錄下的檔案)
[[email protected] usr]# ls
bin etc games include lib lib64 libexec local sbin share src tmp
[[email protected] usr]# ls -a
. .. bin etc games include lib lib64 libexec local sbin share src tmp
[[email protected] usr]# ls -l
總用量 104
dr-xr-xr-x. 2 root root 20480 9月 18 17:42 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 3 root root 23 7月 19 17:31 include
dr-xr-xr-x. 27 root root 4096 7月 19 17:32 lib
文字檔案編輯命令
1.cat命令
用於檢視純文字檔案,內容比較少的檔案
2.more命令
也是用於檢視純文字檔案,內容較多的檔案
3.head命令
用於檢視文字檔案的前多少行 會用到引數 -n後面帶前幾行,前3行就是 -n3
[[email protected] ~]# cat /etc/passwd | head -n3 (檢視/etc/passwd檔案中的前三行 通過管道的方法直接提取前3行)
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.tail命令
用法和head相似,檢視一個檔案的後幾行,也用-n引數,後3行就是-n3,就不一一演示了。tail還有一個功能就是持續重新整理一個檔案的內容,想要實時檢視最新日誌檔案時 tail -f 檔名
[[email protected] ~]# tail -f /var/log/messages
Sep 21 15:02:30 localhost systemd: Starting Network Manager Script Dispatcher Service...
Sep 21 15:02:30 localhost dbus-daemon: dbus[577]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Sep 21 15:02:30 localhost dhclient[734]: bound to 192.168.24.129 -- renewal in 732 seconds.
Sep 21 15:02:30 localhost dbus[577]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
5.tr命令
用於替換文字檔案中的字元,tr ‘原始的字元’ ‘目標字元’
6.wc命令
用於統計指定文字的行數,字數,位元組數
引數 | 作用 |
---|---|
-l | 只顯示行數 |
-w | 只顯示單詞數 |
-c | 只顯示位元組數 |
統計/etc/passwd 中的行數 單詞數 位元組數
[[email protected] ~]# cat /etc/passwd |wc 不加任何引數 直接顯示 行數 單詞數 位元組數
18 26 798
[[email protected] ~]# cat /etc/passwd | wc -l 行數
18
[[email protected] ~]# cat /etc/passwd | wc -w 單詞數
26
[[email protected] ~]# cat /etc/passwd | wc -c 位元組數
798
7.stat命令
令用於檢視檔案的具體儲存資訊和時間等資訊
8.cut命令
cut 命令用於按“列”提取文字字元,我覺得初次接觸這個命令相比較其他命令有趣的多,有一個引數 -d 設定間隔符號,-f是看提取哪一列,比如第1列就是 -f1
eg:提取/etc/passwd 中的第一列
[[email protected] ~]# cat /etc/passwd | cut -d ':' -f1
root
bin
daemon
adm
lp
sync
shutdown
9.diff命令
令用於比較多個文字檔案的差異,diff --brief 檔案a 檔案b
[[email protected] ~]# vi a.txt
[[email protected] ~]# vi b.txt
[[email protected] ~]# diff --brief a.txt b.txt
檔案 a.txt 和 b.txt 不同
10.patch命令
用於為開放原始碼軟體安裝補丁程式
檔案目錄管理命令
1.touch命令
用於建立建立空白檔案或設定檔案時間,如下有幾個引數
引數 | 作用 |
---|---|
-a | 僅修改“讀取時間”(atime) |
-m | 僅修改“修改時間”(mtime) |
-d | 同時修改 atime 與 mtime |
2.mkdir命令 (這個命令得好好說說)
建立一個空白目錄
[[email protected] hahaha]# ls
[[email protected] hahaha]# mkdir dir1
[[email protected] hahaha]# ls
dir1
建立同級目錄
[[email protected] hahaha]# mkdir dir2 dir3 dir4
[[email protected] hahaha]# ls
dir1 dir2 dir3 dir4
有一個引數 -p 遞迴建立目錄,在一級一級建立目錄
[[email protected] dir1]# mkdir -p dir5/dir6/dir7
[[email protected] dir1]# tree
.
└── dir5
└── dir6
└── dir7
3 directories, 0 files
建立許可權是755 的目錄,用到 -m引數
[[email protected] dir2]# mkdir -m 755 dir8
[[email protected] dir2]# ls
dir8
[[email protected] dir2]# ll
總用量 0
drwxr-xr-x. 2 root root 6 9月 21 16:35 dir8
建立目錄,顯示已建立的目錄用-v引數
[[email protected] dir3]# mkdir -v dir9
mkdir: 已建立目錄 "dir9"
最後一個是同時建立同級和子級目錄,如何建立
[[email protected] dir1]# mkdir -p {dir1,dir2/{dir3/{dir4},dir5},dir6,dir7/{dir8}}
[[email protected] dir1]# tree
.
├── dir1
├── dir2
│ ├── dir3
│ │ └── {dir4}
│ └── dir5
├── dir6
└── dir7
└── {dir8}
8 directories, 0 files
3.cp命令
用於複製檔案或目錄
引數 | 作用 |
---|---|
-p | 保留原始檔案的屬性 |
-d | 若物件為“連結檔案”,則保留該“連結檔案”的屬性 |
-r | 遞迴持續複製(用於目錄) |
-i | 若目標檔案存在則詢問是否覆蓋 |
-a | 相當於 -pdr(p、d、r 為上述引數的作用) |
4.mv命令
用於剪下檔案或將檔案重新命名
5.rm命令
用於刪除檔案或目錄 引數 -f 強制刪除,想要刪除一個目錄,需要在 rm 命令
後面一個-r 引數才可以,否則刪除不掉。
6.dd命令
用於按照指定大小和個數的資料塊來複制檔案或轉換檔案
dd 命令的引數及其作用
引數 | 作用 |
---|---|
if | 輸入的檔名稱 |
of | 輸出的檔名稱 |
bs | 設定每個“塊”的大小 |
count | 設定要複製“塊”的個數 |
7.file命令
用於檢視檔案的型別
[[email protected] /]# file /etc/passwd
/etc/passwd: ASCII text
[[email protected] /]# file /etc
/etc: directory