1. 程式人生 > >C語言再學習 -- Linux下find命令用法

C語言再學習 -- Linux下find命令用法

linux下查詢檔案的命令有兩個:locate 和 find

首先說一下locate:

這個命名是對其生成的資料庫進行遍歷(生成資料庫的命令:uodatedb),這一特性決定了用locate查詢檔案速度很快,但是locate命令只能對檔案進行模糊匹配,在精度上來說差了點,簡單介紹下它的兩個選項:

#locat

-i    查詢檔案的時候不區分大小寫,如: locate -i passwd

-n    只顯示查詢結果的前N行,如: locate -n 5 passwd

[email protected]:~# locate -n 5 passwd
/etc/passwd
/etc/passwd-
/etc/cron.daily/passwd
/etc/init/passwd.conf
/etc/pam.d/chpasswd

下面重點說下find:

find在不指定查詢目錄的情況下是對整個系統進行遍歷查詢的。

使用格式: find [指定查詢目錄] [查詢規則] [查詢完後執行的action]

[指定查詢目錄]

[email protected]:~# find /etc /tmp /root -name passwd
/etc/cron.daily/passwd
/etc/passwd
/etc/pam.d/passwd

這裡要注意的是目錄之間要用空格分開

[查詢規則]

1)根據檔名查詢

-name    根據檔名查詢 (精確查詢)

-iname   根據檔名查詢,但是不區分大小寫

[email protected]
:~# find /etc -name "passwd*" /etc/init/passwd.conf /etc/cron.daily/passwd /etc/passwd- /etc/passwd /etc/pam.d/passwd
[email protected]:~# find /etc -name "passwd?"
/etc/passwd-
[email protected]:~# find /home/test -name "[ab].sh"
/home/test/a.sh
/home/test/b.sh

檔名通配:

*     表示通配任意的字元

?     表示通配任意的單個字元

[]    表示通配括號裡面的任意一個字元

2)根據檔案所屬使用者和組來查詢檔案

-user         根據屬主來查詢檔案

-group     根據屬組來查詢檔案

[email protected]:/opt# ls -la 
總用量 32
drwxr-xr-x  8 root root  4096 10月 21 11:28 .
drwxr-xr-x 23 root root  4096  9月 20 13:18 ..
drwxrwxr-x  3 zslf zslf  4096  9月 27 13:03 ethtool-4.6
drwxr-xr-x  4 root root  4096  9月 28 10:46 file
drwxr-xr-x  3 root root  4096  9月  5 19:14 hisi-linux
drwxr-xr-x  8  501 users 4096  9月 21 19:21 i2c-tools-3.0.1
drwxr-xr-x  9 root root  4096 10月 28 14:43 mpp
drwxr-xr-x 20 root root  4096  9月 19 15:47 rootfs_uclibc
[email protected]:/# find ./ -user zslf
./opt/ethtool-4.6
[email protected]:/# find ./ -group zslf
./opt/ethtool-4.6
3)通過使用者和組來查詢

-uid          根據使用者查詢

-group     根據使用者組查詢

列出目錄內使用者的識別碼大於500的檔案或目錄
[email protected]:/mnt/test# find  ./ -uid  +500
./
./hh
列出目錄內組id為500的檔案或目錄
[email protected]:/mnt/test# find  ./ -gid  +500
./
./hh
4)-a 、 -o 、 -not 的使用
[email protected]:/mnt/test# ls -la
總用量 8
drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 .
drwxr-xr-x 5 root root 4096 11月 24 17:20 ..
-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 a.sh
-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 b.sh
-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 c.sh
-rw-r--r-- 1 root root    0 11月 25 09:45 f.sh
-rw-r--r-- 1 root root    0 11月 25 09:45 g.sh

-a 連線兩個不同的條件 (兩個條件必須同時滿足)

[email protected]:/mnt/test# find ./ -name "*.sh" -a -user zslf
./a.sh
./c.sh
./b.sh

-o 連線兩個不同的條件 (兩個條件滿足其一即可)
[email protected]:/mnt/test# find ./ -name "*.sh" -o -user zslf
./
./f.sh
./g.sh
./a.sh
./c.sh
./b.sh

