1. 程式人生 > >2018.3.30 二周第五次課

2018.3.30 二周第五次課

find命令 文件名後綴

find命令 (搜索文件)
搜索文件命令還有which,whereis(不常用),locate
組合鍵解釋:
Ctrl+a  : 光標移動到命令最前面;
Ctrl+e:光標移到命令最後面;
Ctrl+L:清屏
Ctrl+u:刪除光標前面所有的命令
概念:find命令 就是用於搜索文件
命令格式: find 路徑 參數
參數:
-atime +n和-n :表示訪問或執行時間大於或小於n天的文件。
-ctime +n和-n :表示寫入、更改iNode屬性(如更改所有者、權限或者鏈接)的時間大於或小於n天的文件。
-mtime +n和-n :表示寫入時間大於或小於n天的文件,該參數用的最多。

find使用到的命令:

  • -type 通過文件類型查找文件 例: -type f 以文件類型找到
  • -mtime 表示幾天內 或者幾天前的文件,mtime -1 一天內; mtime +10 10天前
  • -size 表示文件字節選擇列出來的文件 例:-size +10k 大於10k的文件。
  • -o 和,例:-type f -o -mtime -1 查看以文件類型和小於一天的文件。
  • -exec 推動後面的命令,例: find /root/ -type f -mmin -120 -exec ls -l {} \; 把前面的命令 以ls -l列出來。

find命令的使用,例:如果知道在哪個目錄下,然後知道文件名字,如何查找呢? find /etc/ -name "sshd_config"

[root@zhangzhen-01 ~]# find /etc/ -name "sshd_config" #在這裏find是搜索,-name空格後面雙引號跟文件全面查找如下
/etc/ssh/sshd_config

如果你只知道大概文件名,可以用模糊查找 find /etc/ -name "sshd※"

[root@zhangzhen-01 ~]# find /etc/ -name "sshd*" #在這裏,前面格式一樣,雙引號裏面sshd後面跟的是“※號”,表示通配符,以sshd開頭的文件全部列出來。
/etc/ssh/sshd_config
/etc/systemd/system/multi-user.target.wants/sshd.service

/etc/sysconfig/sshd
/etc/pam.d/sshd

在這裏我們可以用 -type ,表示通過文件類型查找文件

概念:-type filetype 表示通過文件類型查找文件。如下幾種文件類型
d 表示該文件為目錄;
f 表示該文件為普通文件
l 表示該文件為鏈接文件 (如軟鏈接 硬鏈接)
b 表示該文件為塊設備
c 表示該文件為串行端口設備文件
s 表示該文件為套接字文件
該命令格式如下: find /etc/ -type d -name "sshd*" #查找文件為目錄的sshd*所有文件

stat 獲取一個文件或者目錄的信息(如文件的大小,什麽樣的文件或者目錄,包括權限及uid,還有最近訪問(atime access time),最近更改的時間(mtime modify time)、最近改動的時間(ctime change ctime change)和創建的時間。)

概念:這個stat命令主要就是看最近訪問(atime)、最近更改(mtime)、和最近改動的時間(ctime)創建時間的。

使用命令如下:

[root@zhangzhen-01 ~]# stat dior
文件:"dior"
大小:32 塊:0 IO 塊:4096 目錄
設備:803h/2051d Inode:499801 硬鏈接:2
權限:(0755/drwxr-xr-x) Uid:( 0/ root) Gid:( 1001/ user1)
環境:unconfined_u:object_r:admin_home_t:s0
最近訪問:2018-03-29 00:31:52.739168672 +0800
最近更改:2018-03-28 22:44:38.990222665 +0800
最近改動:2018-03-29 00:31:40.627056046 +0800
創建時間:-

做個示例,搜索小於1天內,在/etc目錄下,最近訪問的塊設備有哪些。

[root@zhangzhen-01 ~]# find /etc/ -type b -atime -1
[root@zhangzhen-01 ~]# find /etc/ -type f -atime -1
/etc/resolv.conf
/etc/pki/tls/openssl.cnf
/etc/pki/nss-legacy/nss-rhel7.config
/etc/rpm/macros.dist
/etc/yum.repos.d/CentOS-Base.repo
/etc/yum.repos.d/CentOS-CR.repo
/etc/yum.repos.d/CentOS-Debuginfo.repo
/etc/yum.repos.d/CentOS-Media.repo

在這裏,find表示搜索, 空格 後面跟需要搜索的文件,空格 -type(表示通過文件類型查找文件) 空格 後面 b(表示塊設備)空格 -atime(最近訪問)-1 (小於一天,可以用“+”多少天以前的,“-”表示多少天以內的)

“-o” 表示或者,使用方法如下:

[root@zhangzhen-01 ~]# find /etc/ -type f -o -mtime -1 -0 -name "*.com"

如何搜索硬鏈接,如何搜索iNode文件都有什麽。

[root@zhangzhen-01 ~]# ln anaconda-ks.cfg /tmp/23.txt.heh #做個硬鏈接到/tmp下創建個23.txt.heh的文件
[root@zhangzhen-01 ~]# ls -l anaconda-ks.cfg #下面的內容就是硬鏈接的文件
-rw-------. 2 root root 6901 3月 27 22:40 anaconda-ks.cfg
[root@zhangzhen-01 ~]# ls -i /tmp/23.txt.heh #查看剛硬鏈接的iNode號
33574978 /tmp/23.txt.heh
[root@zhangzhen-01 ~]# find / -inum 33574978 #查看iNode號的文件都有哪些
/root/anaconda-ks.cfg
/tmp/23.txt.heh

如何用find查看2小時之內的文件

[root@zhangzhen-01 ~]# find /root/ -type f -mmin -120 **#-mmin後面跟分鐘數
/root/.bash_history
/root/dior/1.txt

如果利用find把查看的文件直接ls -l出來,格式如下:

root@zhangzhen-01 ~]# find /root/ -type f -mmin -120 -exec ls -l {} \;
-rw-------. 1 root root 9759 3月 31 00:22 /root/.bash_history
-rw-r--r--. 1 root root 15 3月 30 23:39 /root/dior/1.txt

註:-exec也是find的一種格式,後面跟你要對文件進行的命令, {} 花括號表示ls -l出來的內容, \;反斜杠+分號只是讓他執行這個命令,推動出來。

如何利用find給之前創建的文件批量改後綴名呢,命令格式如下: -mmin

[root@zhangzhen-01 ~]# find /root/ -type f -mmin -300
/root/.bash_history
/root/dior/1.txt
[root@zhangzhen-01 ~]# find /root/ -type f -mmin -120 -exec mv {} {}.bak \;
[root@zhangzhen-01 ~]# find /root/ -type f -mmin -300
/root/dior/1.txt.bak
/root/.bash_history.bak

註:第二個花括號表示所每一個文件。

如何用find把大於10K的文件列出來。 -size +10k

[root@zhangzhen-01 ~]# find /root/ -type f -size +10k -exec ls -lh {} \;
-rwxr-xr-x. 2 root root 37K 8月 4 2017 /root/dior/login

註:在這裏,小於就用“-”號,後面跟單位字節。

文件名後綴

概念:在linux裏,命令有區分大小寫。
後綴名可自定義,不過一般都是分類進行區別,方便大家查找。
參考文獻:https://blog.csdn.net/fushaonian/article/details/72782651

2018.3.30 二周第五次課