linux 基礎命令總結

1.mkdir 建立目錄 -p 建立多級目錄 mkdir -p /data/test
-m, --mode=模式 設置權限模式(相似chmod),而不是rwxrwxrwx 減umask
-p, --parents 須要時建立目標目錄的上層目錄,但即便這些目錄已存在也不看成錯誤處理
○ [root@wen data]# mkdir test/test{1..3} -p #建立一個目錄再在下面建立幾個目錄
[root@wen data]# tree test
test
├── test1
├── test2
└── test3node


2.ls list 查看目錄文件 ls /etc/目錄
-l (long)長格式,-d 查看目錄
-i inode節點號 , -h 人類可讀python


3.cd cd /etc 切換目錄路徑linux

4.pwd 顯示當前所在目錄shell

5.touch 建立文件,不存在即建立,存在就改變訪問時間戳,atime數據庫

6.echo 打印輸出內容,配合 >(重定向,會清除以前內容),>>(在尾部追加內容) 能夠爲文件追加內容vim

 1 [root@wen 926]# echo {1..9}
 2 1 2 3 4 5 6 7 8 9
 3 ○ [root@wen data]# cat >fade.txt
 4 fade walk
 5 ^C
 6 [root@wen data]# cat fade.txt
 7 fade walk
 8 ○ [root@wen data]# cat >>fade.txt<<efo #efo 能夠是任意字符
 9 > i am studing linux
10 > efo
11 [root@wen data]# cat fade.txt
12 fade walk
13 i am studing linux
14 ○ [root@wen data]# echo mygirl 1>walk.txt 2>&1 #正確,錯誤都輸入到walk.txt
15 [root@wen data]# cat walk.txt
16 mygirl
17 [root@wen data]# ech mygirl 1>walk.txt 2>&1 #正確,錯誤都輸入到walk.txt,等同於 &>1
18 [root@wen data]# cat walk.txt
19 -bash: ech: command not found
20 ○ [root@wen data]# echo mygirl &>>walk.txt #追加
21 [root@wen data]# cat walk.txt
22 -bash: ech: command not found
23 mygirl
24 [root@wen data]# ech mygirl &>>walk.txt
25 [root@wen data]# cat walk.txt
26 -bash: ech: command not found
27 mygirl
28 -bash: ech: command not found
29 ○ [root@wen data]# ech mygirl 1>>walk.txt 2>>walk.txt #重定向前面的數字要緊跟着
30 [root@wen data]# cat walk.txt
31 -bash: ech: command not found
32 mygirl
33 -bash: ech: command not found
34 -bash: ech: command not found
View Code

 

7.cat 查看文件內容 cat fade.txt

  -n 匹配排序,cat /rtc/log -n
[root@wen data]# cat walk.txt -n
1 -bash: ech: command not found
2 mygirl
3 -bash: ech: command not found
4 -bash: ech: command not found

8.vi windows記事本,簡單 vim 複雜編輯器,功能複雜,高亮,自動縮進(寫shell/python腳本用)windows

9.xargs 從標準輸入獲取內容建立和執行命令 -n 數字,分組bash

10.cp copy 拷貝文件或目錄,默認不能拷貝目錄, -r :遞歸,用於複製目錄
-a :至關於-pdr, -p:連同檔案的屬性一塊兒複製過去,而非使用默認屬性
○ [root@wen data]# touch test.txt
[root@wen data]# touch /data/926/test.txt
[root@wen data]# cp /data/test.txt /data/926/test.txt
cp:是否覆蓋"/data/926/test.txt"? y
[root@wen data]# \cp /data/test.txt /data/926/test.txt #\cp,再也不提示覆蓋與否,…\rm ,\mv
[root@wen data]# /bin/cp /data/test.txt /data/926/test.txt #再也不提示覆蓋與否

11.rm remove 刪除目錄和文件 -f(force)強制,-r 遞歸,用於刪除目錄
rm -fr "文件名" 強制刪除目錄不提示,很是危險
強調:刪除命令要慎用,很是危險,刪除前必定要先備份一份服務器

 1 ○ [root@wen data]# touch stu{0..6}
 2 [root@wen data]# find /data -type f -name "stu*" |xargs
 3 /data/stu1 /data/stu4 /data/stu6 /data/stu3 /data/stu2 /data/stu0 /data/stu5
 4 [root@wen data]# find /data -type f -name "stu*" |xargs -n 1
 5 /data/stu1
 6 /data/stu4
 7 /data/stu6
 8 /data/stu3
 9 /data/stu2