-not 對條件取反的
[email protected]:/mnt/test# find ./ -not -user zslf
./f.sh
./g.sh

5)根據檔案時間戳的相關屬性來查詢檔案

我們可以使用 stat 命令來檢視一個檔案的時間資訊,如下:

[email protected]:/mnt/test# stat ./
  檔案:"./"
  大小:4096      	塊:8          IO 塊:4096   目錄
裝置:801h/2049d	Inode:291601      硬連結:2
許可權:(0755/drwxr-xr-x)  Uid:( 1000/    zslf)   Gid:( 1000/    zslf)
最近訪問:2016-11-25 09:45:24.699140785 +0800
最近更改:2016-11-25 09:45:22.255140690 +0800
最近改動:2016-11-25 09:45:22.255140690 +0800
建立時間:-

-atime、-mtime、-ctime、-amin、-mmin、-cmin

這裡的 -atime、-mtime、-ctime 分別對應的是 “最近一次訪問時間”,“最近一次內容修改時間”,“最近一次屬性修改時間”,這裡的atime的單位指的是“天”,amin 的單位是分鐘。

查詢在五天內沒有訪問過的檔案
[email protected]:/mnt/test# find ./ -atime +5
查詢在五天內訪問過的檔案
[email protected]:/mnt/test# find ./ -atime -5
./
./f.sh
./g.sh
./a.sh
./c.sh
./b.sh
6)根據檔案型別來查詢檔案

-type 

f    普通檔案

d   目錄檔案

l    連結檔案

b   塊裝置檔案

c    字元裝置檔案

p   管道檔案

s    socket檔案

[email protected]:/mnt/test# find ./ -type f
./f.sh
./g.sh
./a.sh
./c.sh
./b.sh
7)根據大小來查詢檔案

-size

#find  ./  -size   2M           //查詢在/tmp 目錄下等於2M的檔案
#find  ./  -size  +2M           //查詢在/tmp 目錄下大於2M的檔案
#find  ./  -size  -2M           //查詢在/tmp 目錄下小於2M的檔案

[email protected]:/mnt/test# find  ./  -size  -2M
./
./f.sh
./g.sh
./a.sh
./c.sh
./b.sh

8)根據檔案許可權查詢檔案

-perm

#find  ./  -perm   755         //查詢在/tmp目錄下許可權是755的檔案
#find  ./  -perm  +222       //表示只要有一類使用者(屬主,屬組,其他)的匹配寫許可權就行
#find  ./  -perm  -222        //表示必須所有類別使用者都滿足有寫許可權

[email protected]:/mnt/test# find ./ -perm +222
./
./f.sh
./g.sh
./a.sh
./c.sh
./b.sh

9)-nouser 和 -nogroup

-nouser     查詢無有效屬主的檔案

-nogroup  查詢無有效所屬組的檔案

#find  ./  -nogroup –a –nouser       
在整個系統中查詢既沒有屬主又沒有屬組的檔案(這樣的檔案通常是很危險的,作為系統工程師的我們應該及時清除掉)

[查詢完執行的action]

-print                             預設情況下的動作

-ls                                  查詢到後用 ls 顯示出來

-ok [commend]            查詢後執行命令的時候詢問使用者時候要執行

-exec [commend]        查詢後執行命令的時候不詢問使用者,直接執行

[email protected]:/mnt/test# ls -la
總用量 8
drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 .
drwxr-xr-x 5 root root 4096 11月 24 17:20 ..
-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 a.sh
-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 b.sh
-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 c.sh
-rw-r--r-- 1 root root    0 11月 25 09:45 f.sh
-rw-r--r-- 1 root root    0 11月 25 09:45 g.sh
更改許可權
[email protected]:/mnt/test# find ./ -name "*.sh" -exec chmod 777 {} \;
[email protected]:/mnt/test# ls -la
總用量 8
drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 .
drwxr-xr-x 5 root root 4096 11月 24 17:20 ..
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh
-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh
-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh
這裡注意  { } 的使用:替代查詢到的檔案。
重新命名
[email protected]:/mnt/test# find ./ -name "*.sh" -exec cp {} {}.old \;
[email protected]:/mnt/test# ls -la
總用量 8
drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 .
drwxr-xr-x 5 root root 4096 11月 24 17:20 ..
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 a.sh.old
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 b.sh.old
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 c.sh.old
-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 f.sh.old
-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 g.sh.old

