? ? ? ? ? ? ? ? ? ? ? ? ? ?? find查找-day13
上周回顧
用戶管理?
用戶基本概述?
介紹創(chuàng)建用戶會(huì)影響的文件??
? ?? /etc/passwd?
? ?? /etc/shadow?
如何創(chuàng)建用戶 useradd -u,-g,-G,-M.-d.-c.-s,-r?
如何修改用戶 usermod -u,-g,-G,-d.-c.-s,-r,-l?
如何刪除用戶 userdel -r?
如何給用戶設(shè)定密碼 passwd passwd --stdin?
用戶創(chuàng)建流程? /etc/login.defs /etc/defaults/useradd?
組的管理?
組的基本概念?
? 基本組: 創(chuàng)建用戶時(shí)使用-g指定基本組, 基本組必須事先存在?
? 私有組: 創(chuàng)建用戶時(shí),不指定基本組,則默認(rèn)創(chuàng)建同名的私有組?
? 附加組: 當(dāng)基本組無法滿足權(quán)限需求,可關(guān)聯(lián)多個(gè)附加組 組的配置文件? /etc/group ? ? /etc/gshadow 如何創(chuàng)建組 groupadd -g 指定gid
? 如何修改組 groupmod -g 修改gid -n 修改組名稱?
? 如何刪除組 groupdel?
如何給用戶提權(quán)??
su 切換用戶?
? 交互式shell 輸入一條命令,返回一條輸出?
? 非交互式shell 輸入一條指令,批量的完成任務(wù),無需人為干預(yù)?
? 登錄shell 輸入用戶名和密碼登錄系統(tǒng)?
? 非登錄式shell 無需輸入用戶名和密碼,即可登錄用戶?
? 登錄式shell和非登錄式shell 加載的配置文件不一樣?
? su username使用的是非登錄式shell (有些環(huán)境變量沒有 加載,會(huì)導(dǎo)致執(zhí)行失敗)?
? su - username 使用的是登錄式shell (加載所有的環(huán)境變 量)?
? PS: su 切換用戶時(shí)需要輸入用戶的密碼,如果是root使用su 則無限制?
? ? ?? su 需要知道用戶的密碼, 不安全?
sudo 提權(quán)?
? 提權(quán)的作用: 讓用戶臨時(shí)的使用root身份進(jìn)行操作.?
? 首先必須先進(jìn)行權(quán)限劃分 ---> root?
? 其次將權(quán)限與用戶進(jìn)行關(guān)聯(lián) ----> root?
? 最后用戶才能正常使用,或者提權(quán) sudo -l 檢查分配的權(quán)限 是否合理
權(quán)限管理?
權(quán)限: 指用戶能對(duì)系統(tǒng)操作的一種范圍限制.?
用戶和權(quán)限的關(guān)系??
權(quán)限中rwx是什么意思??
? r 可讀 ---->4?
? w 可寫 ----->2?
? x 可執(zhí)行 ------>1?
對(duì)一個(gè)文件進(jìn)行授權(quán)? chmod 755
權(quán)限中rwx對(duì)文件有什么限制??
權(quán)限中rwx對(duì)目錄有什么限制??
PS: 操作文件看文件本身的權(quán)限, 如果是刪除\移動(dòng)\復(fù)制\等操 作,需要看文件的上級(jí)目錄是否給予了足夠的權(quán)限 , 通常是看 有沒有 w 權(quán)限?
授權(quán)基本規(guī)則:?
? 文件: chmod 644 chmod 600 為了安全?
? 目錄: chmod 755?
? 命令: chmod 755 通常來說,命令的屬主和屬組都是root?
變更一個(gè)文件的屬主和屬組? chown -R?
特殊權(quán)限?
Suid: 為命令設(shè)定suid,當(dāng)任何用戶執(zhí)行該命令時(shí),都將以該命令的屬主身份運(yùn)行?
Sgid: 使多個(gè)用戶共享一個(gè)組?
Sbit: 粘滯位, 任何人都可以在該目錄下創(chuàng)建文件,但只允許刪 除自己的. 除root以外?
特殊屬性?
? ? chattr -i 鎖住文件 -a 僅能追加,但不允許其他操作?
? ? lsattr 查看文件特殊屬性?
Umask 控制默認(rèn)權(quán)限 (通常表示要減去的權(quán)限)?
? ? 文件: 666 - umask = 最終權(quán)限?
? ? 目錄: 777 - umask = 最終權(quán)限?
? ? /etc/profile 會(huì)控制umask
重定向與管道?
重定向: 將輸出至屏幕的內(nèi)容,重新指向到一個(gè)文件,或丟失. 標(biāo)
準(zhǔn)輸入 0 標(biāo)準(zhǔn)輸出1 錯(cuò)誤輸出2 修改指向使用 >?
重定向:?
?? > 覆蓋重定向,會(huì)先清空掉原有文件的內(nèi)容,然后寫入新的. 文件不存在則創(chuàng)建?
?? >> 追加重定向, 不會(huì)覆蓋原有的內(nèi)容,而是往文件尾部新增內(nèi)容
?? 2> 錯(cuò)誤覆蓋重定向?
?? 2>> 錯(cuò)誤追加重定向?
?? &> 混合輸出, 正確和錯(cuò)誤都輸出到同一個(gè)文件中, 沒次都 會(huì)覆蓋文件已有內(nèi)容?
?? &>> 混合輸出, 文件尾部添加內(nèi)容.
管道技術(shù)?
管道: 將左邊命令的輸出 --> 管道 ---> 傳遞給右邊命令的輸入?
tee: 將左邊命令的輸出 -->| tee a.log ---> 傳遞給右邊命令的輸入?
?? 1.不影響管道的傳遞?
?? 2.會(huì)將左邊命令的輸出,寫入到一個(gè)a.log文件中?
xargs: 將無法使用管道的命令支持使用管道?
? ? which useradd | xargs ls -l (將前者命令的結(jié)果作為參數(shù)傳 遞給后者命令)
本周內(nèi)容
1.文件查找?
2.文件壓縮?
3.軟件包管理-rpm?
4.軟件包管理-yum?
5.軟件包管理-源碼?
6.磁盤的基本使用?
7.磁盤陣列RAID (LVM 不講)
今日內(nèi)容
1.為什么要有文件查找??
資料很多,忘記放哪了,所以需要通過查找的方式進(jìn)行搜索.?
2.windows如何實(shí)現(xiàn)文件查找?
計(jì)算機(jī)-->搜索框
3.linux如何實(shí)現(xiàn)文件查找??
?? 1.沒有圖形工具?
?? 2.使用命令工具搜索--->find
4.find命令查找語法?
命令 ? ? ? ? ? ? ? 路徑 ? ? ? ? ? ? ? 選項(xiàng) ? ? ? ? ? ? ? 表達(dá)式 ? ? ? ? ? ? ? ? ? 動(dòng)作
?find ? ? ? ? ? ? [path...] ? ? ? ?? [options] ? ? ?? [expression] ? ? ? ? ?? [action]?
?查找 ? ? ? ? ? ?? 地區(qū) ? ? ? ? ? ? ?? 妹紙 ? ? ? ? ? ?? 18-25歲 ? ? ? ? ? ? ? ?? ???
5.find針對(duì)文件名稱、類型擎颖、大小睛榄、修改時(shí)間等方式進(jìn)行查找文件?
1) 按名稱查找
#1.按照名稱進(jìn)行查找?
?? [root@oldboyedu ~]# find ./ -name "*eth0"
#2.按照名稱查找(不區(qū)分大小寫)?
?? [root@oldboyedu ~]# find ./ -iname "*eth0"
2) 按文件大小查找 size
#1.查找/etc/目錄下大于5M的文件?
?? [root@oldboyedu ~]# find /etc/ -size +5M
#2.查找/etc/目錄下小于5M的文件?
?? [root@oldboyedu ~]# find /etc/ -size -5M
#3.查找/etc/目錄下等于5M的文件?
?? [root@oldboyedu ~]# find /etc/ -size 5M
3) 按文件類型查找 -type
f ? ? ? # 文件?
d ? ?? # 目錄?
s ? ?? # socket套接字文件?
l ? ? ? # 鏈接文件?
c ? ?? # 字符設(shè)備?
b ? ?? # 塊設(shè)備
#1.查找當(dāng)前目錄下類型是文件的,并且名稱跟eth0相關(guān)的都列出來?
?? [root@oldboyedu ~]# find ./ -type f -iname "*eth0" | xargs ls -l
#2.查找/etc/目錄下類型是文件的,大小是大于5M,名稱以.bin結(jié)尾的?
?? [root@oldboyedu ~]# find /etc/ -type f? -size +5M? name "*.bin"
#3.查找/etc/目錄下類型是文件的,名稱是.repo結(jié)尾的?
?? [root@oldboyedu ~]# find /etc/ -type f? -name "*.repo"
#4.查找/dev下的類型是塊設(shè)備的,并名稱是sda開頭的?
?? [root@oldboyedu ~]# find /dev/ -type b -name "sda*" | xargs ls -l
#5.查找/dev下的類型是字符設(shè)備,并名稱是tty開頭的?
?? [root@oldboyedu ~]# find /dev/ -type c -name "tty*"
4) 按修改時(shí)間進(jìn)行查找 -mtime
[root@oldboyedu ~]# for i in {1..31}; do date -s "2019/08/$i" ; touch file-$i ; done
#1.第7天
?? [root@oldboyedu ~]# find ./ -type f -mtime 7
#2.7天以前的內(nèi)容都會(huì)被篩選出來,然后刪除. ? 保留了最近7天的內(nèi)容 ? ?
?? [root@oldboyedu ~]# find ./ -type f -mtime +7 -name "file-*"
#3.最近7天的內(nèi)容都會(huì)被篩選出來?
?? [root@oldboyedu ~]# find ./ -type f -mtime -7 -name "file-*"
#4.本地文件保留最近7天的備份文件, 備份服務(wù)器保留3個(gè)月的備份文件 (實(shí)際使用方案) ? ? ??
?? find /backup/ -iname "*.bak" -mtime +7 -delete?
?? find /backup/ -iname "*.bak" -mtime +180 -delete
5) 按用戶和組進(jìn)行查找 -user -group -nouser -nogroup
#查找屬主是jack?
?? [root@xuliangwei ~]# find /home -user jack?
#查找屬組是admin?
?? [root@xuliangwei ~]# find /home -group admin
#查找屬主是jacky, 屬組是jack?
?? [root@oldboyedu ~]# find /home/ -type d -user jacky group jack
#查找沒有屬主?
?? [root@xuliangwei ~]# find /home -nouser?
#查找沒有屬組?
?? [root@xuliangwei ~]# find /home -nogroup?
#查找沒有屬主或?qū)俳M?
?? [root@xuliangwei ~]# find / -nouser -nogroup
6.find查找后的處理動(dòng)作?
查找到一個(gè)文件后要门,需要對(duì)文件進(jìn)行如何處理,find的默認(rèn)動(dòng)作是 print
動(dòng)作 含義?
-print 打印查找到的內(nèi)容(默認(rèn)) ---ignore?
-ls 以長格式顯示的方式打印查找到的內(nèi)容 ---ignore | xargs ls -l delete 刪除查找到的文件 (刪除目錄,僅能刪除空目錄) ---ignore | xargs rm -f -ok 后面跟自定義 shell 命令(會(huì)提示是否操作) ---ignore -exec 后面跟自定義 shell 命令(標(biāo)準(zhǔn)寫法 -exec \;) | xargs
[root@oldboyedu ~]# time find ./ -type f? -name "file*"? -exec rm -f {} \; real? ? 0m6.585s user? ? 0m3.617s sys? ?? 0m3.532s
[root@oldboyedu ~]# time find ./ -type f? -name "file*"? | xargs rm -f
real? ? 0m0.152s user? ? 0m0.016s sys? ?? 0m0.146s
#查找/var/log/ 類型是文件的,并且名稱是.log結(jié)尾的,并且7天以前 的,然后刪除 [root@oldboyedu ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -exec rm -f {} \; [root@oldboyedu ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -delete [root@oldboyedu ~]# #find /var/log/ -type f -name "*.log" -mtime +7 | xargs rm -f
符號(hào) 作用 -a 與 -o 或 -not|! 非
3.記得文件的內(nèi)容是什么恨樟,但是不清楚文件名稱是什么,也不知道路 徑在哪,怎么辦?
4.find邏輯運(yùn)算符
5.find練習(xí)
#find 是查詢文件 #grep 過濾內(nèi)容
#將ifnd查詢的文件結(jié)果,作為grep的參數(shù) [root@oldboyedu ~]# find /etc/ -type f | xargs grep "log_group" --color=auto /etc/audit/auditd.conf:log_group = root
#1.查找當(dāng)前目錄下卤唉,屬主不是root的所有文件 [root@oldboyedu ~]# find /home/ ! -user root -ls [root@oldboyedu ~]# find /home/ -not -user root -ls? # 使用較少 ? ? ? ? #2.查找當(dāng)前目錄下,屬主屬于jack仁期,并且大小大于1k的文件 [root@oldboyedu ~]# find /home/ -type f -a -user jacky -a -size +1k
#3.查找當(dāng)前目錄下的屬主為root 或者 以xml結(jié)尾的普通文件 [root@xuliangwei ~]# find . -type f -a \( -user hdfs o -name '*.xml' \)
1.查找/var目錄下桑驱,屬主不是root竭恬,且文件名不以f開頭的文件 [root@oldboyedu ~]# find /var/ -type f? ! -user root a? ! -name "f*"
2.查找/var目錄下屬主為root,且屬組為mail的所有文件 [root@oldboyedu ~]# find /var/ -type f -user root -a group mail
3.查找/var目錄下不屬于root熬的、lp的所有文件 [root@oldboyedu ~]# find /var/ -type f ! -user root -a ! -user lp
4.查找/var目錄下最近一周內(nèi)產(chǎn)生的文件痊硕,同時(shí)屬主不為root,也不是 postfix的文件 [root@oldboyedu ~]# find /var/ -type f -mtime -7 ! user root ! -name "postfix"
5.查找/etc目錄下大于1M且類型為普通文件的所有文件 [root@oldboyedu ~]# find /etc/ -type f -size +1M
6.將/etc/中的所有目錄(僅目錄)復(fù)制到/tmp下押框,目錄結(jié)構(gòu)不變 [root@oldboyedu ~]# find /etc/ -type d -exec mkdir -p /tmp/{} \;
7.將/etc目錄復(fù)制到/var/tmp/,/var/tmp/etc的所有目錄權(quán)限 777/var/tmp/etc目錄中所有文件權(quán)限666 [root@oldboyedu ~]# cp /etc/ /var/tmp/ -rp [root@oldboyedu ~]# find /var/tmp/etc/ -type d? -exec chmod 777 {} \; [root@oldboyedu ~]# find /var/tmp/etc/ -type f? -exec chmod 666 {} \;
8.保留/var/log/下最近7天的日志文件,其他全部刪除 [root@oldboyedu ~]# find /var/log/ -type f -mtime +7? -exec rm -f {} \;
#2 今日總結(jié)
1.find 查找文件 文件名稱 文件大小 文件類型 修改時(shí)間 用戶用戶組 2.find處理動(dòng)作 -print 忽略 默認(rèn)送 -ls 長格式顯示,不能加參數(shù),所以忽略 -delete 刪除文件,刪除目錄必須確保目錄為空 -ok 執(zhí)行command命令,但會(huì)提示,忽略 -exec 執(zhí)行command命令
9.創(chuàng)建touch file{1..10}10個(gè)文件, 保留file9,其他一次全部刪除 [root@oldboyedu ~]# find ./ -type f -name "file*" ! name "file9" -exec rm -f {} \;
10.解釋如下每條命令含義 mkdir /root/dir1 touch /root/dir1/file{1..10} find /root/dir1 -type f -name "file5" find /root/dir1 ! -name "file5" find /root/dir1 -name "file5" -o -name "file9" find /root/dir1 -name "file5" -o -name "file9" -ls find /root/dir1 \( -name "file5" -o -name "file9" \) ls find /root/dir1 \( -name "file5" -o -name "file9" \) exec rm -vf {} \; find /root/dir1? ! \( -name "file4" -o -name "file8" \) -exec rm -vf {}? \;
3.find的邏輯運(yùn)算 與 -a 或 -o 非 ! | -not