10 /data/stu0
11 /data/stu5
12 [root@wen data]# find /data -type f -name "stu*" |xargs -n 2 #分組
13 /data/stu1 /data/stu4
14 /data/stu6 /data/stu3
15 /data/stu2 /data/stu0
16 /data/stu5
17 ○ [root@wen data]# find /data -type f -name "stu*" |xargs rm -f #find找到,管道xargs刪除
18 ○ [root@wen data]# touch stu{0..6}
19 [root@wen data]# find /data -type f -name "stu*" -exec rm {} \; #另外一種刪除方法
20 [root@wen data]# ls
View Code

 

12.mv move 移動文件和目錄網絡

14.find 查找 -type 文件類型(f(file),d(diretory),c(character),b(block),s(socket),l(link))
-name "文件名",-mtime 時間,按修改時間查找,時間數字
+7 7天之前 7 第7天 -7最近7天
15.*grep linux三劍客老三 過濾須要的內容,-v 排除內容
-C #除了顯示匹配行外,顯示該行先後的num行
-B #除了顯示匹配行外,顯示該行以前的num行
-A #除了顯示匹配行外,顯示該行以後的num行 例子查看19-(5)

 1 ○ [root@wen data]# cat fade.txt
 2 fade walk
 3 i am studing linux
 4 [root@wen data]# grep -v fade fade.txt
 5 i am studing linux
 6 [root@wen data]# grep -v f fade.txt
 7 i am studing linux
 8 [root@wen data]# grep -v i fade.txt
 9 fade walk
10 [root@wen data]# grep fade fade.txt
11 fade walk
View Code

 

16.head 頭,頭部 讀取文件的前n行,默認前10行,-n 數字,習慣-5,忽略-n

17.tail 尾巴 輸出文件的後n行,默認後10行,-n 數字,習慣-5,忽略-n

 1 [root@wen data]# seq 20 > num.txt
 2 [root@wen data]# head num.txt
 3 1
 4 2
 5 3
 6 4
 7 5
 8 6
 9 7
10 8
11 9
12 10
13 [root@wen data]# tail num.txt
14 11
15 12
16 13
17 14
18 15
19 16
20 17
21 18
22 19
23 20
24 [root@wen data]# head -3 num.txt
25 1
26 2
27 3
28 [root@wen data]# tail -3 num.txt
29 18
30 19
31 20
View Code

 

18.alias 查看設置別名,unalias取消別名
○ [root@wen data]# alias rm='echo this command does not allow to use'
[root@wen data]# alias|grep rm
alias rm='echo this command does not allow to use'
[root@wen data]# rm
this command does not allow to use
定義別名永久生效: /etc/profile 全局生效 ~/.bashrc 當前用戶生效
分享連接 http://oldboy.blog.51cto.com/2561410/699046

19.seq 序列

 1 [root@wen data]# seq -s '*' 10 #以"*"爲間隔符
 2 1*2*3*4*5*6*7*8*9*10
 3 [root@wen data]# seq 1 2 10 #以1爲起點,2爲間隔,10爲終點
 4 1
 5 3
 6 5
 7 7
 8 9
 9 [root@wen data]# seq 0 2 10