刪除查詢到的超過30天沒有訪問過的檔案
[email protected]:/mnt/test# find ./ -atime +30 -exec rm -rf {} \;

也可以使用xargs來對查詢到的檔案進一步操作:
參看:C語言再學習 -- Xargs用法詳解

[email protected]:/mnt/test# ls -la
總用量 8
drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 .
drwxr-xr-x 5 root root 4096 11月 24 17:20 ..
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 a.sh.old
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 b.sh.old
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 c.sh.old
-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 f.sh.old
-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh
-rwxr-xr-x 1 root root    0 11月 25 10:17 g.sh.old

修改許可權
[email protected]:/mnt/test# find ./ -name "*.old" | xargs chmod 700
[email protected]:/mnt/test# ls -la
總用量 8
drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 .
drwxr-xr-x 5 root root 4096 11月 24 17:20 ..
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh
-rwx------ 1 root root    0 11月 25 10:17 a.sh.old
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh
-rwx------ 1 root root    0 11月 25 10:17 b.sh.old
-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh
-rwx------ 1 root root    0 11月 25 10:17 c.sh.old
-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh
-rwx------ 1 root root    0 11月 25 10:17 f.sh.old
-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh
-rwx------ 1 root root    0 11月 25 10:17 g.sh.old

相關推薦

C語言學習 -- Linuxfind命令用法

linux下查詢檔案的命令有兩個:locate 和 find 首先說一下locate: 這個命名是對其生成的資料庫進行遍歷(生成資料庫的命令:uodatedb),這一特性決定了用locate查詢檔案速度很快,但是locate命令只能對檔案進行模糊匹配,在精度上來說差了點,

C語言學習 -- 修改linux檔案許可權

Linux系統中的每個檔案和目錄都有訪問許可許可權,用它來確定誰可以通過何種方式對檔案和目錄進行訪問和操作。檔案或目錄的訪問許可權分為只讀,只寫和可執行三種。(0無許可權,1可執行,2可寫,4可讀)以檔

Linuxfind命令的使用

find為什麽要使用find命令? Linux系統中有著成千上萬的文件,如果你想要找到自己想要的文件,一款查找軟件是必不可少的,而locate是根據其生成的數據庫進行查找,雖然速度會略快,但非實時查找,有些新的文件或目錄是匹配不到的,而且locate是模糊匹配,而find命令為實時查找,且為精確匹配,如

C語言學習 -- 關鍵字struct(轉)

結構體的一般定義形式為: 標籤(tag)欄位允許為成員列表提供一個名字,這樣它就可以在後續的宣告中使用。標籤允許多個宣告使用同一個成員列表,並且建立同一種類型的結構。 struct 標籤{ 型別名1 成員名1; 型別名2 成員名2; …… 型別名n 成員名n;    }結構體變數;

C語言學習-定義變數

當我們在c語言裡建立一個變數的時候 int x = 5; int y = 6; 00C517B8 mov dword ptr [x],5 00C517BF mov dword ptr [y],6 實際上在彙編層面,

C語言學習5-陣列與優化

什麼是陣列?為什麼要用陣列? 通俗來講,在記憶體中一塊連續儲存資料的叫陣列,陣列的每個子元素的寬度都一樣,並且只能為通用的資料型別做單位(char,short,int等等) 讓我們先定義一個數組,然後賦值: char arr1[2] = { 0 }; arr1

C語言學習7-結構體

