標籤:linux
1. find 尋找某個時間段的檔案
在一個目錄下,有很多很多檔案,只想找到12月1日至12月31日的檔案,如何找呢?
先手動找到在這個時間段最早的檔案,比如1.txt是12月1日0點1分建立的,再找到這個時間段最晚的檔案,比如2.txt是12月31日23點55分建立的;
find . -type f -newer 1.txt ! -newer 2.txt
不去手動找,參考下面:
其實也就是修改檔案的建立日期(mtime)。 touch就有這個功能。
例 建立一個2012年10月8日的檔案
touch -m -t 1210080000 1.txt
然後stat 1.txt
發現除了Modify 那一行為2012年10月8日0點0分外,其他兩項均為當前日期和時間
還可以
touch -c -t 1210080000 2.txt
stat 2.txt
Access 和 Modify 均為2012年10月8日0點0, Change 為當前日期和時間
2. 建立指定日期的檔案
其實也就是修改檔案的建立日期(mtime)。 touch就有這個功能。
例 建立一個2012年10月8日的檔案
touch -m -t 1210080000 1.txt
然後stat 1.txt
發現除了Modify 那一行為2012年10月8日0點0分外,其他兩項均為當前日期和時間
還可以
touch -c -t 1210080000 2.txt
stat 2.txt
Access 和 Modify 均為2012年10月8日0點0, Change 為當前日期和時間
3. 用find時排除某些目錄或者檔案
1. 只排除一個目錄或者檔案,如尋找/tmp/ 目錄下所有檔案(不包含目錄), 並且不包含目錄123
find /tmp/ -path "/tmp/123" -prune -o -type f -print
2. 排除兩個或者多個目錄或者檔案,如尋找/tmp/ 目錄下所有檔案(不包含目錄), 並且不包含目錄123和目錄234和目錄345
find /tmp/ \( -path "/tmp/123" -o -path "/tmp/234" -o -path "/tmp/345" \) -prune -o -type f -print
注意,如果是尋找目錄時,-path 後面的目錄名一定不要帶/ 如 寫成 -path "/tmp/123/" 就錯了,而尋找檔案時,帶/ 沒有問題。
4. 為你的linux增加自訂PATH
把mysql安裝在 /usr/local/mysql 下。如果要是用mysql這個命令,我們不得不敲絕對路徑/usr/local/mysql/bin/mysql,這樣太費勁。
那麼就把 /usr/local/mysql/bin 加入到 PATH變數中。
vi /etc/profile
在最後面加入
export PATH=$PATH:/usr/local/mysql/bin
報存後,運行命令
source /etc/profile
檢查PATH變數
echo $PATH
這樣就可以直接輸入mysql 了。
5. linux下檔案的特殊許可權s和t
先看看這兩個檔案的許可權:
[[email protected] ~]# ls -ld /usr/bin/passwd /tmp
drwxrwxrwt 4 root root 4096 Jun 2 17:33 /tmp
-rwsr-xr-x 1 root root 22984 Jan 7 2007 /usr/bin/passwd
這裡的s和t是針對執行許可權來講的。
這個s許可權,是為了讓一般使用者臨時具有該檔案所屬主/組的執行許可權。就比如/usr/bin/passwd在執行它的時候需要去修改/etc/passwd和/etc/shadow等檔案,這些檔案除了root外,其他使用者都沒有寫入權限,但是又為了能讓普通使用者修改自己的密碼,只能時臨時讓他們具有root的許可權。所以這個s許可權就是用來完成這個特殊任務的。s許可權只能應用在二進位的可執行檔上。
如果你不想讓普通使用者修改自己的密碼,只需要
[[email protected] ~]# chmod u-s /usr/bin/passwd 或者
[[email protected] ~]# chmod 0755 /usr/bin/passwd
0755最前面的0表示不使用任何特殊許可權,該位上的數字可以是0,1(--t),2(-s-),3(-st),4(s--),5(s-t),6(ss-),7(sst)
那個t許可權只針對目錄生效,它表示只能讓所屬主以及root可以刪除(重新命名/移動)該目錄下的檔案。比如/tmp目錄本來就是任何使用者都可以讀寫,如果別人可以任意刪除(重新命名/移動)自己的檔案,那豈不是很危險。所以這個t許可權就是為瞭解決這個麻煩的。下面舉一個例子,說明一下這個許可權的用法:
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# mkdir test
[[email protected] tmp]# chmod 1777 test
[[email protected] tmp]# ls -ld test
drwxrwxrwt 2 root root 4096 Jun 2 18:10 test
[[email protected] tmp]# su test1
[[email protected] tmp]$ touch test/1.txt
[[email protected] tmp]$ ls -l test
total 4
-rw-r--r-- 1 test1 test 0 Jun 2 18:12 1.txt
[[email protected] tmp]$ exit
[[email protected] tmp]# su www
[[email protected] tmp]$ ls -l test/1.txt
-rwxrwxrwx 1 test1 test 6 Jun 2 18:12 test/1.txt
[[email protected] tmp]$ rm test/1.txt
rm: cannot remove `test/1.txt‘: Operation not permitted
提示不能刪除1.txt
[[email protected] tmp]$ exit
[[email protected] tmp]# chmod -t test
去掉t許可權。
[[email protected] tmp]# ls -ld test
drwxrwxrwx 2 root root 4096 Jun 2 18:13 test
[[email protected] tmp]# su www
[[email protected] tmp]$ rm -f test/1.txt
再次刪除,則刪除成功。
[[email protected] tmp]$ ls test/1.txt
ls: test/1.txt: No such file or directory
6. locate 命令報錯
docate /bin/ls
warning: locate: could not open database: /var/lib/slocate/slocate.db: No such file or directory
warning: You need to run the ‘updatedb‘ command (as root) to create the database.
Please have a look at /etc/updatedb.conf to enable the daily cron job.
這是提示我們,系統目前沒有建立slocate 資料庫。
解決辦法: 輸入命令 updatedb 建立資料庫
然後需 vim /etc/updatedb.conf
讓 DAILY_UPDATE=yes
7. find -name 尋找多個檔案
find / -name 1.txt -o -name 2.txt -o -name 3.txt ...
8. find 尋找指定許可權的檔案
這就用到了 -perm 選項,具體用法是這樣的:
1. -perm mode (比如: -perm 775)
解釋:775 前面沒有橫線是代表只要找到百分之百一樣的許可權才算
好比說一個檔案是 -rwxrwxr-x (775)就是100%的match
但是另一個檔案是 -rwxrwxrwx (777)不是100%的match,others 多了寫的許可權就不是100%,所以不match!
2. -perm -mode (例如: -perm -775)
解釋:-775 前面有橫線表示只要標示的許可權match,其他的無所謂,所以上面的第二個檔案(777的)在這個case也會match的。
關於find命令中-perm中+- 的含義
舉個例子:比如目前的目錄下有a b c d4個檔案
a檔案的許可權為6000 也就是a檔案僅有suid sgid的特殊許可權
b檔案的許可權為2000 也就是b檔案僅有sgid的特殊許可權
c檔案的許可權為4000 也就是c檔案僅有suid的特殊許可權
d檔案的許可權為6600 也就是d檔案有sgid suid的特殊許可權並且該檔案擁有者對該檔案有讀寫權限
那麼現在find . -type f -perm 6000 可以找到a檔案,因為a檔案許可權為6000
那麼現在find . -type f -perm -6000 可以找到a d兩個檔案,這是因為:
我們可以先將a b c d這4個檔案的許可權轉化為2進位那麼,
a許可權轉為2進位後為 110 000 000 000
b許可權轉為2進位後為 010 000 000 000
c許可權轉為2進位後為 100 000 000 000
d許可權轉為2進位後為 110 110 000 000
在 find . -type f -perm -6000 中的6000許可權轉為2進位為110 000 000 000,那麼6000前的-號代表缺一不可,也就是如果有1的地方必須有1,那麼這裡找-6000許可權的檔案,這6000許可權裡前面有2個位置都是1,所以這裡find找-6000許可權的檔案就是找前面2個位置都是1的檔案.而只有a d這兩個檔案前2個位置都是1,所以find . -type f -perm -6000 只會找到a d兩個檔案.
find . -type f -perm +6000會找到a b c d這4個檔案,這是因為:
+6000 裡的這個+號代表有1即可,也就是有1的位置,任何位置只要有1就可以.那麼這裡找+6000許可權的檔案,這6000許可權前面2個位置都有1,所以這裡find 找+6000許可權的檔案就是找前面2個位置只要有一個位置有1的檔案就可以了,這4個檔案都符合要求所以最後都能被 find . -type f -perm +6000找到
我們使用man查詢find的協助文檔時,發現 -perm 還有一個 /modle 的格式,其實它等同於 +modle, 它建議我們使用 / 而不使用 +, 因為+ 容易產生混淆。所以以後您見到 find -perm /modle 這樣的格式時,就把它看成是+modle 即可。
命 令 習 題 集 ( 三 )