一、文件查找
1.locate命令: locate KEYWORD 常用選項: -i 執行區分大小寫的搜索 -n N只列舉前N個匹配項目 查詢系統上預建的文件索引數據庫在:/var/lib/mlocate/mlocate.db上,由于事先建立索引,所以查找速度快。 2.find命令: 實時查找工具,通過遍歷指定路徑完成文件查找,查詢的速度稍微慢點,精確查找,實時查找??赡苤凰阉饔脩艟邆渥x取和執行權限的目錄。 find - search for files in a directory hierarchy find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression] find [OPTION]... [查找路徑] [查找條件] [處理動作] 查找路徑:指定具體目標路徑;默認為當前目錄 查找條件:指定的查找標準,可以文件名、大小、類型、 權限等標準進行;默認為找出指定路徑下的所有文件 處理動作:對符合條件的文件做操作,默認輸出至屏幕 1)根據文件名和inode查找 -name "文件名稱":支持通配符 *,?[],[^] -iname "文件名稱":不區分大小寫 -inum n 按照inode號查找 2)根據屬主、屬組查找: -user UserName:按照用戶名查找 -group GroupName:按照組名查找 -uid:按照Uid進行檢索 -gid:按照gid進行檢索 -nouser:查找沒有屬主的文件 -nogroup:查找沒有屬組的文件 3)文件類型查找 -type: 根據文件類型查找 f: 普通文件 d: 目錄 b: 塊設備 c: 字符設備 l: 符號鏈接文件 p: 命名管道 s: 套接字 4)組合條件(摩根定律) -a: 與,同時滿足 -o: 或, -not, !:非,取反 非A,并且 非B: 非(A或B) -not ( -user hadoop -o -iname "*.txt" ) 非A,或 非B: 非(A且B) 實例: !A -a !B = !(A -o B) !A -o !B = !(A -a B) find -name "file.txt" find -iname "file.txt" find / -name "*.txt" find /var/ -name "*log*" find -user alren -group gentoo find -user chen -not group chen find -user gentoo -o -user archlnux find /-user alren -o -uid 1001 find -not \(-user chen -o -user alren\)等價于find -not -user chen -a -not -user alren 5)文件大小查找 -size [+|-]#UNIT 常用單位:k, M, G #UNIT: (#-1, #] 如:6k 表示(5k,6k] -#UNIT:[0,#-1] 如:-6k 表示[0,5k] +#UNIT:(#,∞) 如:+6k 表示(6k,∞) 6)時間戳查找 根據時間戳: 以“天”為單位; 如下#=1為例 -atime [+|-]#, #: [#,#+1) find /etc/ -atme 1 :表示大于等于1天,小于2天時間段被訪問過; +#: [#+1,∞] find /etc/ -atime +1 :表示一天之外包括一天被訪問過 -#: [0,#) find /etc/ -atime -1 :表示一天前被訪問過; -mtime -ctime 以“分鐘”為單位: -amin -mmin -cmin 7)根據權限查找: -perm [+|-]MODE MODE:精確匹配 +MODE: 任何一類用戶的任何一位權限匹配;常用于查找某類用戶的某特定權限是否存在(cetos6.x); /MODE:任何一類用戶(u,g,o)對象的權限中只要能一位匹配即可,或關系,+ 從centos7開始淘汰 -MODE: 每類用戶的指定要檢查的權限位都匹配,則為或關系; find -perm 755 會匹配權限模式恰好是755的文件 只要當任意人有寫權限時,find -perm +222就會匹配 如: find /var/ -perm +222 或者find /var/ -perm /222 只要任意用戶有寫的權限時即可被匹配 只有當每個人都有寫權限時,find -perm -222才會匹配 如:find -perm -222 只有所有人都有讀的權限時,才能被匹配到 只有當其它人(other)有寫權限時,find -perm -002才 會匹配 如:find /var/ -perm -002 只有其他人有讀的權限時才能匹配到 處理動作: -print:默認的處理動作,顯示值屏幕 -ls:類似于對查找到的文件執行ls -l命令 -delete:刪除查找到的文件 -exec COMMAND {} \;對查找到的每個文件執行由command指定的命令 有些命令不能接受過多參數,此時命令執行可能會失敗,下面方式可規避此問題 find |xargs COMMAND FIDN實例: find -name "*.conf" -exec cp {} {}.com \; #備份配置文件,并改名 find /tmp -ctime +3 -user chen -ok -rm {} \;#提示刪除存在時間超過三天以上的chen用戶臨時文件 find ~ -perm -002 -exec chmod o-w {} \; #在你的主目錄中尋找可被其它用戶寫入的文件 find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} \; find /home –type d -|xargs rm -rf
二、壓縮、解壓縮及其歸檔工具
gzip命令:
1)gzip命令: gzip [OPTION]... FILE ... 選項: -d:解壓縮,相當于gunzip -c:將壓縮或解壓縮的結果輸出至標準輸出 [root@centos7 ~]# gzip -c awk.txt >awk.gz [root@centos7 ~]# ls awk.gz awk.txt passwd test.sh test.x [root@centos7 ~]# gzip -c passwd >passwdddddd.gz [root@centos7 ~]# ls awk.gz awk.txt passwd passwdddddd.gz test.sh test.x [root@centos7 ~]# gunzip passwdddddd.gz [root@centos7 ~]# ls awk.gz awk.txt passwd passwdddddd test.sh test.x [root@centos7 ~]# gzip -d awk.gz [root@centos7 ~]# ls awk awk.txt passwd passwdddddd test.sh test.x [root@centos7 ~]#
bzip2命令:
2)bzip2命令: bzip2 [OPTION]... FILE ... -k: keep, 保留原文件 -d:解壓縮 [root@centos7 ~]# ls awk awk.txt passwd passwdddddd test.sh test.x [root@centos7 ~]# bzip2 -k passwd [root@centos7 ~]# ls awk awk.txt passwd passwd.bz2 passwdddddd test.sh test.x [root@centos7 ~]# bzip2 passwdddddd [root@centos7 ~]# ls awk awk.txt passwd passwd.bz2 passwdddddd.bz2 test.sh test.x [root@centos7 ~]# bunzip2 passwdddddd.bz2 [root@centos7 ~]# ls awk awk.txt passwd passwd.bz2 passwdddddd test.sh test.x [root@centos7 ~]# bunzip2 -d passwd.bz2 bunzip2: Output file passwd already exists. [root@centos7 ~]# ls awk awk.txt passwd passwd.bz2 passwdddddd test.sh test.x [root@centos7 ~]# mv passwd passwd1 [root@centos7 ~]# bunzip2 -d passwd.bz2 [root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd test.sh test.x [root@centos7 ~]#
zip命令:
3)zip命令: zip - package and compress (archive) files 1)打包 zip 打包后的文件名 要打包的文件 zip passwd.zip passwd 2)解壓 unzip .zip結尾的文件 unzip passwd.zip root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd test.sh test.x [root@centos7 ~]# zip -r passwd.zip passwd adding: passwd (deflated 63%) [root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd passwd.zip test.sh test.x [root@centos7 ~]# unzip passwd.zip Archive: passwd.zip replace passwd? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: passwd [root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd passwd.zip test.sh test.x [root@centos7 ~]# rm -f passwd [root@centos7 ~]# unzip passwd.zip Archive: passwd.zip inflating: passwd [root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd passwd.zip test.sh test.x
tar命令:
歸檔工具: tar [options] -f file.tar File1 ... -c: 創建歸檔 -x: 展開歸檔 -t: 不展開而直接查看被歸檔的文件 -v:顯示創壓縮或解壓縮過程 tar -cvf archive.tar file1 創建一個非壓縮的 tarball tar -cvf archive.tar file1 file2 dir1 創建一個包含了'file1','file2'以及'dir1'的檔案文件 tar -tf archive.tar 顯示一個包中的內容 tar -xvf archive.tar 釋放一個包 tar -xvf archive.tar -C /tmp 將壓縮包釋放到 /tmp目錄下 tar -cvfj archive.tar.bz2 dir1 創建一個bzip2格式的壓縮包 tar -xvfj archive.tar.bz2 解壓一個bzip2格式的壓縮包 tar -cvfz archive.tar.gz dir1 創建一個gzip格式的壓縮包 tar -xvfz archive.tar.gz 解壓一個gzip格式的壓縮
[root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd passwd.zip test.sh [root@centos7 ~]# tar -zcvf passwd.tar.gz passwd passwd [root@centos7 ~]# ls awk awk.txt passwd passwd1 passwdddddd passwd.tar.gz passwd.zip test.sh [root@centos7 ~]# tar -jcvf passwd.tar.bz2 passwd passwd [root@centos7 ~]# ls awk passwd passwdddddd passwd.tar.gz test.sh awk.txt passwd1 passwd.tar.bz2 passwd.zip [root@centos7 ~]# tar -zxvf passwd.tar.gz passwd [root@centos7 ~]# ls awk passwd passwdddddd passwd.tar.gz test.sh awk.txt passwd1 passwd.tar.bz2 passwd.zip [root@centos7 ~]# rm -f passwd [root@centos7 ~]# tar -zxvf passwd.tar.gz passwd [root@centos7 ~]# ls awk passwd passwdddddd passwd.tar.gz test.sh awk.txt passwd1 passwd.tar.bz2 passwd.zip [root@centos7 ~]# rm -f passwd [root@centos7 ~]# tar -jxvf passwd.tar.bz2 passwd [root@centos7 ~]# ls awk passwd passwdddddd passwd.tar.gz test.sh awk.txt passwd1 passwd.tar.bz2 passwd.zip
三、實戰小練習
1、查找/var目錄下屬主為root,且屬組為mail的所有文件 [root@centos7 ~]# find /var/ -user root-a -group mail /var/spool/mail /var/spool/mail/root /var/spool/mail/rooter [root@centos7 ~]# 2、查找/var目錄下不屬于root、lp、gdm的所有文件 [root@centos7 ~]# find /var/ -not \( -userroot -o -user lp -o -user gdm \)|wc -l 125 [root@centos7 ~]# find /var/ -not -userroot -a -not -user lp -a -not -user gdm |wc -l 125 [root@centos7 ~]# 3、查找/var目錄下最近一周內其內容修改過,同時屬主不為 root,也不是postfix的文件 [root@centos7~]# find /var/ -mtime -7 -a -not -user root -a -not -user postfix /var/lib/setroubleshoot/setroubleshoot_database.xml /var/spool/abrt/ccpp-2016-07-19-15:47:15-3568 [root@centos7 ~]# find /var/ -mtime -7 -not \( -user root -o -user postfix \) /var/lib/setroubleshoot/setroubleshoot_database.xml /var/spool/abrt/ccpp-2016-07-19-15:47:15-3568 [root@centos7 ~]# 4、查找當前系統上沒有屬主或屬組,且最近一個周內曾被訪 問過的文件 [root@centos7 ~]# find / \( -nouser -o-nogroup \) -a -atime -7 find: ‘/proc/8419/task/8419/fd/6’: No suchfile or directory find: ‘/proc/8419/task/8419/fdinfo/6’: Nosuch file or directory find: ‘/proc/8419/fd/6’: No such file ordirectory find: ‘/proc/8419/fdinfo/6’: No such fileor directory [root@centos7 ~]# 5、查找/etc目錄下大于1M且類型為普通文件的所有文件 [root@centos7 ~]# find /etc/ -size +1M -type f /etc/selinux/targeted/policy/policy.29 /etc/udev/hwdb.bin /etc/brltty/zh-tw.ctb [root@centos7 ~]# 6、查找/etc目錄下所有用戶都沒有寫權限的文件 [root@centos7 ~]# find /etc/ -not -perm /222 [root@centos6 ~]# find /etc/ -not -perm +222 [root@centos7 ~]# find /etc/ -not -perm /222 |wc -l 23 [root@centos6 ~]# find /etc/ -not -perm +222 |wc -l 23 [root@centos6 ~]# 7、查找/etc目錄下至少有一類用戶沒有執行權限的文件 [root@centos7 ~]# find /etc/ -not-perm -111 8、查找/etc/init.d目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件 [root@centos7 ~]# find /etc/init.d/ -perm-113
小編總結也辛苦,如果覺得還可以,順手點個贊,哈哈:)…….
原創文章,作者:alren,如若轉載,請注明出處:http://www.www58058.com/36633