為什麼使用結構體? struct My { char name[20] = "如風斬嶽"; int age; char addr[50] ; int money; double Coordinates; //..... }; 當我們有這樣一種需求,

C語言學習--關鍵字

C語言一共有32個關鍵字,如下表所示: 關鍵字 說明 auto 宣告自動變數 short 宣告短整型變數或函式 int

C語言學習 -- 負數

有符號數的表示方法是由硬體決定,而不是由C決定的。有三種表示方法: 1、二進位制原碼 0000 0001  表示 1 1000 0001  表示 -1 這個方法有個缺點是有兩個零: +0 和 -0。這會引起混淆,而且用兩個位

C語言學習 -- 檔案

檔案是什麼 一個檔案(file)通常就是磁碟上的一段命名的儲存區。C 將檔案看成是連續的位元組序列,其中每一個位元組都可以單獨地讀取。 二進位制和文字模式 1、在windows系統中,文字模式

C語言學習 -- ASCII碼錶(轉)

ASCII碼錶第一部分:ASCII非列印控制字元表ASCII表上的數字0–31分配給了控制字元,用於控制像印表機等一些外圍裝置。例如,12代表換頁/新頁功能。此命令指示印表機跳到下一頁的開頭。(參詳ASCII碼錶中0-31)第二部分:ASCII列印字元數字 32–126 分配給了能在鍵盤上找到的字元,當您檢視

【 分類 】- C語言學習

專欄達人 授予成功建立個人部落格專欄

Linuxfind命令在根目錄查詢不到檔案

  你遇到過linux下root使用者執行find命令按檔名在根目錄下查詢不到指定檔案的情況嗎?如果你遇到這種情況,你分析可能有哪幾種原因導致?這裡記錄一下這個有意思的問題,包括問題場景和解決方法。 問題現象   實現一個工具,需要在root命令下執行普通使用者user1下的stop_

Linuxfind命令在根目錄查找不到文件

comm which user 搜索 and 返回 咨詢 問題解決 not 你遇到過linux下root用戶執行find命令按文件名在根目錄下查找不到指定文件的情況嗎?如果你遇到這種情況,你分析可能有哪幾種原因導致?這裏記錄一下這個有意思的問題,包括問題場景和解決方法。

C語言學習 -- 詳解C++/C 面試題 1

對這篇文章記憶猶新,因為之前找工作面試的時候,遇到過一家公司就是用的這套面試題。現在就結合考查的知識點和我總結完 C 語言再學習後的深入理解,來詳細的講講我對這篇文章的總結。 一、請填寫BOOL ,

C語言學習 -- 論記憶體管理

但現在看來,缺少示例。從新再寫一篇文章,著重介紹常見記憶體錯誤、跨函式使用儲存區。開始吧,再論記憶體管理!!發生記憶體錯誤是件非常麻煩的事情。編譯器不能自動發現這些錯誤,通常是在程式執行時才能捕捉到。而這些錯誤大多沒有明顯的症狀時隱時現增加了改錯的難度。一、常見的記憶體錯誤及

C語言學習 -- 記憶體管理

malloc ( )函式: malloc ( ) 向系統申請分配指定size個位元組的記憶體空間。返回型別是 void* 型別。void* 表示未確定型別的指標。C,C++規定,void* 型別可

C語言學習 -- 關鍵字typedef

一、typedef 介紹 typedef為C語言的關鍵字,作用是為一種資料型別定義一個新名字。比如人們常常使用 typedef 來編寫更美觀和可讀的程式碼。所謂美觀,意指 tepe

C語言學習 -- 時間函式

gmtime函式:可以把time函式得到的結果按照格林尼治時間轉換成一個結構體localtime函式:可以把time函式得到的結果按照當前時區轉換成一個結構體asctime函式:可以把一個記錄時間的結構體轉換成字串,一般與上兩個函式合用的格林時間,與北京時間換算,/* 時間函式演示 */ #

C語言學習 -- 論陣列和指標

之前有總結指標陣列,但是現在看來總結的太簡單了。好多重要的知識點都是一帶而過的。本想在後面新增後來想想算了,還是再寫一篇文章來詳細介紹陣列和指標這對冤家吧。一開始覺得C語言再學習專欄都寫了五十篇了,現在