10 0
11 2
12 4
13 6
14 8
15 10
16 * [root@wen data]# seq 100 >ett.txt #查看ett.txt內第20到25行的內容(常見考題)
17 1) [root@wen data]# head -25 ett.txt|tail -6
18 20
19 21
20 22
21 23
22 24
23 25
24 2) [root@wen data]# sed -n '20,25p' ett.txt #更高效的方法
25 20
26 21
27 22
28 23
29 24
30 25
31 3) [root@wen data]# awk '19<NR && NR<26' ett.txt
32 20
33 21
34 22
35 23
36 24
37 25
38 4) [root@wen data]# awk '{if(NR >19 && NR< 26) printf $0 "\n"}' ett.txt
39 20
40 21
41 22
42 23
43 24
44 25
45 5) [root@wen data]# grep 22 -C 2 ett.txt #除了顯示匹配行外,顯示該行先後的num行
46 20
47 21
48 22
49 23
50 24
51 6) [root@wen data]# grep 25 -B 5 ett.txt #除了顯示匹配行外,顯示該行以前的num行
52 20
53 21
54 22
55 23
56 24
57 25
58 7) [root@wen data]# grep 20 -A 5 ett.txt #除了顯示匹配行外,顯示該行以後的num行
59 20
60 21
61 22
62 23
63 24
64 25
View Code

 

20.sed linux三劍客老二,流編輯器,實現對文件的增刪改替換查 s,g常聯合使用,表示對當前進行全局匹配替換
參數 -n 取消默認輸出, -c 容許多項編輯, -I 修改文件內容

 1 [root@wen data]# echo mygirl >>fade.txt
 2 [root@wen data]# sed -i 's#mygirl#jujingyi#g' fade.txt
 3 [root@wen data]# cat fade.txt
 4 fade walk
 5 i am studing linux
 6 jujingyi
 7 Ø [root@wen data]# echo dream-girl{01..04} > /data/girl/del.sh
 8 [root@wen data]# find /data -type f -name '*.sh' |xargs cat
 9 dream-girl01 dream-girl02 dream-girl03 dream-girl04
10 [root@wen data]# find /data -type f -name '*.sh' |xargs sed 's#dream-girl*#jujingyi#g'
11 jujingyi01 jujingyi02 jujingyi03 jujingyi04
12 [root@wen data]# find /data -type f -name '*.sh' |xargs sed 's#.*#jujingyi#g'
13 jujingyi
14 [root@wen data]# find /data -type f -name '*.sh' |xargs sed -i 's#dream*#jujingyi#g'
15 [root@wen data]# find /data -type f -name '*.sh' |xargs cat
16 Ø jujingyi-girl01 jujingyi-girl02 jujingyi-girl03 jujingyi-girl04
17 [root@wen data]# echo dream-girl{01..04} > /data/girl/del.sh
18 [root@wen data]# sed -i 's#dream-*#beautiful#g' `find /data -type f -name '*.sh'` #替換方法二
19 [root@wen data]# find /data -type f -name '*.sh' |xargs cat
20 beautifulgirl01 beautifulgirl02 beautifulgirl03 beautifulgirl04
View Code

 

21.linux系統查看命令幫助的手段
a.man 命令名/配置文件 b.命令 --help (稍微簡單的幫助) c.搜索引擎「linux 命令名」,info d.help 命令名,特殊bash內置命令

22.經常使用快捷鍵
Ctrl +c 終止當前任務命令或程序
Ctrl +d退出當前用戶環境,至關於
Ctrl +l 清屏,至關於clear命令

23.查看系統64位,內核
[root@wen data]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@wen data]# uname -r
2.6.32-573.el6.x86_64
[root@wen data]# uname -m
x86_64

24.tree 查看目錄結構 沒有則安裝 yum -y install tree
tree -L 1 ,查看當前下一層目錄

25.linux 基礎知識
一,分區
一塊硬盤:主分區,擴展分區,邏輯分區
主分區+擴展分區的數量 <= 4,其中一個主分區能夠用一個擴展分區,擴展分區最多隻能有一個
擴展分區不能直接使用,還須要在上面建立邏輯分區,邏輯分區可有多個
主分區 + 擴展分區 編號只能1~4,邏輯分區的編號只能從5開始

