1. 程式人生 > >二周第五次課(2月2日)

二周第五次課(2月2日)

設備 就會 權限 chm 1.5 同文件 二進制 $path 表壓縮

2.23/2.24/2.25 find命令

2.26 文件名後綴



2.23 find命令(上)


!!回顧

#which ls

查找命令的絕對路徑

#echo $PATH

which所找的東西都會在環境變量$PATH裏尋找。


#whereis ls 查找文件在哪兒

ls:/usr/bin/ls /usr/share/man/man1/ls.1.gz


#yum install -y mlocate 安裝#locate命令


#locate 模糊搜索

用#locate之前先手動#updatedb(手動更新數據庫,數據庫每天會在4:00AM自動更新)

用法 #locate 123

此用戶會把123相關的文件和目錄都會搜索出來。


ctrl d 退出終端,exit;logout

ctrl c 退出當前窗口

ctrl u 光標前的都刪掉

ctrl n 把光標摞到最後

ctrl a 前把光標摞到最前


#find 用法

#find /etc/ -name "sshd_config" 精確搜索

#find /etc/ -name "sshd*" 迷糊搜索

#find /etc/ type d -name "sshd*" 目錄迷糊搜索

#find /etc/ type f -name "sshd*"文件迷糊搜索


d目錄

f(-)二進制、普通文件

s stick

c 字符串 鼠標、鍵盤

b 塊狀存儲設備。硬盤,u盤

l 鏈接文件 軟硬鏈接。



2.24 find命令(中)


#find / -type -name -(mtime,ctime,atime)


mtime 表示寫入時間大於或小於n天的文件,該參數用得最多。

atime 表示訪問或執行大於或小於n天的文件。

ctime 表示寫入、更改inode屬性(如更改所有者、權限或者鏈接)的時間大於或小於n天的文件。


#stat 2.txt

技術分享圖片

可以看到最近訪問時間(atime)、最近更改時間(mtime)、最近改動時間(ctime)


#chmod 700 2.txt

#stat 2

可以發現2.txt的改動時間被改了

技術分享圖片

#echo"12121212" >> 2.txt

#stat 2.txt

可以發現2.txt的更改時間被改了。


知識點*

如果更改了文件的內容,ctime一定會變,因為ctime記錄了文件的大小(標紅)、時間、內容、權限:所有者、所屬組。此時,增加了文字內容,那麽大小就會變了,所以ctime也跟著變。


用法

#find / -type f -mtime +1或者-1

解析:mtime 後面接的是時間,+增-減,時間默認單位是天。

例如

查找在/etc/在一天以內發生更改的文件,

#find /etc/ -type f -mtime -1

那麽,一天之前(+1)的用法是

#find /etc/ -type f -mtime +1


並且(增加條件)的用法

#find /etc/ -type f -ctime -1 -name "*.conf"


或者的用法(-o)

#find /etc/ -type f -o -mtime -1 -o -name "*.conf"


關於#find常用參數:-type -name -mtime -o



2.25 find命令(下)


硬鏈接文件如何找?

test0,

先把硬鏈接文件放到/tmp下 並改名1.txt.bak

# ln 1_heard.txt /tmp/1.txt.bak

再查看 1_heard.txt的 inode號

#ls -i 1_heard.txt

......395 1_heard.txt

#find / -inum ....395

/root/1_heard.txt

/tmp/1.txt.bak


在/root下find一個小時內修改過的文件。-mmin後面帶分鐘,-60 代表60分鐘。

#find /root/ -type f -mmin -60


在/root下find一個小時內修改過的文件(-mmin 60),並且ls -l 顯示其中一個。(-exec 連接語段,{}表示其中一個,如果兩個是{}{},\;連著用。exec一般是-exec 命令{}\;)。

#find /root/ -type f -mmin -120 -exec ls -l {} \;


test

找出mtime 150的文件,並修改他們的後綴為.bak

#find /root/ -type f -mmin -150

#find /root/ -type f -mmin -150 -exec mv {}{}.bak \;


test 2

找出大於10k的文件

#find /root/ -type f -size +10k -exec ls -lh {}{} \;



2.26 文件名後綴


Linux的命令和文件,是區分大小寫的。

Linux的文件是有後綴名的,但並不是不同後綴就代表不同文件,加與不加都是無所謂的。

但是為了便於區分,我們習慣在定義文件名時加一個後綴名。這樣當用戶看到這個文件名時,就會很快知道它到底是一個什麽文件,例如1.sh、2.tar.gz、my.cnf、test.zip等。

1.sh 腳本文件

2.tar.gz代表壓縮包

my.cnf 配置文件

test.zip 壓縮文件。


二周第五次課(2月2日)