Linux系統常用命令
下面是Linux系統一些常見的操作命令的使用情況介紹。
1.檔案相關命令
1) 檔案建立
① 建立單個檔案
touch test.txt
② 建立多個檔案
touch test1.txt test2.txt
touch {test1.txt,test2.txt}
2) 檔案刪除
① 刪除檔案時,系統會詢問是否刪除
rm test.txt
② 強制刪除檔案
rm -rf test.txt
3) 檔案移動或重新命名
① 檔案移動
mv test.txt /tmp/
② 檔案重新命名
mv test.txt a.txt
③ 檔案移動並重命名
mv test.txt /tmp/a.txt
4) 檔案拷貝、追加或重新命名
① 檔案拷貝
cp test.txt /tmp/
② 檔案拷貝並重命名
cp test.txt /tmp/a.txt
③ 一個檔案內容拷貝到裡另一個檔案中
cat test1.txt > test2.txt
④ 一個檔案內容追加到裡另一個檔案中
cat test1.txt >> test2.txt
5) vim檔案操作
① vim工作模式
② 插入命令
③ 定位命令
④ 刪除命令
⑤ 替換和取消命令
6) 檔案內容檢視cat、more、less、head、tail、grep、cut、sort
① 正序檢視
cat test.txt
② 倒序檢視
tac test.txt
③ 從前向後以頁讀取檔案(上翻:[b],下翻:[空格鍵])
more test.txt
④ 從第5行向後以頁讀取檔案(上翻:[b],下翻:[空格鍵])
more +5 test.txt
⑤ 查詢第一個出現"test"字串的行,並從該處前兩行開始顯示輸出
more +/test test.txt
⑥ 從前向後以頁讀取檔案(上翻:[pageup],下翻:[pagedown])
less test.txt
⑦ 顯示檔案的前n行(預設10行,不帶n)
head -n test.txt
⑧ 顯示檔案的後n行(預設10行,不帶n)
tail -n test.txt
⑨ 跟蹤顯示檔案新追加的內容
tail -f test.txt
⑩ 文字過濾,模糊查詢含字母a的行
grep a test.txt
⑪ 顯示test.txt檔案第1,3,5行(-d:指定分隔符;-f:指定檔案)
cut -d : -f 1,3,5 test.txt
⑫ 檔案內容排序(-k:指定欄位;3:第三列;-t:指定分割符;-n:以數字大小進行排序,-u:去重)
sort -k 3 -t : -n -u test.txt
7) 給檔案設定擁有者,許可權
① 將test.txt的使用者擁有者設為zwh1,組的擁有者設為zwh2
chown zwh1:zwh2 test.txt
② 將當前目錄下的所有檔案與子目錄的使用者擁有者為zwh1,組擁有者設為zwh2
chown -R zwh1:zwh2 *
③ 將當前目錄下的所有檔案與子目錄皆設為任何人可讀取
chmod -R a+r *
④ 將test1.txt 與test2.txt設為其擁有者和其所屬同一個組者可寫入,但其他以外的人則不可寫入
chmod ug+w,o-w test1.txt test2.txt
8)其他他檔案操作
① 檢視檔案詳情
stat test.txt
② 檢視檔案大小
du -h test.txt [root@hdp1 /]# du -h zwh/ 4.0K zwh/
③ 檢視資料夾及子目錄檔案大小
du -ah zwh/ [root@hdp1 /]# du -ah zwh/ 0 zwh/test1.txt 0 zwh/test2.txt 4.0K zwh/test.txt 4.0K zwh/
④ 回到原來路徑
cd -
⑤ 回到上級路徑
cd ..
2.系統命令
1)主機名操作
① 檢視主機名
hostname
② 修改主機名(重啟後永久生效),將名字修改即可
vim /etc/hostname
2)修改IP
vim /etc/sysconfig/network-scripts/ifcfg-eth0
3)檢視系統資訊
① 顯示全部資訊
uname -a
② 顯示作業系統的發行編號
uname -r
③ 顯示作業系統名稱
uname -s
4) 檢視檔案資訊
file test.txt
5) 檢視分割槽
df -h
6) 檢視使用者最近登入情況
last
3.使用者和組
1)使用者操作
① 新增一個tom使用者,設定它屬於users組,並添加註釋資訊
useradd -g users -c "hr tom" tom
② 設定tom使用者的密碼
passwd tom
③ 修改tom使用者的登陸名為tom1
usermod -l tom1 tom
④ 將tom新增到zwh和root組中
usermod -G zwh,root tom
2)使用者組操作
① 新增一個zwh的組
groupadd zwh
② 將tom使用者從root組和zwh組刪除
gpasswd -d tom root和gpasswd -d tom zwh
③ 將zwh組名修改為zwh1
groupmod -n zwh1 zwh
4.查詢
1) 查詢可執行的命令
which ls
2) 從某個資料夾開始查詢
find / -name "zwh*" -ls
3) 查詢並刪除
find / -name "zwh*" -ok rm {} \;
find / -name "zwh*" -exec rm {} \;
4) 查詢使用者為hadoop的檔案
find /usr -user hadoop -ls
5) 查詢使用者為hadoop並且(-a)擁有組為root的檔案
find /usr -user hadoop -a -group root -ls
6) 查詢使用者為hadoop或者(-o)擁有組為root並且是資料夾型別的檔案
find /usr -user hadoop -o -group root -a -type d
7) 查詢許可權為777的檔案
find / -perm -777 -type d -ls
8) 顯示命令歷史
history
5.打包和壓縮
1) gzip壓縮
gzip test.txt
2) 解壓
gzip -d test.txt.gz
3) bzip2壓縮
bzip2 test
4) 解壓
bzip2 -d test.bz2
5) 打包並壓縮成bz2
tar -jcvf a.tar.bz2
6) 解壓bz2
tar -jxvf a.tar.bz2
7) 將當前目錄的檔案打包
tar -cvf bak.tar .
8) 解壓
tar -xvf bak.tar
9) 打包並壓縮gzip
tar -zcvf test.tar.gz
10) 解壓縮
tar -zxvf test.tar.gz
11) 解壓到/usr/下
tar -zxvf test.tar.gz -C /usr
12) 檢視壓縮包內容
tar -ztvf test.tar.gz
6.防火牆
Centos7:
1) 檢視防火牆狀態
firewall-cmd --state
2) 停止防火牆
systemctl stop firewalld.service
3) 開啟防火牆
systemctl start firewalld.service
4) 禁止firewall開機啟動
systemctl disable firewalld.service
Centos6:
1) 檢視防火牆狀態
service iptables status
2)停止防火牆
service iptables stop
3)開啟防火牆
service iptables star
4)檢視iptables是否開機啟
chkconfig iptables --list
5)設定iptables開機啟動
chkconfig iptables on
6)設定iptables開機不啟動
chkconfig iptables off