1. Linux簡(jiǎn)介與安裝
1.1 Linux的基本介紹
- 操作系統(tǒng)
Unix→Max OS、Linux或(GNU/Linux)
MS-DOS→Windows -
Linux無處不在
大部分網(wǎng)站的服務(wù)器都是Linux系統(tǒng)
Android系統(tǒng)的底層用的是修改過的Linux內(nèi)核
全球500臺(tái)最快的超級(jí)計(jì)算機(jī)中,80%采用Linux系統(tǒng) -
Linux特點(diǎn)
免費(fèi)、更新頻繁 - 不同
Linux發(fā)行版
RedHat:性能穩(wěn)定,老牌的Linux發(fā)行版。收費(fèi)的是RHEL(企業(yè)版)。
Fedora:RedHat的社區(qū)免費(fèi)后繼版。
CentOS:可以算是RHEL的克隆版,免費(fèi)。
Mandriva:最早Mandrake的開發(fā)者是基于RedHat進(jìn)行開發(fā)的。
SUSE:德國最著名的Linux發(fā)行版。
Debian:迄今為止,最遵循GNU規(guī)范的Linux系統(tǒng)。
Ubuntu:Debian的后繼或一個(gè)分支。
Gentoo:高度的自定制性。
Slackware:歷史最悠久的Linux發(fā)行版,創(chuàng)建于1992年。
Deepin:中國發(fā)行。對(duì)優(yōu)秀的開源產(chǎn)品進(jìn)行集成和配置,開發(fā)軟件。
總結(jié)如下:
Linux→Mandriva、Red Hat、Debian、Slackware
Debian→Knoppix、Ubuntu、Skolelinux -
Ubuntu
簡(jiǎn)便易用、更新頻繁、社區(qū)完善、標(biāo)準(zhǔn)化
有多個(gè)版本:桌面版、服務(wù)器版、嵌入式版、云版
官網(wǎng)、中文官網(wǎng)、優(yōu)麒麟 -
Linux主流桌面管理器
Gnome、KDE、Unity、XFCE
1.2 Ubuntu系統(tǒng)的安裝
- 華為云安裝
Ubuntu系統(tǒng)
系統(tǒng)版本Ubuntu 18.04.4 LTS
密碼Aa299*** - 第一次
root登錄
命令行輸入:ssh root@121.37.170.230
密碼:Aa299***
報(bào)錯(cuò):WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! Host key verification failed.
由于更換了服務(wù)器的操作系統(tǒng),導(dǎo)致之前配置的SSH免密登錄失效。
解決:ssh-keygen -R 121.37.170.230
重新登錄:ssh root@121.37.170.230 -
XFCE介紹
XFCE是一個(gè)精簡(jiǎn)的輕量化桌面,與Gnome和KDE相比更小巧,并且界面美觀、對(duì)用戶友好,適合云服務(wù)器遠(yuǎn)程連接場(chǎng)景下使用。
資料:Linux的桌面環(huán)境gnome、kde、xfce、lxde 等等使用比較 - 安裝
XFCE桌面并使用VNC Viewer連接
(1) 安裝VNC Server
遠(yuǎn)程登錄云服務(wù)器
ssh root@121.37.170.230
更新軟件列表
sudo apt update
安裝Xfce桌面環(huán)境。
sudo apt install xfce4 xfce4-goodies
安裝TightVNC服務(wù)器
sudo apt install tightvncserver
初始化VNC Server
vncserver
輸入密碼、驗(yàn)證密碼、輸入n
(2) 配置VNC Server
停止第一個(gè)虛擬桌面
vncserver -kill :1
修改xstartup文件
vim ~/.vnc/xstartup
按i進(jìn)入編輯模式,添加以下內(nèi)容
#!/bin/sh
xrdb $HOME/.Xresources
startxfce4 &
配置可執(zhí)行
sudo chmod +x ~/.vnc/xstartup
重啟VNC Server
vncserver
回顯信息中出現(xiàn)類似這樣的一條信息:Log file is /root/.vnc/xxx:1.log。其中,1表示當(dāng)前用戶分配的是VNC的第一個(gè)虛擬桌面。
(3) 云服務(wù)器控制臺(tái)配置
更改安全組規(guī)則,放行5901端口。
(4) 使用VNC Viewer連接云服務(wù)器
在本地PC打開VNC Viewer客戶端,輸入云服務(wù)器的彈性公網(wǎng)IP:5901,設(shè)置名稱,單擊OK。
注意:① 服務(wù)器重啟后,需要重新執(zhí)行vncserver重啟VNC Server。②xxx:1.log日志中的1與5901端口中的1對(duì)應(yīng)。 ③XFCE桌面配置重置命令rm -r ~/.config/xfce4④ 如果本地通過VNC Viewer連接時(shí)報(bào)錯(cuò)Too many authentication failures,則需要重啟vncserver。即:sudo vncserver -kill :1和sudo vncserver :1
資料: 使用VNC Viewer連接Linux云服務(wù)器 - 安裝
Gnome桌面(可選)
在Ubuntu Server 18.04上安裝GNOME桌面
2. Linux中基本命令的使用
2.1 終端的基本操作
- 終端介紹
有些操作使用終端更方便快捷。
Linux和Unix終端命令大部分類似。
自從20世紀(jì)6,70年代開始,終端基本沒變。一次學(xué)習(xí),終身受用。
mac OS系統(tǒng)使用的是Unix終端。 - 命令行
Command Line提示符拆解
root@ecs-x-large-2-linux-20200315202744:~#
root: 當(dāng)前用戶名
ecs-x-large-2-linux-20200315202744:主機(jī)名。
~:當(dāng)前用戶的家目錄
#:用戶權(quán)限,其中$表示普通用戶,#表示超級(jí)管理員。
// 切換到超級(jí)管理員
root@ecs-x-large-2-linux-20200315202744:~# sudo su
//查看用戶
root@ecs-x-large-2-linux-20200315202744:~# whoami
root
//查看主機(jī)
root@ecs-x-large-2-linux-20200315202744:~# hostname
ecs-x-large-2-linux-20200315202744
- 命令介紹
(1) 格式
command parameters
命令區(qū)分大小寫。
(2) 短參數(shù)
command -p、command -p -a、command -pa、command -p 10
一個(gè)橫線、短參數(shù)可以合并、通過空格賦值。
(3) 長(zhǎng)參數(shù)
command --para、command --para1 --para2、command --para=10
兩個(gè)橫線、長(zhǎng)參數(shù)不可以合并、通過等號(hào)賦值。
(4) 其他參數(shù)
有些參數(shù)的形式?jīng)]有絕對(duì)的準(zhǔn)則。例如:ls 路徑名
(5) 命令補(bǔ)全
按一次Tab鍵補(bǔ)全命令或路徑。
按兩次Tab鍵列出所有相關(guān)命令。
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Documents Downloads Music Pictures Public Templates Videos
root@ecs-x-large-2-linux-20200315202744:~# ls -a
. .bash_history .dbus .ssh Desktop Pictures
root@ecs-x-large-2-linux-20200315202744:~# ls --all
. .bash_history .dbus .ssh Desktop Pictures
root@ecs-x-large-2-linux-20200315202744:~# ls Desktop
root@ecs-x-large-2-linux-20200315202744:~# date
Fri Jul 31 08:30:38 CST 2020
root@ecs-x-large-2-linux-20200315202744:~# c
Display all 103 possibilities? (y or n)
c++ clear_console
c++filt cloud-init
c89 cloud-init-per
c89-gcc cmp
root@ecs-x-large-2-linux-20200315202744:~# logout
Connection to 121.37.170.230 closed.
? ~
注意:①按兩次Tab列出所有相關(guān)命令。如果相關(guān)命令太多時(shí),則只顯示第一頁。按空格顯示下一頁,按回車顯示下一行,按Q退出顯示。 ②logout關(guān)閉服務(wù)器連接。
- 查看命令歷史記錄
上下查找:↑、↓
搜索查找:Ctrl + R
(reverse-i-search)d': date
列出所有:history - 快捷鍵
清屏:Ctrl + L或者clear
Ctrl + D:退出連接或關(guān)閉命令行:
Ctrl + A:光標(biāo)跳到一行命令起始位置
Ctrl + E:光標(biāo)跳到一行命令結(jié)束位置
Ctrl + U:刪除在光標(biāo)左側(cè)的所有命令字符
Ctrl + K:刪除在光標(biāo)右側(cè)的所有命令字符
Ctrl + W:刪除在光標(biāo)左側(cè)的一個(gè)單詞
Ctrl + Y:粘貼用Ctrl + U、Ctrl + K或Ctrl + W刪除的字符串。
注釋:Ctrl + L和clear相當(dāng)于滾動(dòng)到新的一屏。而macOS中的command + K才是真正的清屏。
2.2 Linux下的文件操作
2.2.1 Linux文件系統(tǒng)
-
Linux中的文件目錄組織形式
Linux有且只有一個(gè)根目錄,就是/。
Linux用/來表示目錄的層級(jí)與包含關(guān)系。
Linux的目錄形式為:/usr/bin。
bin是usr目錄的子目錄,usr是/根目錄的子目錄。 -
/根目錄的直屬子目錄介紹
(1)bin
binary二進(jìn)制文件,包含用戶的所有可執(zhí)行程序。
(2)boot
boot啟動(dòng),包含與Linux啟動(dòng)密切相關(guān)的文件。
(3)etc
包含系統(tǒng)的配置文件。
(4)home
用戶的私人目錄。在這個(gè)目錄中,我們放置私人的文件。
Linux中的每個(gè)用戶(除了root)都在home目錄中有一個(gè)私人目錄(家目錄)。
例如:用戶名是nmw,那么我的私人目錄(家目錄)就是/home/nmw。
(5)lib
library“庫”,包含被程序所調(diào)用的庫文件。
例如:.so結(jié)尾的文件。windows中這樣的庫文件是以.dll結(jié)尾的。
(6)media
媒體,當(dāng)可移動(dòng)的外設(shè)(例如USB盤、SD卡、DVD、光盤等)插入電腦時(shí),Linux可以讓我們通過media的子目錄訪問這些外設(shè)的內(nèi)容。
(7)mnt
mount掛載,類似于media,但一般用于臨時(shí)掛載一些裝置。
(8)opt
optional application software package“可選的應(yīng)用軟件包”。
用于安裝多數(shù)第三方軟件和插件。
(9)root
根,超級(jí)管理員的root的家目錄。
一般用戶的家目錄位于/home下,root用戶例外。
(10)sbin
system binary系統(tǒng)二進(jìn)制文件,比bin多了個(gè)system。
包含的是系統(tǒng)級(jí)的重要可執(zhí)行程序。
(11)srv
service服務(wù),包含一些網(wǎng)絡(luò)服務(wù)啟動(dòng)后所需要取用到的數(shù)據(jù)。
(12)tmp
temporary臨時(shí),普通用戶和程序存放臨時(shí)文件的地方。
(13)usr
Unix Software Resource表示Unix操作系統(tǒng)軟件資源(歷史遺留命名)。
該目錄是最龐大的目錄之一,安裝了大部分用戶要調(diào)用的程序。
類似于Windows中的C:\Windows和C:\Program Files這兩個(gè)文件夾的集合。
(14)var
包含程序的數(shù)據(jù),例如log日志文件。
root@ecs-x-large-2-linux-20200315202744:~# pwd
/root
root@ecs-x-large-2-linux-20200315202744:~# cd /
root@ecs-x-large-2-linux-20200315202744:/# ls
CloudResetPwdUpdateAgent initrd.img.old sbin
CloudrResetPwdAgent lib snap
HostGuardAgent_Linux64_V1.12.50.deb.sha256 lib64 srv
bin lost+found swapfile
boot media sys
dev mnt tmp
etc opt usr
home proc var
hostguard_setup_config.dat root vmlinuz
initrd.img run vmlinuz.old
root@ecs-x-large-2-linux-20200315202744:/# cd root
root@ecs-x-large-2-linux-20200315202744:~#
注意:① pwd命令可以打印當(dāng)前所在的絕對(duì)目錄結(jié)構(gòu)。② 以上目錄列表形式不僅限于Linux系統(tǒng),類Unix系統(tǒng)類似。
? ~ cd /
? / ls
Applications Users cores home sbin var
Library Volumes dev opt tmp
System bin etc private usr
? / cd ~
? ~ pwd
/Users/nimengwei
? ~
-
Linux文件系統(tǒng)結(jié)構(gòu)圖
/:bin、dev、home、lib、root、var
home:drew、nmw -
witch命令
獲取命令的可執(zhí)行文件的位置。
root@ecs-x-large-2-linux-20200315202744:~# pwd
/root
root@ecs-x-large-2-linux-20200315202744:~# which pwd
/bin/pwd
root@ecs-x-large-2-linux-20200315202744:~# which which
/usr/bin/which
注意:Windows命令中可執(zhí)行程序一般以.exe為后綴,而Linux中一般沒有后綴名。
-
ls命令
(1)--color=auto/none
開啟/關(guān)閉顏色,默認(rèn)開啟。
其中:藍(lán)色→目錄;綠色→可執(zhí)行文件;綠色→壓縮文件;淺藍(lán)色→鏈接文件;灰色→其他文件。
(2)-a
a是all,顯示隱藏的文件。
以.開頭的文件是隱藏文件。
.表示當(dāng)前目錄。
..表示上一級(jí)目錄。
(3)-A
與-a的區(qū)別就是不列出.和..兩個(gè)文件。
(4)-l
列出文件和目錄詳細(xì)信息的列表。
(5)-h
h是humain readable,表示適合人類閱讀的。
(6)-t
按照文件最后一次修改時(shí)間排序。
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Documents Downloads Music Pictures Public Templates Videos
root@ecs-x-large-2-linux-20200315202744:~# ls -a
. .bash_history .dbus .ssh Desktop Pictures
.. .bashrc .gnupg .viminfo Documents Public
root@ecs-x-large-2-linux-20200315202744:~# ls .
Desktop Documents Downloads Music Pictures Public Templates Videos
root@ecs-x-large-2-linux-20200315202744:~# ls ..
bin lost+found swapfile
boot media sys
initrd.img run vmlinuz.old
root@ecs-x-large-2-linux-20200315202744:~# ls -l
total 32
drwxr-xr-x 2 root root 4096 Jul 30 09:49 Desktop
drwxr-xr-x 2 root root 4096 Jul 30 09:49 Documents
drwxr-xr-x 2 root root 4096 Jul 30 09:49 Downloads
root@ecs-x-large-2-linux-20200315202744:~# ls -lh
total 32K
drwxr-xr-x 2 root root 4.0K Jul 30 09:49 Desktop
drwxr-xr-x 2 root root 4.0K Jul 30 09:49 Documents
drwxr-xr-x 2 root root 4.0K Jul 30 09:49 Downloads
root@ecs-x-large-2-linux-20200315202744:~# ls -lath
total 128K
-rw-r--r-- 1 root root 1.5K Aug 1 00:45 .bash_history
-rw------- 1 root root 35K Jul 31 21:15 .xsession-errors
drwx------ 17 root root 4.0K Jul 31 20:08 .
-rw------- 1 root root 1.3K Jul 31 20:08 .ICEauthority
注釋::① total 32:該層文件/文件夾大小之和。
② drwxr-xr-x 2 root root 4096 Jul 30 09:49 Desktop:drwxr-xr-x文件權(quán)限,2對(duì)于目錄表示目錄中文件個(gè)數(shù),對(duì)于文件則表示鏈接數(shù)目,root文件所有者名稱,root文件所在的群組,4096文件大小,Jul 30 09:49最近修改的時(shí)間,Desktop文件/目錄名。
-
cd命令
可以使用相對(duì)路徑和絕對(duì)路徑(/開頭)。
回到家目錄:cd ~或者cd
root@ecs-x-large-2-linux-20200315202744:~# cd /
root@ecs-x-large-2-linux-20200315202744:/# cd usr
root@ecs-x-large-2-linux-20200315202744:/usr# pwd
/usr
root@ecs-x-large-2-linux-20200315202744:/usr# cd ..
root@ecs-x-large-2-linux-20200315202744:/# cd usr/games
root@ecs-x-large-2-linux-20200315202744:/usr/games# cd ../..
root@ecs-x-large-2-linux-20200315202744:/# cd ~
root@ecs-x-large-2-linux-20200315202744:~# cd /usr/games
root@ecs-x-large-2-linux-20200315202744:/usr/games# cd ~
root@ecs-x-large-2-linux-20200315202744:~# cd ../usr/games
root@ecs-x-large-2-linux-20200315202744:/usr/games#
注意:兩次tab可以自動(dòng)補(bǔ)全命令。
-
du命令
du是disk usage的縮寫,表示磁盤使用/占用。
可以查看文件和目錄所占的空間大小。
(1)-h
表示適合人類閱讀的。
(2)-a
默認(rèn)du目錄只顯示目錄大小。加上-a,則會(huì)顯示目錄和文件的大小。
(3)-s
只顯示目錄總大小,不顯示目錄和文件列表。
root@ecs-x-large-2-linux-20200315202744:~# du -ah
4.0K ./Pictures
4.0K ./.viminfo
4.0K ./.dbus/session-bus/90234ee485824633a7d7fa2e6f70265b-1
8.0K ./.dbus/session-bus
12K ./.dbus
.....
644K .
root@ecs-x-large-2-linux-20200315202744:~# du -sh
644K .
注意:ls -l只會(huì)統(tǒng)計(jì)文件或目錄本身的大小。du命令會(huì)遍歷目錄內(nèi)部的目錄和文件,統(tǒng)計(jì)所有文件大小。
2.2.2 Linux文件操作
- 顯示文件內(nèi)容
(1)cat命令
一次性顯示文件所有內(nèi)容。
-n參數(shù)表示顯示行號(hào)。
(2)less命令
分頁顯示文件內(nèi)容。
顯示內(nèi)容之后的快捷鍵如下:
①q-中止讀取文件 ② 空格-讀取下一屏內(nèi)容 ③ 回車-讀取下一行
④d- 前進(jìn)半個(gè)頁面 ⑤u-后退半頁 ⑥b或者↓-后退一頁
⑦y或者↑-后退一行 ⑧=-顯示你在文件中的位置 ⑨h- 顯示幫助文檔
⑩/- 進(jìn)入搜索模式。n-跳轉(zhuǎn)到下一個(gè)搜索結(jié)果。N-跳轉(zhuǎn)到上一個(gè)搜索結(jié)果。
root@ecs-x-large-2-linux-20200315202744:/var/log# less syslog
root@ecs-x-large-2-linux-20200315202744:/var/log# cat syslog
Aug 1 06:25:01 ecs-x-large-2-linux-20200315202744 rsyslogd: [origin software="rsyslogd" swVersion="8.32.0" x-pid="685" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
...
- 顯示文件的首位
(1)head命令
顯示文件的開頭幾行
-n- 顯示幾行,默認(rèn)顯示10行。
(2)tail命令
顯示文件的結(jié)尾幾行
①-n- 顯示幾行,默認(rèn)顯示10行。
②-f- 實(shí)時(shí)追蹤文件的更新。
默認(rèn)每過1s檢查一次文件是否有更新。
可以用-s參數(shù)指定間隔檢查的秒數(shù),例如:tail -f -s 4 syslog
Ctrl + c終止查看。
root@ecs-x-large-2-linux-20200315202744:/var/log# head -n 1 syslog
Aug 1 06:25:01 ecs-x-large-2-linux-20200315202744 rsyslogd: [origin software="rsyslogd" swVersion="8.32.0" x-pid="685" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
root@ecs-x-large-2-linux-20200315202744:/var/log# tail -n 2 syslog
Aug 1 09:42:54 ecs-x-large-2-linux-20200315202744 systemd[1]: Started Session 26 of user root.
Aug 1 09:47:00 ecs-x-large-2-linux-20200315202744 systemd[1]: Started Session 27 of user root.
root@ecs-x-large-2-linux-20200315202744:/var/log# tail -f syslog
Aug 1 09:05:10 ecs-x-large-2-linux-20200315202744 org.freedesktop.thumbnails.Cache1[1764]: Registered thumbailer /usr/bin/gdk-pixbuf-thumbnailer -s %s %u %o
Aug 1 09:05:10 ecs-x-large-2-linux-20200315202744 org.freedesktop.thumbnails.Cache1[1764]: Registered thumbailer /usr/bin/gdk-pixbuf-thumbnailer -s %s %u %o
Aug 1 09:05:10 ecs-x-large-2-linux-20200315202744 dbus-daemon[1764]: [session uid=0 pid=1764] Successfully activated service 'org.freedesktop.thumbnails.Cache1'
...
^C
注意:Ctrl + c可以終止大部分終端命令。
-
touch命令
創(chuàng)建一個(gè)空白文件。
該命令的設(shè)計(jì)初衷是修改文件的時(shí)間戳。如果文件不存在,就會(huì)新建一個(gè)新的。
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Documents Downloads Music Pictures Public Templates Videos
root@ecs-x-large-2-linux-20200315202744:~# touch new_file
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Downloads Pictures Templates new_file
Documents Music Public Videos
root@ecs-x-large-2-linux-20200315202744:~# ls -l
total 32
drwxr-xr-x 2 root root 4096 Jul 30 09:49 Public
drwxr-xr-x 2 root root 4096 Jul 30 09:49 Templates
drwxr-xr-x 2 root root 4096 Jul 30 09:49 Videos
-rw-r--r-- 1 root root 0 Aug 1 10:00 new_file
...
root@ecs-x-large-2-linux-20200315202744:~# touch new_file2 new_file3
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Downloads Pictures Templates new_file new_file3
Documents Music Public Videos new_file2
-
mkdir命令
創(chuàng)建一個(gè)目錄。
(1) 創(chuàng)建單層目錄
mkdir 目錄
(2) 創(chuàng)建多層目錄
mkdir -p 目錄1/目錄2/目錄3
root@ecs-x-large-2-linux-20200315202744:~# mkdir new_folder
root@ecs-x-large-2-linux-20200315202744:~# mkdir new_folder2 new_folder3
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Downloads Pictures Templates new_file new_file3 new_folder2
Documents Music Public Videos new_file2 new_folder new_folder3
root@ecs-x-large-2-linux-20200315202744:~# mkdir -p dir1/dir2/dir3
root@ecs-x-large-2-linux-20200315202744:~# cd dir1/dir2
root@ecs-x-large-2-linux-20200315202744:~/dir1/dir2# ls
dir3
-
cp命令
拷貝文件或目錄。
(1) 拷貝文件
cp 文件 備份文件:拷貝文件到當(dāng)前目錄為一個(gè)新的文件名。
cp 文件 目錄:拷貝文件到其他目錄。
cp 文件 目錄/備份文件:拷貝文件到其他目錄為一個(gè)新的文件名。
root@ecs-x-large-2-linux-20200315202744:~# cp new_file new_file_copy
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Music Templates new_file new_file_copy new_folder3
Documents Pictures Videos new_file2 new_folder
Downloads Public dir1 new_file3 new_folder2
root@ecs-x-large-2-linux-20200315202744:~# cp new_file new_folder
root@ecs-x-large-2-linux-20200315202744:~# cd new_folder
root@ecs-x-large-2-linux-20200315202744:~/new_folder# ls
new_file
root@ecs-x-large-2-linux-20200315202744:~/new_folder# cd ..
root@ecs-x-large-2-linux-20200315202744:~# cp new_file new_folder/new_file_copy
root@ecs-x-large-2-linux-20200315202744:~# cd new_folder
root@ecs-x-large-2-linux-20200315202744:~/new_folder# ls
new_file new_file_copy
(2) 拷貝目錄
添加-r參數(shù),表示recursive遞歸拷貝。
目錄中的所有內(nèi)容(子目錄和文件)都會(huì)被拷貝。
root@ecs-x-large-2-linux-20200315202744:~# cp -r new_folder new_folder_copy
root@ecs-x-large-2-linux-20200315202744:~# cd new_folder_copy
root@ecs-x-large-2-linux-20200315202744:~/new_folder_copy# ls
new_file new_file_copy
root@ecs-x-large-2-linux-20200315202744:~/new_folder_copy#
- 使用通配符
*
例如:cp *.txt folder把當(dāng)前目錄下所有txt文件拷貝到folder目錄中。
cp ha* folder:把當(dāng)前目錄以ha開頭的文件都拷貝到folder目錄中。
root@ecs-x-large-2-linux-20200315202744:~# cp new_file* new_folder2
root@ecs-x-large-2-linux-20200315202744:~# cd new_folder2
root@ecs-x-large-2-linux-20200315202744:~/new_folder2# ls
new_file new_file2 new_file3 new_file_copy
-
mv命令
mv表示move移動(dòng)。mv命令有兩個(gè)功能。
(1) 移動(dòng)文件/目錄
移動(dòng)目錄也不需要添加新的參數(shù)(-r)。
root@ecs-x-large-2-linux-20200315202744:~# mv new_file2 new_folder3 new_folder
root@ecs-x-large-2-linux-20200315202744:~# cd new_folder
root@ecs-x-large-2-linux-20200315202744:~/new_folder# ls
new_file new_file2 new_folder3 new_file_copy
(2) 重命名文件/目錄
移動(dòng)到當(dāng)前目錄則相當(dāng)于重命名。
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Music Templates new_file new_folder2
Documents Pictures Videos new_file_copy new_folder_copy
Downloads Public dir1 new_folder
root@ecs-x-large-2-linux-20200315202744:~# mv new_folder2 new_folder2_rename
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Music Templates new_file new_folder2_rename
Documents Pictures Videos new_file_copy new_folder_copy
Downloads Public dir1 new_folder
root@ecs-x-large-2-linux-20200315202744:~#
注意:拷貝、移動(dòng)等命令都可以使用通配符*。
-
rm命令
刪除文件和目錄。
-i:詢問是否刪除。
-f:不詢問強(qiáng)制刪除。
-r:遞歸刪除,用來刪除目錄。
root@ecs-x-large-2-linux-20200315202744:~# rm new_file_copy
root@ecs-x-large-2-linux-20200315202744:~# rm -i new_file
rm: remove regular empty file 'new_file'? n
root@ecs-x-large-2-linux-20200315202744:~# rm -r new_folder2_rename
root@ecs-x-large-2-linux-20200315202744:~# ls
Desktop Downloads Pictures Templates dir1 new_folder
Documents Music Public Videos new_file new_folder_copy
注意:① rm -rf的含義是強(qiáng)制遞歸刪除文件夾,很危險(xiǎn)。② 超級(jí)管理員運(yùn)行rm -rf /*或者rm -rf /命令會(huì)刪除你的整個(gè)Linux系統(tǒng)。
-
rmdir
只能用來刪除一個(gè)空的目錄,不常用。
root@ecs-x-large-2-linux-20200315202744:~# mkdir aaa
root@ecs-x-large-2-linux-20200315202744:~# rmdir aaa
- 文件的存儲(chǔ)
文件名、權(quán)限和文件內(nèi)容。
每個(gè)文件的文件內(nèi)容被分配一個(gè)標(biāo)識(shí)號(hào)碼,即inode。
每個(gè)文件名都綁定到它的文件內(nèi)容,用inode標(biāo)識(shí)。
ls -i- 查看文件的inode
root@ecs-x-large-2-linux-20200315202744:~# ls -i
1843494 Desktop 1843495 Downloads 1843500 Pictures 1843496 Templates
1843498 Documents 1843499 Music 1843497 Public 1843501 Videos
-
ln命令
創(chuàng)建鏈接(快捷方式)。
分類:Physical link物理鏈接/硬鏈接、Symbolic link符號(hào)鏈接/軟鏈接
硬鏈接只能夠指向文件。軟鏈接既能夠指向文件,又能夠指向目錄。軟鏈接應(yīng)用更廣泛。
(1)Physical link物理鏈接/硬鏈接
創(chuàng)建文件1的硬鏈接文件2
命令:ln 文件1 文件2
原理:文件1→inode文件內(nèi)容,文件2→inode文件內(nèi)容
介紹:文件2和文件1的inode相同。修改文件1和文件2,則修改的是同一塊內(nèi)容。文件1和文件2都刪除,文件內(nèi)容才會(huì)刪除。
root@ecs-x-large-2-linux-20200315202744:~# mkdir test
root@ecs-x-large-2-linux-20200315202744:~# cd test
root@ecs-x-large-2-linux-20200315202744:~/test# touch f1
root@ecs-x-large-2-linux-20200315202744:~/test# ln f1 f2
root@ecs-x-large-2-linux-20200315202744:~/test# ls -l
total 0
-rw-r--r-- 2 root root 0 Aug 1 12:02 f1
-rw-r--r-- 2 root root 0 Aug 1 12:02 f2
root@ecs-x-large-2-linux-20200315202744:~/test# ls -i
1843520 f1 1843520 f2
root@ecs-x-large-2-linux-20200315202744:~/test#
注意:2表示指向該文件內(nèi)容的文件數(shù)為2。f1和f2指向的文件內(nèi)容inode相同(1843520)。
(2) Symbolic link符號(hào)鏈接/軟鏈接
創(chuàng)建文件1的軟鏈接文件2
命令:ln -s 文件1 文件2 。其中,s是symbolic符號(hào)的縮寫。
原理:文件1 → inode文件內(nèi)容,文件2 → 文件1
介紹:文件2和文件1的inode不同。如果刪除文件1,則文件2會(huì)變成死鏈接。
root@ecs-x-large-2-linux-20200315202744:~/test# touch f1
root@ecs-x-large-2-linux-20200315202744:~/test# ls
f1
root@ecs-x-large-2-linux-20200315202744:~/test# ln -s f1 f2
root@ecs-x-large-2-linux-20200315202744:~/test# ls
f1 f2
root@ecs-x-large-2-linux-20200315202744:~/test# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 1 12:15 f1
lrwxrwxrwx 1 root root 2 Aug 1 12:16 f2 -> f1
root@ecs-x-large-2-linux-20200315202744:~/test# ls -i
1843520 f1 1843521 f2
注意:第一個(gè)字符-表示普通文件,l表示軟鏈接。 1表示指向該文件內(nèi)容的文件數(shù)為1。f1和f2指向的文件內(nèi)容inode不同。
2.3 Linux下的權(quán)限管理
2.3.1 用戶權(quán)限
-
Linux中的用戶組織
Linux系統(tǒng)是一個(gè)多用戶多任務(wù)的分時(shí)操作系統(tǒng),任何一個(gè)要使用系統(tǒng)資源的用戶,都必須首先向系統(tǒng)管理員申請(qǐng)一個(gè)賬號(hào),然后以這個(gè)賬號(hào)的身份進(jìn)入系統(tǒng)。
用戶 賬號(hào)一方面可以幫助系統(tǒng)管理員對(duì)使用系統(tǒng)的用戶進(jìn)行跟蹤,并控制他們對(duì)系統(tǒng)資源的訪問;另一方面也可以幫助用戶組織文件,并為用戶提供安全性保護(hù)。 -
sudo與su相關(guān)命令
sudo:暫時(shí)切換到超級(jí)用戶模式以執(zhí)行超級(jí)用戶權(quán)限,需要輸入當(dāng)前用戶密碼。有時(shí)間限制,Ubuntu默認(rèn)為一次時(shí)長(zhǎng)15分鐘。
su:切換到某某用戶模式,需要輸入切換后賬戶的密碼。如果不加賬戶默認(rèn)為root賬戶。沒有時(shí)間限制。
sudo su:運(yùn)行sudo命令給su命令提權(quán),運(yùn)行su命令,切換到root模式。
注意:普通用戶切換到root用戶模式有兩種方式。一種是su命令,需要輸入root密碼,一種是sudo su命令,需要輸入當(dāng)前用戶密碼。 - 創(chuàng)建用戶
adduser nmw
root@ecs-x-large-2-linux-20200315202744:~# adduser nmw
Adding user `nmw' ...
Adding new group `nmw' (1000) ...
Adding new user `nmw' (1000) with group `nmw' ...
Creating home directory `/home/nmw' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for nmw
Enter the new value, or press ENTER for the default
Full Name []: nimengwei
Room Number []:
Work Phone []: 17626186618
Home Phone []:
Other []:
Is the information correct? [Y/n] y
創(chuàng)建用戶后會(huì)默認(rèn)在/home目錄中添加nmw用戶的家目錄。
root@ecs-x-large-2-linux-20200315202744:~# cd /home
root@ecs-x-large-2-linux-20200315202744:/home# ls
nmw
- 修改密碼
passwd nmw - 刪除用戶
刪除用戶:deluser nmw
刪除用戶及對(duì)應(yīng)家目錄:deluser --remove-home nmw
注意:adduser和deluser只是Debian一族(包括Ubuntu)才有的命令。其他Linux發(fā)行版,添加用戶和刪除用戶一般用useradd和userdel。 - 用戶群組管理
(1) 創(chuàng)建用戶如果不設(shè)置群組,則會(huì)創(chuàng)建一個(gè)和用戶名相同的群組,并將用戶劃歸到這個(gè)群組。
(2) 查看家目錄
root@ecs-x-large-2-linux-20200315202744:~# ls -l /home
total 4
drwxr-xr-x 2 nmw nmw 4096 Aug 1 17:39 nmw
其中,第三列nmw表示文件/目錄的所有者,第四列nmw表示文件或/目錄所在的群組。
(3) 創(chuàng)建群組
addgroup friends
root@ecs-x-large-2-linux-20200315202744:~# addgroup friends
Adding group `friends' (GID 1001) ...
Done.
(4) 刪除群組
delgroup happy
root@ecs-x-large-2-linux-20200315202744:~# delgroup happy
Removing group `happy' ...
Done.
- 修改用戶群組
(1)usermod 用戶名
-l對(duì)用戶重命名。/home中的家目錄名不變,需要手動(dòng)修改。
-g修改用戶所在群組。原群組剔除此用戶。
-G將用戶添加到多個(gè)群組。原群組剔除此用戶。
-aG追加到新的群組。原群組不剔除此用戶。
注意:使用usermod命令修改用戶組時(shí),很容易被剔除原有組,因此盡量不要使用。可以使用gpasswd命令。
(2)gpasswd -a 用戶名 組名
一次從一個(gè)組中添加(或刪除)一個(gè)用戶。
(3)groups 用戶名
可以查看該用戶所在的群組
root@ecs-x-large-2-linux-20200315202744:~# usermod -g friends nmw
root@ecs-x-large-2-linux-20200315202744:~# usermod -G friends,happy nmw
root@ecs-x-large-2-linux-20200315202744:~# usermod -aG friends,happy nmw
root@ecs-x-large-2-linux-20200315202744:~# groups nmw
nmw : friends happy
root@ecs-x-large-2-linux-20200315202744:~# groups root
root : root
root@ecs-x-large-2-linux-20200315202744:~# gpasswd -a nmw sudo
Adding user nmw to group sudo
2.3.2 文件權(quán)限
- 文件訪問權(quán)限符
-:如果在第一個(gè)字符,表示普通文件;否則,表示沒有權(quán)限。
d:目錄;
l:鏈接;
r:讀;
w:寫
x:執(zhí)行/運(yùn)行 - 三種基本權(quán)限
(1)r讀 數(shù)值表示為4
(2)w寫 數(shù)值表示為2
(3)x可執(zhí)行 數(shù)值表示為1 - 文件權(quán)限介紹
例如:drwxrwxr-x
一共十個(gè)字符,分成四段。
(1) 第1個(gè)字符d表示目錄,-表示普通文件,l表示鏈接。
(2) 第2-4個(gè)字符rwx表示當(dāng)前所屬用戶的權(quán)限,用數(shù)值表示為4+2+1=7。
(3) 第5-7個(gè)字符rwx表示當(dāng)前所屬組的權(quán)限,用數(shù)值表示為4+2+1=7。
(4) 第8-10個(gè)字符r-x表示其他用戶權(quán)限,用數(shù)值表示為4+1=5。
此文件的權(quán)限用數(shù)值表示為775。 - 使用數(shù)字分配權(quán)限
chmod 765 filename
參數(shù):-R遞歸修改文件訪問權(quán)限。
chmod是change和mode的縮寫。
只要你是此文件的所有者,就可以用chmod來修改文件的訪問權(quán)限。
root@ecs-x-large-2-linux-20200315202744:/home/nmw# touch 1.txt
root@ecs-x-large-2-linux-20200315202744:/home/nmw# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 1 20:06 1.txt
root@ecs-x-large-2-linux-20200315202744:/home/nmw# chmod 600 1.txt
root@ecs-x-large-2-linux-20200315202744:/home/nmw# ls -l
total 0
-rw------- 1 root root 0 Aug 1 20:06 1.txt
- 使用字母分配權(quán)限
范圍:u用戶、g群組、o其他、a所有
分配:+添加權(quán)限、-移除權(quán)限、=分配權(quán)限。
chmod u+rx file:所屬用戶添加讀和運(yùn)行的權(quán)限。
chmod g+r file:群組其他用戶增加讀的權(quán)限。
chmod o-r file:其他用戶移除讀的權(quán)限。
chmod g+r o-r file:群組其他用戶增加讀的權(quán)限,其他用戶移除讀的權(quán)限。
chmod go-r file:群組其他用戶和其他用戶移除讀的權(quán)限。
chmod a+x file:所屬用戶增加運(yùn)行的權(quán)限。
chmod u=rwx,g=r,o=- file:所屬用戶分配讀寫和執(zhí)行權(quán)限,群組其他用戶分配讀的權(quán)限,其他用戶分配無權(quán)限。
注意:使用字母分配權(quán)限不需要一次設(shè)置三組。
3. Linux中的文本編輯和軟件安裝
3.1 Nano文本編輯器
-
Linux終端文本編輯器
Nano、Vim、Emacs等 -
nano介紹
nano:毫微、十億分之一。
功能強(qiáng)大,適合入門。
Ubuntu和macOS系統(tǒng)都默認(rèn)安裝了Nano和Vim。
注釋:pico的含義是萬億分之一。 -
nano組合快捷鍵
Ctrl + G顯示幫助文檔、Ctrl + O保存文件、Ctrl + R打開其他文件
Ctrl + Y上一個(gè)屏幕、Ctrl + V下一個(gè)屏幕、Ctrl + K剪切當(dāng)前一行
Ctrl + U粘貼剪切的內(nèi)容、Ctrl + X退出、Ctrl + W查找
Ctrl + \替換、Ctrl + F向前移動(dòng)一格光標(biāo)、Ctrl + B向后移動(dòng)一格光標(biāo)
Ctrl + P向上移動(dòng)一行、Ctrl + N向下移動(dòng)一行 -
nano的參數(shù)
nano 文件:如果有文件且有寫的權(quán)限,則打開。如果沒有文件,則創(chuàng)建文件。
(1)-m
激活鼠標(biāo)控制光標(biāo)位置。
(2)-i
激活自動(dòng)縮進(jìn)的功能。
(3)-A
激活智能Home鍵的功能。
nano -miA file_nano
- 單個(gè)用戶配置
.nanorc
每個(gè)Linux用戶都可以在自己的家目錄創(chuàng)建.nanorc文件。
每次nano啟動(dòng)前,都會(huì)讀取此配置文件。
語法:通過set和unset進(jìn)行配置。
例如:set mouse激活鼠標(biāo),set autoindent激活自動(dòng)縮進(jìn),set smarthome激活只能Home鍵。
root@ecs-x-large-2-linux-20200315202744:/home/nmw# nano .nanorc
root@ecs-x-large-2-linux-20200315202744:/home/nmw# cat .nanorc
set mouse
set autoindent
set smarthome
注釋:vim的配置文件是.vimrc,bash shell的配置文件是.bashrc。可以通過nano ~/.nanorc進(jìn)行單用戶nano配置。
- 配置全局
nanorc
全局nano配置文件只能被root用戶修改。
位置:/etc/nanorc。
命令:sudo nano /etc/nanorc
優(yōu)先級(jí):家目錄下的.nanorc配置優(yōu)先級(jí)更高。
總結(jié):單用戶配置文件在~/,全局配置文件在/etc/。單用戶配置文件起始為點(diǎn).,全局配置文件沒有。 -
bash配置
shell是用戶和操作系統(tǒng)之間的命令解釋器。Ubuntu和大部分常見的Linux發(fā)行版默認(rèn)的shell程序就是bash。
單個(gè)用戶配置文件:nano ~/.bashrc
全局配置文件:nano /etc/bash.bashrc
優(yōu)先級(jí):家目錄下.bashrc配置優(yōu)先級(jí)更高。
配置別名: 配置文件中添加alias 別名='命令' -
profile配置
單個(gè)用戶配置文件:nano ~/.profile
全局配置文件:nano /etc/profile
.bashrc與.profile對(duì)比如下:
(1) 登錄的shell:用戶名/密碼 → 使用.profile配置的shell→調(diào)用.bashrc
(2) 不登錄的shell:使用.bashrc配置的shell - 使配置文件立即生效
修改.bashrc和.profile文件后,默認(rèn)在用戶下次登錄系統(tǒng)時(shí)才生效。
可使用source命令使改動(dòng)立即生效:
source .bashrc
3.2 Ubuntu中的軟件安裝
- 本章節(jié)軟件安裝相關(guān)知識(shí)點(diǎn)是基于
Debian的Linux發(fā)行版。不是Debian一族的Linux發(fā)行版軟件安裝方式稍有不同,例如:Fedora、Redhat、CentOS。 - 軟件包
包的后綴:.deb
軟件倉庫:repository全球有很多軟件倉庫。
軟件倉庫列表文件:nano /etc/apt/sources.list - 軟件倉庫列表信息
deb http://archive.ubuntu.com/ubuntu/ bionic universe
deb:Debian
http://archive.ubuntu.com/ubuntu/:倉庫地址
bionic:Ubuntu 18.04代號(hào)
universe:軟件倉庫的區(qū)域
如果要切換到其他軟件倉庫,只需要修改倉庫地址。 - 包管理工具
apt-get:常用。
aptitude:卸載軟件時(shí)可以卸載不同的依賴。默認(rèn)沒有安裝。 - 常用命令介紹
以下命令都需要root權(quán)限,非root用戶需要加sudo。
更新可安裝的軟件列表:apt-get update
升級(jí)所有已安裝軟件:apt-get upgrade
在軟件列表里搜索可用軟件:apt-cache search
安裝軟件:apt-get install XXX
卸載軟件:apt-get autoremove XXX -
dpkg命令
dpkg是apt-get和aptitude兩個(gè)命令的基礎(chǔ)命令。
dpkg可以安裝本地的.deb軟件包。
安裝:dpkg -i XXX.deb
卸載:dpkg -r XXX
3.3 RTFM閱讀那該死的手冊(cè)
-
Read The Fucking Manual
可執(zhí)行程序或shell命令
系統(tǒng)調(diào)用(Linux內(nèi)核提供的函數(shù))
庫調(diào)用(程序庫中的函數(shù))
特殊文件(通常在/dev下)
文件格式和慣例(例如/etc/passwd)
游戲
雜項(xiàng)(包括宏包和慣例,比例man(7),groff(7))
系統(tǒng)管理命令(通常只能被root用戶使用)
內(nèi)核子程序 -
man命令
查看相關(guān)命令或函數(shù):man 數(shù)字 命令/函數(shù)
如果不加數(shù)字,man默認(rèn)從數(shù)字較小的手冊(cè)中尋找相關(guān)命令和函數(shù)。
↑和↓實(shí)現(xiàn)上一行和下一行跳轉(zhuǎn),空格鍵實(shí)現(xiàn)下一頁跳轉(zhuǎn),/斜杠實(shí)現(xiàn)搜索,q退出。
root@ecs-x-large-2-linux-20200315202744:~# man ls
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
...
注釋:NAME名字,SYNOPSIS摘要,DESCRIPTION描述,AUTHOR作者,REPORTING BUGS報(bào)告故障,COPYRIGHT版權(quán),SEE ALSO另見。
-
SYNOPSIS區(qū)域介紹
示例1
root@ecs-x-large-2-linux-20200315202744:~# man mkdir
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
(1) 字段介紹
[OPTION]:可選,添加參數(shù),例如:-m、-p、-v等。
DIRECTORY:必選,目錄。
...:表示可以指定多個(gè)參數(shù)和多個(gè)目錄
(2) 字體介紹
粗體:表示要原封不動(dòng)的輸入。例如:mkdir
下劃線:表示要用實(shí)際內(nèi)容替換。例如:[OPTION]和DIRECTORY
示例2
root@ecs-x-large-2-linux-20200315202744:~# man cp
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
(1) 拷貝一個(gè)源文件到目標(biāo)文件
cp [OPTION]... [-T] SOURCE DEST
例如:cp file.txt file_copy.txt
(2) 拷貝一個(gè)或多個(gè)源文件到目錄
cp [OPTION]... SOURCE... DIRECTORY
例如:cp file.txt file_copy.txt photo/
(3) 拷貝一個(gè)或多個(gè)源文件到目錄
cp [OPTION]... -t DIRECTORY SOURCE...
不常用。與(2)效果相同,但是把目錄名放在了前面。-t必須加。
示例3
root@ecs-x-large-2-linux-20200315202744:~# man apt-get
SYNOPSIS
apt-get [-asqdyfmubV] [-o=config_string] [-c=config_file]
[-t=target_release] [-a=architecture] {update | upgrade |
dselect-upgrade | dist-upgrade |
install pkg [{=pkg_version_number | /target_release}]... |
remove pkg... | purge pkg... |
source pkg [{=pkg_version_number | /target_release}]... |
build-dep pkg [{=pkg_version_number | /target_release}]... |
download pkg [{=pkg_version_number | /target_release}]... |
check | clean | autoclean | autoremove | {-v | --version} |
{-h | --help}}
該命令區(qū)域只有一行,只是顯示在了多行。
(1) [-asqdyfmubV]
可以使用-a、-s、-q、-d等參數(shù)中的一個(gè)或多個(gè)。
(2) [-o=config_string] [-c=config_file] [-t=target_release] [-a=architecture]
這幾個(gè)參數(shù)如果使用,必須加上參數(shù)值。
(3) {update | upgrade | ... | {-v | --version} | {-h | --help}}
必須且只能使用|(或)隔開的某一項(xiàng)。
例如:apt-get install XXX、apt-get update、apt-get autoclean
錯(cuò)誤用法:apt-get update install XXX
- 查找命令
語法:apropos 關(guān)鍵字
在手冊(cè)中查找所有含有此關(guān)鍵字的相關(guān)命令。
root@ecs-x-large-2-linux-20200315202744:~# apropos sound
default.pa (5) - PulseAudio Sound Server Startup Script
pacat (1) - Play back or record raw or encoded audio streams on a PulseA...
pacmd (1) - Reconfigure a PulseAudio sound server during runtime
pactl (1) - Control a running PulseAudio sound server
pamon (1) - Play back or record raw or encoded audio streams on a PulseA...
paplay (1) - Play back or record raw or encoded audio streams on a PulseA...
parec (1) - Play back or record raw or encoded audio streams on a PulseA...
parecord (1) - Play back or record raw or encoded audio streams on a PulseA...
pavucontrol (1) - A volume control for the PulseAudio sound server
pulseaudio (1) - The PulseAudio Sound System
start-pulseaudio-x11 (1) - PulseAudio Sound Server X11 Startup Script
-
-h和--help參數(shù)
顯示命令的幫助文檔。
root@ecs-x-large-2-linux-20200315202744:~# apt-get -h
apt 1.6.12 (amd64)
Usage: apt-get [options] command
apt-get [options] install|remove pkg1 [pkg2 ...]
apt-get [options] source pkg1 [pkg2 ...]
...
Most used commands:
update - Retrieve new lists of packages
upgrade - Perform an upgrade
install - Install new packages (pkg is libc6 not libc6.deb)
...
注釋:-h沒有man命令詳細(xì),但比man命令可讀性更強(qiáng)。
-
whatis命令
whatis命令是man命令的精簡(jiǎn)版。只會(huì)顯示man命令顯示手冊(cè)的開頭部分。
root@ecs-x-large-2-linux-20200315202744:~# whatis ls
ls (1) - list directory contents
-
locate命令
搜索包含關(guān)鍵字的所有文件和目錄。
語法:locate 文件/目錄
特點(diǎn):從文件數(shù)據(jù)庫中查找,速度快。對(duì)于剛創(chuàng)建的文件,還沒有被收錄到文件數(shù)據(jù)庫,因此locate目錄就查不到。模糊查詢。
文件數(shù)據(jù)庫更新:Linux系統(tǒng)一般每天會(huì)更新一次文件數(shù)據(jù)庫。我們可以使用updatedb命令強(qiáng)制系統(tǒng)立即更新文件數(shù)據(jù)庫。
root@ecs-x-large-2-linux-20200315202744:~# locate nmw
/home/nmw
/home/nmw/.bash_logout
/home/nmw/.bashrc
root@ecs-x-large-2-linux-20200315202744:~# touch new_file
root@ecs-x-large-2-linux-20200315202744:~# locate new_file
root@ecs-x-large-2-linux-20200315202744:~# updatedb
root@ecs-x-large-2-linux-20200315202744:~# locate new_file
/root/new_file
root@ecs-x-large-2-linux-20200315202744:~#
-
find命令
搜索包含關(guān)鍵字的所有文件和目錄。
語法:find [path] what [do]
含義:path指定在哪個(gè)目錄內(nèi)查找,默認(rèn)從當(dāng)前目錄開始查找。what要查找的文件名、大小、最近訪問時(shí)間等。do對(duì)查找到的文件做后續(xù)操作。
what參數(shù):-name文件名,-size文件大小,-atime最近訪問時(shí)間,-type文件類型
特點(diǎn):與locate命令不同,find命令不會(huì)在文件數(shù)據(jù)庫中查找文件記錄,而是實(shí)際遍歷你的硬盤。因此可以查找到剛創(chuàng)建的文件。精準(zhǔn)查詢,可以使用*通配符實(shí)現(xiàn)模糊查詢。 -
find命令參數(shù)
(1)-name根據(jù)文件名查找
root@ecs-x-large-2-linux-20200315202744:~# find -name new_file
./new_file
root@ecs-x-large-2-linux-20200315202744:~# find -name syslog
root@ecs-x-large-2-linux-20200315202744:~# find /var/log -name syslog
/var/log/syslog
/var/log/installer/syslog
root@ecs-x-large-2-linux-20200315202744:~# find /var/log -name syslog*
/var/log/syslog.1
/var/log/syslog.2.gz
/var/log/syslog
/var/log/syslog.4.gz
/var/log/syslog.3.gz
/var/log/installer/syslog
(2) -size根據(jù)文件大小查找
root@ecs-x-large-2-linux-20200315202744:~# find /var -size +50M
/var/cache/apt/srcpkgcache.bin
/var/cache/apt/pkgcache.bin
root@ecs-x-large-2-linux-20200315202744:~# find -size 2k
./.ICEauthority
./.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml
root@ecs-x-large-2-linux-20200315202744:~# find -size 10k
./.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
(3) -atime根據(jù)文件最近訪問時(shí)間查找
root@ecs-x-large-2-linux-20200315202744:~# find / -name *.log -atime -7
/root/.vnc/ecs-x-large-2-linux-20200315202744:1.log
/usr/local/hostguard/log/daemon.log
/usr/local/hostguard/log/hostguard.log
...
注釋:-atime -7表示最近7天訪問過。
(4) -type根據(jù)類型查找
-type d只查找目錄,-type f只查找文件。
root@ecs-x-large-2-linux-20200315202744:~# find -name file1
./file1
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -type d
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -type f
./file1
10.find命令操作查找結(jié)果
(1) -printf格式化打印
%p文件名,%u文件所有者、\n換行。
(2) -delete刪除
(3) -exec 無提示操作
{} 用于與-exec選項(xiàng)結(jié)合使用來匹配所有文件,然后會(huì)被替換為相應(yīng)的文件名。
(4) -ok 提示操作
與-exec參數(shù)作用相同,但會(huì)對(duì)每一個(gè)文件操作做確認(rèn)提示。
root@ecs-x-large-2-linux-20200315202744:~# find -name file1
./file1
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -print
./file1
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -printf "%p - %u\n"
./file1 - root
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -exec chmod 600 {} \;
root@ecs-x-large-2-linux-20200315202744:~# ls -l file1
-rw------- 1 root root 0 Aug 4 21:04 file1
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -ok chmod 600 {} \;
< chmod ... ./file1 > ? y
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -ok chmod 600 {} \;
< chmod ... ./file1 > ? y
root@ecs-x-large-2-linux-20200315202744:~# find -name file1 -delete
root@ecs-x-large-2-linux-20200315202744:~# find -name file1
注釋:find -name file1等價(jià)于find -name file1 -print
資料:find命令