1.常規分區:數據不是特別重要的業務(集羣的某個節點)
/boot 引導分區 200M
swap 交換分區 內存的1.5倍,內存大於 8G,就給 8~16G
/ 根分區,全部目錄頂點 剩餘全部空間
2.數據重要(數據庫,存儲服務區)
/boot 引導分區 200M
swap 交換分區 內存的1.5倍,內存大於 8G,就給 8~16G
/ 根分區,全部目錄頂點 100~200G
/data 全部,存放數據
3.特大網站,門戶(產品線特別多,需求)
/boot 引導分區 200M
swap 交換分區 內存的1.5倍,內存大於 8G,就給 8~16G
/ 根分區,全部目錄頂點 100~200G
剩餘空間不分配,哪一個部門領到了服務器,根據需求再進行分區
二,硬盤
▫ 系統的第一塊IDE接口的硬盤稱爲 /dev/had
▫ 系統的第二塊IDE接口的硬盤稱爲 /dev/hdb
▫ 系統的第一塊SCSI接口的硬盤稱爲 /dev/sda
▫ 系統的第二塊SCSI接口的硬盤稱爲 /dev/sdb
價格與性能:SSD>SAS>SATA

三,其餘硬件
1.網站PC服務器
Dell(廣泛)
1u = 4.45cm---->R420,410,620,630
2u--->R730,720,710
2.raid卡及其介紹
詳見linux筆記

26.stat 查看目錄或文件的狀態 display file or file system status

27.檢查網絡服務

 1 ssh服務是否好的 檢測辦法:從哪一個機器連就在那個機器上操做
 2 telnet 192.168.59.131 22(服務器的IP和port)在windows上操做
 3 不通的可能緣由:
 4 a.物理鏈路是否有問題,ping 192.168.59.131 
 5 b.服務器端防火牆阻擋
 6 [root@wen data]# /etc/init.d/iptables stop
 7 iptables:將鏈設置爲政策 ACCEPT:filter [肯定]
 8 iptables:清除防火牆規則: [肯定]
 9 iptables:正在卸載模塊: [肯定]
10 c.端口沒有開放,服務器沒有監聽你鏈接的端口
11 [root@wen data]# netstat -lntup |grep 22 #以ssh服務22端口爲例
12 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1503/sshd 
13 tcp 0 0 :::22 :::* LISTEN 1503/sshd 
14 [root@wen data]# netstat -lntup |grep sshd
15 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1503/sshd 
16 tcp 0 0 :::22 :::* LISTEN 1503/sshd 
17 [root@wen data]# /etc/init.d/sshd restart #重啓ssh服務
18 ○ 網卡配置 剛安裝的linux 網絡服務默認是關閉的,須要手動調整
19 #更改配置文件將ONBOOT=no改爲yes 
20 [root@wen data]# sed -i 's#ONBOOT=no#ONBOOT=yes#g' /etc/sysconfig/network-scripts/ifcfg-eth0 
21 [root@wen data]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
22 DEVICE=eth0
23 TYPE=Ethernet
24 ONBOOT=yes
25 [root@wen data]# service network restart #重啓網絡服務生效
26 ○ 小節:linux客戶端DNS能夠在網卡配置文件裏設置(ifcfg-eth0)
27 linux客戶端DNS也能夠在/etc/resolv.conf裏設置
28 網卡里的設置DNS優先於/etc/resolv.conf,若是重啓網絡網卡的DNS會覆蓋/etc/resolv.conf的設置
29 ○ [root@wen ~]# cat /etc/resolv.conf
30 ; generated by /sbin/dhclient-script
31 search localdomain
32 nameserver 192.168.59.2 #DNS
33 [root@wen ~]# /etc/init.d/network restart #重啓網卡
34 [root@wen ~]# setup "network configuration" "DNS configuration" 就是修改/etc/resolv.conf
View Code

 

28.rz,上傳 sz,下載命令 可執行 yum install lrzsz -y yum groupinstall 或 "Dial-up Networking Soupport" -y 命令來安裝

29.su 切換用戶 su 和 su -的區別

30.linux 命令提示符由PS1 環境變量控制
[root@wen data]# set|grep PS1
PS1='[\u@\h \W]\$ '
[root@wen data]# PS1='[\u@\h \W\t]\$ ' #能夠經過全局變量配置/etc/profile,使其永久生效
[root@wen data01:35:17]# #提示符添加顯示時間

31.克隆機 1).編輯eth0的配置文件:
[root@wen data01:4]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
刪除 HWADDR=00:0c:29:e9:95:dd 和 UUID
2).若是有必要再清空以下文件:
> /etc/udev/rules.d/70-persistent-net.rules
3).最後reboot

相關文章
相關標籤/搜索