Linux基礎和檔案操作
一、基礎知識點
1、安裝centos 7虛擬機器擴充知識點:
DHCP :DHCP(動態主機配置協議)是一個區域網的網路協議。指的是由伺服器控制一段IP地址範圍,客戶機登入伺服器時就可以自動獲得伺服器分配的IP地址和子網掩碼。預設情況下,DHCP作為Windows Server的一個服務元件不會被系統自動安裝,還需要管理員手動安裝並進行必要的配置
DNS: 可以把域名地址轉換成ip地址
主機名:生產環境,要有意義 地址+樓層+房間號+業務
swap分割槽:交換分割槽,也就是虛擬記憶體,當實體記憶體不夠的時候,一些長時間沒有什麼操作的程式,會被轉移到swap中來,釋放出其佔用的實體記憶體
關於分割槽
linux 分割槽結構為樹結構, windows 為森林結構,所以有C盤D盤之類的, 而linux /分割槽就是根分割槽, swap可以理解為虛擬記憶體, /boot 這是個目錄, 可以分成一個分割槽,然後掛載到/boot目錄, 也可以只用一個/分割槽, 裡面會有boot目錄的,儲存的是引導檔案,核心等等的東東
2、Shell提示符
# $ = 普通使用者, # = root使用者(超級管理員) ### [root@l92 ~]# # root:當前登入使用者 # @ :沒有意義 # 192:主機名稱,如果很長會顯示不全,通過hostname檢視 # ~:當前使用者所在家目錄 # #:通常指超級管理員# $:普通使用者 [root@192 ~]# whoami root [root@192 ~]# useradd jack [root@192 ~]# passwd jack Changing password for user jack. New password: BAD PASSWORD: The password is a palindrome Retype new password: passwd: all authentication tokens updated successfully. [root@192 ~]# su jack [root@192 ~]# cd root 提示沒有許可權
3、shell基礎語法
#命令 選項 引數 command [-options] [arguments] # 例子 [root@192 ~]# ls anaconda-ks.cfg [root@192 ~]# ls -a # 命令+選項 . .. anaconda-ks.cfg .bash_logout .bash_profile .bashrc .cshrc .tcshrc [root@192 ~]# ls -a /home/ #命令+選項+引數 . .. nq #選項: 用於調節命令的輸出效果 #以 “-”引導短格式選項(單個字元),例如“-l” #以“--”引導長格式選項(多個字元),例如“--color” #多個短格式選項可以寫在一起,只用一個“-”引導,例如“-al” ''' [root@192 ~]# ls -a -l total 24 dr-xr-x---. 2 root root 114 Dec 30 20:25 . dr-xr-xr-x. 17 root root 224 Dec 30 20:24 .. -rw-------. 1 root root 1465 Dec 30 20:25 anaconda-ks.cfg -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout 等價於ls -la [root@192 ~]# ls -la total 24 dr-xr-x---. 2 root root 114 Dec 30 20:25 . dr-xr-xr-x. 17 root root 224 Dec 30 20:24 .. -rw-------. 1 root root 1465 Dec 30 20:25 anaconda-ks.cfg -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout 等價於 ls -al [root@192 ~]# ls -al total 24 dr-xr-x---. 2 root root 114 Dec 30 20:25 . dr-xr-xr-x. 17 root root 224 Dec 30 20:24 .. -rw-------. 1 root root 1465 Dec 30 20:25 anaconda-ks.cfg -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout ''' #引數: 命令操作的物件,如檔案、目錄名等 # 選項和引數可以出現位置調換,命令,選項,引數之間要至少有一個空格 # 命令必須開頭, 選項和引數位置可以發生變化
二、Bash shell基本特性
1、安裝補全軟體、ifconfig軟體
# ifconfig 在7的版本沒有,需要裝軟體 yum insatll net-tools -y yum insatll net-tools # yum install bash-completion -y 補齊選項 ls - 兩下tab建,所有的選項都會提示出來
2、常用快捷建
Ctrl + a #游標跳轉至正在輸入的命令列的首部 Ctrl + e #游標跳轉至正在輸入的命令列的尾部 Ctrl + c #終止前臺執行的程式 Ctrl + d #在shell中,ctrl-d表示推出當前shell。 Ctrl + z #將任務暫停,掛至後臺 Ctrl + l #清屏,和clear命令等效。 Ctrl + k #刪除從游標到行末的所有字元 Ctrl + u #刪除從游標到行首的所有字元 Ctrl + r #搜尋歷史命令, 利用關鍵字,Tab建選中 Ctrl + w #按單詞或空格進行向前刪除 Ctrl + 左右建 #按單詞或空格進行向前向後跳 #在命令列前加面加 "#" 則該命令不會被執行
3、歷史命令history
''' -w 儲存命令歷史到歷史檔案 -c 清空命令歷史記錄, 不會情況檔案 -d 刪除命令歷史的第 N 條行 !行數 執行第幾行命令 ''' [root@192 ~]# history -d 10 #刪除第10條歷史記錄 [root@192 ~]# history 1 systemctl stop firewalld.service 2 ping www.baidu.com 3 netstat -rn 4 route add -host 192.168.92.3 dev eth0:0 5 ping www.baidu.com 6 ifconfig 7 yum install net-tools -y 8 ifconfig 9 yum install bash-completion 10 history 11 history -d 10 #刪除第10條歷史記錄 12 history [root@192 ~]# !5 #執行第5行命令 [root@192 ~]# history -w #儲存歷史集合,儲存到當前使用者的家目錄 .bash_history [root@192 ~]# history -c #清空歷史命令
三、檔案管理知識點
i、檔案目錄結構:
linux下CentOS7系統目錄結構如下:
1、存放命令的目錄bin、sbin
/bin,普通使用者使用的命令 /bin/ls,/bin/date
/sbin,管理員使用的命令
注意:一般bin路徑下存放的都是可執行檔案
[root@192 sbin]# poweroff #關機 [root@192 sbin]# service #恢復 [root@192 sbin]# useradd username #新增使用者
......
2、存放使用者資料的家目錄 /home和/root
/home普通使用者家目錄,預設是/home/username/
/root 超級管理員root的家目錄,普通使用者無權操作
~表示root家目錄
[root@192 ~]# useradd test1 [root@192 ~]# passwd test1 Changing password for user test1. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@192 ~]# ls /home/ nq test1 [root@192 ~]#
3、系統檔案目錄usr
/usr,相當於C:Windows
/usr/local,軟體安裝的目錄,相當於C:Program
/usr/bin/,普通使用者使用的應用程式(重要)
/usr/sbin,管理員使用的應用程式(重要)
/usr/lib,庫檔案Glibc 32bit
/usr/lib64,庫檔案Glibc 64bit
''' df -h #df -h檢視系統中檔案的使用情況 Size 分割區總容量 Used 已使用的大小 Avail 剩下的大小 Use% 使用的百分比 Mounted on 路徑地址 ''' [root@192 usr]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 980M 0 980M 0% /dev tmpfs 991M 0 991M 0% /dev/shm tmpfs 991M 9.5M 981M 1% /run tmpfs 991M 0 991M 0% /sys/fs/cgroup /dev/sda2 18G 1.3G 17G 8% / /dev/sda1 1020M 132M 889M 13% /boot tmpfs 199M 0 199M 0% /run/user/0 ''' du -sh * 檢視當前目錄下各個檔案及目錄佔用空間大小 du -sh /usr/ ''' [root@192 usr]# du -sh * 60M bin 0 etc 0 games 36K include 695M lib 108M lib64 12M libexec 0 local 39M sbin 207M share 0 src 0 tmp [root@192 usr]# du -sh /usr/ 1.1G /usr/
4、系統啟動目錄 /boot
/boot存放的是系統啟動相關的檔案,比如:grub(引導裝在程式)
[root@192 boot]# ls config-3.10.0-1160.el7.x86_64 initramfs-3.10.0-1160.el7.x86_64.img efi symvers-3.10.0-1160.el7.x86_64.gz grub System.map-3.10.0-1160.el7.x86_64 grub2 vmlinuz-0-rescue-365e7da98b7b497f9affe1f0f8bb5489 initramfs-0-rescue-365e7da98b7b497f9affe1f0f8bb5489.img vmlinuz-3.10.0-1160.el7.x86_64
5、配置檔案目錄 /etc (最重要的)
/etc,極其重要,後續所有服務的配置都在這個目錄中
/etc/sysconfig/network-script/ifcfg-,網路配置檔案,網絡卡配置是ifcfg-ens32
/etc/hostname,系統主機名配置檔案,主機名很重要,有些特殊服務要依賴主機名,沒有主機名會報錯起不來;修改了要重啟:reboot
/etc/resolv.conf,dns客戶端配置檔案,域名解析伺服器,一般我們不配置,因為網絡卡的配置好了,會覆蓋掉它,網絡卡的優先順序高
/etc/hosts,本地域名解析配置檔案,域名解析,先找自己的hosts,再去域名解析
[root@192 network-scripts]# ls #網路配置檔案 ifcfg-ens32 ifdown-ippp ifdown-routes ifup ifup-ipv6 ifup-ppp ifup-tunnel ifcfg-lo ifdown-ipv6 ifdown-sit ifup-aliases ifup-isdn ifup-routes ifup-wireless ifdown ifdown-isdn ifdown-Team ifup-bnep ifup-plip ifup-sit init.ipv6-global ifdown-bnep ifdown-post ifdown-TeamPort ifup-eth ifup-plusb ifup-Team network-functions ifdown-eth ifdown-ppp ifdown-tunnel ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6
6、可變目錄/var和臨時目錄/tmp 、/var/tmp
/var,存放一些變化檔案,比如/var/log/下的日誌檔案,登陸日誌
/var/tmp,程序產生的臨時檔案
/tmp,系統臨時目錄,任何使用者都可以使用
7、裝置目錄檔案/dev
/dev,存放裝置檔案,比如硬碟,硬碟分割槽,光碟機,等等
/dev/sd 硬碟裝置,比如:linux中磁碟檔案叫sd,第一個硬碟叫a,第二個叫b,sda1表示第一個磁碟的第一個分割槽,sdb4:第二個硬碟的第四個分割槽(服務可以插很多硬碟)
/dev/null,黑洞裝置,只進不出。類似於垃圾回收站
/dev/random,生成隨機數的裝置
/dev/zero,能遠遠不斷的產生資料,類似於取款機,隨時隨地取錢
4、dev/zero例子:源源不斷取資料 dd if=/dev/zero of=/opt/test.txt bs=1M count=1024 ''' dd:用指定大小的塊拷貝一個檔案,並在拷貝的同時進行指定的轉換。 if=檔名:輸入檔名,預設為標準輸入。即指定原始檔。< if=input file > of=檔名:輸出檔名,預設為標準輸出。即指定目的檔案。< of=output file > bs=bytes:同時設定讀入/輸出的塊大小為bytes個位元組。 count=blocks:僅拷貝blocks個塊,塊大小等於ibs指定的位元組數。 '''
8、虛擬檔案系統目錄 /proc目錄(對應程序停止,proc對應目錄會被刪除)
/proc,反映系統當前程序的實時狀態 :process
[root@192 dev]# ls /proc 1 1485 233 275 31 446 530 96 execdomains kpagecount partitions timer_stats 10 1486 234 276 32 447 536 acpi fb kpageflags sched_debug tty 1047 1490 235 277 33 448 544 buddyinfo filesystems loadavg schedstat uptime 1052 15 236 278 363 45 558 bus fs locks scsi version 1054 16 24 279 395 451 590 cgroups interrupts mdstat self vmallocinfo 11 17 244 280 4 453 6 cmdline iomem meminfo slabinfo vmstat 1138 18 245 281 41 47 60 consoles ioports misc softirqs zoneinfo 1158 19 246 282 42 499 7 cpuinfo irq modules stat 1162 2 248 283 43 5 8 crypto kallsyms mounts swaps 1229 20 249 284 439 522 885 devices kcore mpt sys 1249 21 251 285 44 523 886 diskstats keys mtrr sysrq-trigger 13 22 255 286 440 528 887 dma key-users net sysvipc 14 23 256 30 444 529 9 driver kmsg pagetypeinfo timer_list
9、其他目錄
/run:系統執行是所需檔案,以前防止在/var/run中,後來拆分成獨立的/run目錄。重啟後重新生成對應的目錄資料
#1 media:提供裝置的掛載點,媒體檔案 # linux 新增了碟符,需要手動掛載 # 把光盤裡的資料,掛載到media目錄 mount /dev/cdrom /media/ # 2 mnt:提供裝置的掛載點(同上) # 3 opt:第三方工具,第三方軟體預設安裝的(mysql...) # run :下有pid,log結尾的檔案 ls /run cat sshd.pid # 程序執行的pid號,放在檔案中 ps aux |grep sshd # .lock檔案的作用,鎖機制 # 假設現在執行 yum install tree # 再開一個視窗執行相同命令 yum install tree ''' Another app is currently holding the yum lock; waiting for it to exit... The other application is: yum Memory : 71 M RSS (470 MB VSZ) Started: Tue Aug 18 00:26:31 2020 - 00:24 ago State : Sleeping, pid: 6191 ''' cat /run/yum.pid
ii、檔案管理操作
1、相對路徑和絕對路徑
相對路徑:是從當前位置開始,比如:.或者 .. 這樣的,一般都是相對路徑
絕對路徑:是從起始位置開始,比如(linux系統),從/開始的路徑,如:/home/nq
# 切換目錄的方法: # cd 絕對路徑 cd /etc/hostname # cd 相對路徑 cd test/abc cd . cd .. # cd #切換目錄,例: cd /etc # cd - #切換回上一次所在的目錄 # cd ~ #切換回當前使用者的家目錄,注意:root和普通使用者是否有所不同嗎? # cd . #代表當前目錄,一般在拷貝、移動等情況下使用 cp /etc/hostname ./ # cd .. #切換回當前目錄的上級目錄
2、檔案(touch)\資料夾(mkdir)建立、複製、移動、刪除
# 建立目錄 # 選項:-v 顯示詳細資訊 -p 遞迴建立目錄 # mkdir dir1 # mkdir /home/od/dir1 /home/od/dir2 # mkdir -v /home/od/{dir3,dir4} # mkdir -pv /home/od/dir5/dir6 # mkdir -pv /home/{od/{diu,but},boy} # 建立檔案 # touch file #無則建立,有則修改時間 # touch file2 file3 # touch /home/od/file4 file5 # touch file{a,b,c} #{}集合,等價 touch a b c # touch file{1..10} # touch file{a..z
# 複製: # cp file /tmp/file_copy # cp name /tmp/name #不修改名稱 # cp file /tmp/ #不修改名稱 # cp -p file /tmp/file_p #-p保持原檔案或目錄的屬性 # cp -r /etc/ /tmp/ #複製目錄需要使用-r引數, 遞迴複製 # cp -rv /etc/hosts /etc/hostname /tmp #拷貝多個檔案至一個目錄 # cp -rv /etc/{hosts,hosts.bak} # cp -rv /etc/hosts{,-org} #複製 hello檔案到test2資料夾下 [root@192 test1]# cp hello test2 [root@192 test1]# ls test2 hello # 複製 test2資料夾到test3資料夾下 [root@192 test1]# cp /test2 test3 [root@192 test1]# ls test3 test2
移動
# mv file file1 #原地移動算改名 # mv file1 /tmp/ #移動檔案至tmp目錄 # mv /tmp/file1 ./ #移動tmp目錄的檔案至當前目錄 # mv dir/ /tmp/ #移動目錄至/tmp目錄下 # touch file{1..3} # mv file1 file2 file3 /opt/ #移動多個檔案或至同一個目錄 # mkdir dir{1..3} # mv dir1/ dir2/ dir3/ /opt #移動多個目錄至同一個目錄 [root@192 test1]# mv hello hello1 [root@192 test1]# ls hello1 test2 test3 {test3} world [root@192 test1]# ls test3 test2 [root@192 test1]# mv ./test3/test2 ./ [root@192 test1]# ls hello test2 test3 {test3} world [root@192 test1]# ls test3 [root@192 test1]#
刪除
#選項:-r: 遞迴 -f: 強制刪除 -v: 詳細過程 # rm file.txt #刪除檔案, 預設rm存在alias別名,rm -i所以會提醒是否刪除檔案 # rm -f file.txt #刪除檔案, 不提醒 # rm -r dir/ #遞迴刪除目錄,會提示 dir是目錄路徑 # rm -rf dir/ #強制刪除目錄,不提醒(慎用) [root@192 test1]# rm hello1 rm: remove regular empty file ‘hello1’? y [root@192 test1]# rm -f world [root@192 test1]# ls test2 test3 {test3} # 刪除當前路徑下的test2資料夾 [root@192 test1]# rm ./test2 rm: remove regular empty file ‘./test2’? y [root@192 test1]# ls test3 {test3}
3、檢視檔案檔案內容命令
# cat 命令 [root@192 test1]# cat test.txt #正常檢視檔案的方式 hello world thank bye [root@192 test1]# cat -n test.txt #帶行數的檢視檔案方式 1 hello world 2 thank 3 bye [root@192 test1]# cat -A test.txt #檢視檔案中特殊符合,像tab鍵之類 hello world$ thank$ bye$ [root@192 test1]# tac test.txt #倒序檢視檔案 bye thank hello world [root@192 test1]# cat >> test1.txt <<EOF #管道輸入 > adc > cdf > EOF [root@192 test1]# cat test1.txt adc cdf ''' less、more 命令 ''' # less /etc/services #使用游標上下翻動,空格進行翻頁,q退出 # more /etc/services #使用回車上下翻動,空格進行翻頁,q退出(有百分比) ''' # ----------------head 命令------------------ head filename 預設是前十行 head -n5 filename #檢視前5行,(n可加可不加) 還可以應用在檢視程序上 ps aux | head -3
#----------------tail 命令------------------- 跟head一樣,只不過tail是檢視尾部 # tail test.txt # 檢視檔案尾部,預設10行 # tail -20 test.txt # 檢視檔案尾部20行 # tail -f test.txt #-f動態檢視檔案尾部的變化 # tailf test.txt #檢視檔案尾部的變化 ''' [root@192 test1]# head -n2 test.txt # 檢視檔案前3行 hello world thank [root@192 test1]# ps aux|head -2 # 檢視前三個程序 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 127904 6548 ? Ss 20:26 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 ''' # ----------------------grep檔案內容過濾------------------ grep "^root" test.txt #匹配以root開頭的行 grep "bash$" test.txt #匹配以bash結尾的行 grep -i "ftp" test.txt #忽略大小寫匹配 grep -Ei "sync$|ftp" test.txt #匹配檔案中包含sync結尾或ftp字串 grep -n -A 2 "Failed" test.txt #匹配test檔案中Failed字串,並列印它的下2行 grep -n -B 2 "Failed" test.txt #匹配test檔案中Failed字串,並列印它的上2行 grep -n -C 2 "Failed" test.txt #匹配test檔案中Failed字串,並列印它的上下2行 ''' [root@192 test1]# grep "^hello" test.txt #匹配hello 開頭的的內容 hello world [root@192 test1]# grep "you$" test.txt #匹配you 結尾的的內容 hao are you [root@192 test1]# grep -i "ftp" /etc/passwd # -i 表示忽略大小寫 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@192 test1]# grep -EI "sync$|ftp" /etc/passwd #匹配檔案中包含sync結尾或ftp字串 sync:x:5:0:sync:/sbin:/bin/sync ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@192 test1]# grep -n -A 1 "yes" test.txt #檢視yes下的一行 7:yes 8-saglj [root@192 test1]# grep -n -B 1 "yes" test.txt #檢視yes上的一行 6-good bye 7:yes [root@192 test1]# grep -n -C 1 "yes" test.txt #檢視yes上下一行 6-good bye 7:yes 8-saglj # 上翻,下翻 control+b:下翻 control+f:上翻
4、wget、curl和檔案上傳與下載(rz、sz)
wget 檔案url #下載檔案到當前目錄 wget -O 儲存路徑 檔案url #下載檔案到指定的路徑 #curl是一款很強大的http命令列工具 ''' 語法:curl [引數] [url] -A/--user-agent <string> 設定使用者代理髮送給伺服器 -b/--cookie <name=string/file> cookie字串或檔案讀取位置 -c/--cookie-jar <file> 操作結束後把cookie寫入到這個檔案中 -C/--continue-at <offset> 斷點續轉 -D/--dump-header <file> 把header資訊寫入到該檔案中 -e/--referer 來源網址 -f/--fail 連線失敗時不顯示http錯誤 -o/--output 把輸出寫到該檔案中 -O/--remote-name 把輸出寫到該檔案中,保留遠端檔案的檔名 -r/--range <range> 檢索來自HTTP/1.1或FTP伺服器位元組範圍 -s/--silent 靜音模式。不輸出任何東西 -T/--upload-file <file> 上傳檔案 -u/--user <user[:password]> 設定伺服器的使用者和密碼 -w/--write-out [format] 什麼輸出完成後 -x/--proxy <host[:port]> 在給定的埠上使用HTTP代理 -#/--progress-bar 進度條顯示當前的傳送狀態 ''' curl url 檔案瀏覽,比如:curl http://www.baidu.com #瀏覽百度網頁 curl -O 儲存路徑 url 將url的內容下載到指定的路徑
# yum install lrzsz # 本地檔案上傳到伺服器 rz filename # 伺服器檔案下載到本子 sz 檔案路徑
5、命令查詢
#檔案查詢命令:locate、which、whereis、find locate /etc/sh #搜尋etc目錄下所有以sh開頭的檔案 locate -i /etc/sh #搜尋etc目錄下,所有以sh開頭的檔案,忽略大小寫 which ls #查詢ls命令的絕對路徑 whereis ls #查詢命令的路徑、幫助手冊、等 whereis -b ls #僅顯示命令所在的路徑,僅看二進位制 # 對於核心相關的一些命令,用which whereis 是無法查詢到的,需要使用type採查詢 type -a ls #檢視命令的絕對路徑(包括別名) type -a for
6、字元處理命令(sort、uniq、cut、sed、awk、wc)
''' sort [OPTION]... [FILE]... -r:倒序 -n:按數字排序 -t:指定分隔符(預設空格) -k:指定第幾列, 指定幾列幾字符(指定1,1 3.1,3.3) ''' [root@192 ~]# sort -t ":" -n -k2 file.txt #按照數字排列 d:1 c:2 # uniq:去重,刪除重複的行,並統計出重複的行數,必須是緊挨著的才可以刪除(必須配置sord) sort file.txt | uniq #去重 sort file.txt | uniq -c # 去重,並計算出重複的次數 #cut 截區欄位 cut -d " " -f2,5 filename #把字串按“ ” 分割,取出第2和第5個 ''' wc統計行數 #wc [OPTION]... [FILE]... #選項:-l顯示檔案行數 -c顯示檔案位元組 -w顯示檔案單詞 ''' wc -l filename #統計檔案有多少行 ls | wc -l # 統計當前路徑下有多少檔案和資料夾 ps aux |grep ssh | wc -l # 統計有多少個ssh相關的程序
7、檔案屬性及型別
-rw-------. 1 root root 4434 May 30 13:58 ks.cfg # -rw-------. ①:第一個字元是檔案型別,其他則是許可權 1 ②:硬連結次數 root ③:檔案屬於哪個使用者 root ④:檔案屬於哪個組 4434 ⑤:檔案大小 May30 13:58 ⑥⑦⑧:最新修改的時間與日期 ks.cfg ⑨:檔案或目錄名稱 #檔案型別: - #普通檔案(文字, 二進位制, 壓縮, 圖片, 日誌等) d #目錄檔案 b #裝置檔案(塊裝置)儲存裝置硬碟 /dev/sda1, /dev/sda2 c #裝置檔案(字元裝置),終端 /dev/tty1, /dev/zero s #套接字檔案, 程序間通訊(socket) p #管道檔案 l #連結檔案 # 檢視檔案型別 file filename