­

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