茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1664719 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00017.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]在 Linux 下使用 find 指令查詢目錄與檔案的速查筆記

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]在 Linux 下使用 find 指令查詢目錄與檔案的速查筆記

在 Linux 下使用 find 指令查詢目錄與檔案的速查筆記

在 Linux 平台下找檔案不外乎使用威力強大的 find 命令,威力強大的背後就是有一點點學習曲線,不過整體上來說還算簡單,因此想寫一篇文章留下備忘,讓自己日後可以快速查閱參考。

【 基本語法 】

查詢檔案名稱 ( 也可以查詢「目錄名稱」,其中 * 是萬用字元 )

'*.mp3'

   註1: $HOME 是 Linux 下的一個環境變數,預設指向執行帳號的 HOME 目錄

查詢檔案名稱 (不區分大小寫)

'Network'

指定只要搜尋「 檔案」名稱

'*.log' -type f

指定只要搜尋「 目錄」名稱

'apache2' -type d

   補充說明:可用的 –type 參數值如下

b      block (buffered) special

c      character (unbuffered) special

d      directory ( 一般目錄 )

p      named pipe (FIFO)

f      regular file ( 一般檔案 )

l      symbolic link

s      socket

D      door (Solaris)

找尋所有檔案大小 大於 50MB 的檔案

find /var -type f -size +50M

   註1: 不加上 –name 參數即代表搜尋所有檔案

找尋所有檔案大小 小於 50MB 的檔案

find /var -type f -size -50M

尋找
超過 7 天沒有被存取或修改過的檔案
(判斷檔案存取時間)

find $HOME -type f -atime +7

尋找 曾經在 7 天內被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -atime -7

尋找 超過 10 分鐘沒有被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -amin +10

尋找 曾經在 10 分鐘內被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -amin -10

尋找 檔案 建立時間已超過 30 天的檔案

find $HOME -type f -ctime +30

尋找 特定使用者的檔案 ( 以帳號名稱 tom 為例 )


find $HOME -type f -user tom

【 進階應用 】

刪除 30 天以上未經存取過的暫存檔案 ( 注意: 以下指令最後一個分號(;)前一定要加上反斜線 )

'{}' \;

   註1: 加上 –print 是為了讓被刪除的檔案檔名一併顯示在畫面上,這個參數可以省略
   註2: 使用 –exec 會讓查詢到的每一個檔案路徑代入 ‘{}’ 位置,一個檔案會執行一遍 rm 命令

刪除 30 天以上未經存取過的暫存檔案 ( 使用 xargs 當成單一命令的參數 )

find /tmp -type f -print0 | xargs -0 rm -v

   註1: 加上 –print0 是為了讓輸出的結果不以「斷行字元」分隔,而改以 null 為結果的分隔字元

   註2: 使用 xargs 命令加上 –0 是為了讓傳入的資料以 null 字元當成參數的分隔
   註3: 使用 rm 命令加上 –v 是為了能顯示出被刪除的檔案名稱,這個參數可以省略
   註4: 使用 xargs 會將所有 find 命令查到的檔案轉換成 rm 的參數列,如果檔案過多可能會執行失敗!
   註5: 使用 xargs 可確保後面的程式 ( rm ) 只執行一次,所以理論上執行速度較快!

相同參數需輸入多筆並且以「或」邏輯運算時要用 –o 參數串接起來

例1:同時找兩種檔名樣式的檔案

'*.ogg'

例2:同時找兩個擁有者的檔案

local -user user1 -o -user user2

 

【 注意事項 】

  • 使用萬用字元時務必加上單引號( ' )

    !!以下是錯誤示範!!

  • [user1@server ~]# find $HOME -name *.txt
    find: paths must precede expression
    Usage: find [path...] [expression]

 

相關連結


原文出處: The Will Will Web | 在 Linux 下使用 find 指令查詢目錄與檔案的速查筆記
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]如何在 Linux 底下找到想找的東西?

如何在 Linux 底下找到想找的東西?

◎ 本文翻譯自 OLEX Wazi,採 Creative Commons Attribution 3.0 Unported 授權釋出。

在 Linux 底下,"find"、"grep" 及 "awk" 是精細且強大的檔案搜尋工具。透過它們,你可以找到系統上最大和最新的文件、微調搜索參數、搜尋文件內的文字,並執行一些華麗的用戶管理技巧。

 

搜尋最大或最新的檔案

只要你能弄清楚的話,"find" 指令幾乎可以做到任何事。下面的例子是一個能從小到大排序系統中十個最佔空間的檔案,並以人性化方便閱讀的格式顯示容量大小的範例:

# find / -type f -exec du {} \; 2>/dev/null | sort -n | tail -n 10 | xargs -n 1 du -h 2>/dev/null  
1.2G/home/carla/.local/share/Trash/files/download  
1.3G/home/carla/sda1/carla/.VirtualBox/Machines/ubuntu-hoary/Snapshots/{671041dd-700c-4506-68a8-7edfcd0e3c58}.vdi  
2.2G/home/carla/.local/share/Trash/files/dreamstudio.iso  
[...]

這些結果提醒了我為什麼我不喜歡有個資源回收筒,因為當我刪除某些檔案的時候,我是十分認真的確定要刪除(但是檔案仍存在在資源回收筒中)。這個指令會強制搜尋整個檔案系統而且跑一次大概需要幾分鐘才會跑完,所以你可以趁機去外面走一走運動身體。當然,你可以修改指令來搜尋指定的資料夾;舉例來說,用 "find /var/" 來尋找肥大的日誌檔案。


進一步了解這個指令將會發現 "find / -type f" 的意思是「搜尋根目錄中的所有檔案」。選項 "-exec" 是指加入其他指令,然而在這個範例中,加入的是 "du" 這個查詢磁碟使用量的 "-exec du {} \;" 代表「每個找到的檔案都用 du 指令執行以取得以 bytes 為單位的檔案大小資訊」。"2>/dev/null" 是指將所有的錯誤訊息丟棄,使它們不會干擾你的結果。如果你還是想查看被丟棄的訊息的話,可以將範例指令中的兩個 "2>/dev/null" 都刪掉並重新執行。"sort -n" 會將所有的檔案依大小列出,而 "tail -n 10" 則是顯示最後 10 筆,兩個指令合起來就會顯示出依大小排序的前 10 大檔案。你可以把指令打到這裡就執行,然後可以看到像下面那樣的輸出:

1206316 /home/carla/.local/share/Trash/files/download
2209784 /home/carla/.local/share/Trash/files/dreamstudio.iso

"xargs -n 1 du -h" 用來做最後的修飾,將檔案大小從以 bytes 為單位轉成比較好讀的格式。

你可以輕鬆的尋找電腦中最後 5 分鐘內變動過的所有檔案:
# find / -mmin -5 -type f

下面的指令則用來找尋在最後 10 到 20 分鐘之間變動過的檔案:

# find / -mmin +10 -mmin -20 -type f

"+10" 指的是 10 分鐘以前,而 "-20" 指最後 20 分鐘內。如果你不使用加號或減號,則表示恰好為那個數字。另外,也可以使用 "-mtime" 來以 n 天=n*24 小時為單位進行搜尋。如果你想找的是資料夾,則用 "-type d"。

 

在多個資料夾內搜尋

如下面範例所示,你可以列出任意多個要搜尋的資料夾:

# find /etc /var /mnt /media -xdev -mmin -5 -type f

"-dev" 限制在所在的檔案系統內進行搜尋,而不會跨過其他掛載的檔案系統。"find" 預設不會隨著符號連結做深入搜尋,所以你只需要加入 "-dev" 使得搜尋侷限於指定的檔案系統而不會隨著網路共享空間及可移除裝置到處流浪。

 

排除指定目錄

你可以利用 "prune" 選項來排除指定目錄來達到縮小搜尋範圍的效果。"prune" 這個選項有點奇怪:你必須反向思考。下面的例子將會搜尋除了 /proc 及 /sys 這兩個虛擬目錄以外的整個檔案系統:

# find /etc /var /mnt /media -xdev -mmin -5 -type f

首先,你要列出要被排除的目錄,"-o" 表示 "or",然後記得要替括號加上溢出字元(反斜線)。然後,"-prune -o" 表示「不要去查看前面列出的目錄」。

我喜歡利用 "prune" 選項來排除瀏覽器的暫存檔,因為這些檔案會混淆搜尋結果。下面的例子將能達到我前述的需求,並列出每個檔案的日期時間:

$ find / \( -name proc -o -name sys -o -name .mozilla -o -name chromium \) -prune -o -type f -mmin -10 -printf "%Ac\t%p\n"
Wed 28 Sep 2011 10:34:54 AM PDT /home/carla/.local/share/akonadi/db_data/ib_logfile0
Wed 28 Sep 2011 10:34:54 AM PDT /home/carla/.local/share/akonadi/db_data/ibdata1
Wed 28 Sep 2011 05:21:48 PM PDT /home/carla/articles/findgrep.html

其中 "printf" 這個選項代表「輸出格式」。當你想控制你的輸出格式時,請善用 "printf"。你可以指定斷行,日期與時間的格式,以及檔案屬性像是權限、所有者及時間戳記。加入 "%Ac" 將會輸出日期與時間,"\t" 則是跳到下一個定位點,"%p" 則會印出完整的檔案名稱,而 "\n" 則是加入斷行標記。

如你所見,"find" 有許多使用者經常透過 "ls" 指令來完成工作的內建功能。

 

搜尋特定類型的檔案

利用檔案副檔名來搜尋是非常容易的。下列的例子將會在目前的目錄下搜尋三種不同的圖片格式:

$ find . -name "*.png" -o -name "*.jpg" -o -name "*.gif" -type f

利用 "-name" 舉例來說,想搜尋 *mysong.ogg*,你可以用檔名的任何一部分搭配一般 shell 的萬用字元,像 "mys*" 使用 "-iname" 來進行不區分大小寫的搜尋。

 

搜尋重複檔案

這邊介紹兩種可以搜尋重複檔案的方法。第一個方法透過下列指令來檢查檔案的 MD5 切細值 (hash):

$ find . -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 24

上述指令將會計算每個檔案的 MD5 切細值並藉此加以排序,分開顯示在不同的行上,並比對每個切細值的前 24 位數。

第二種方法則是檢查檔案大小:

$ find . -type f -printf "%p - %s\n" | sort -nr -k3 | uniq -D -f1

兩種方法各有優缺,比對 MD5 切細值會得到非常精準的結果,可是比對檔案大小則速度比較快。

 

搜尋檔案內的文字

若要在文字檔案裡面找東西,"grep" 這個指令可以說是超級無敵好用的。假設你有一個資料夾放了滿滿的伺服器設定檔,而你想要在這些檔案之中搜尋你所有的測試項目。如果你很有先見之明的對那些測試項目都使用了 "test" 這個字,下面的指令可以幫你找到它們:

# grep -inR -A2 test /etc/fooserver/

這個指令告訴 "grep" 以不區分大小寫及遞迴(遇到目錄則繼續搜尋目錄內的檔案)的方式在 /etc/fooserver/ 目錄下搜尋,並印出吻合條件的那一行及下兩行。"n" 選項表示印出行號,這對於大檔案來說是十分有用的。

 

搜尋文字區塊

"awk" 這個指令能夠進行 "grep" 不能做到的搜尋相關文字區塊的功能,透過一個簡單的語法:awk '/開始樣式/,/結束樣式/'。假設你想透過 "lspci" 指令來查看關於 Ethernet 裝置的擴展訊息:

$ lspci -v | awk '/[Ee]thernet/,/^$/
08:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller (rev 03)
Subsystem: Lenovo Device 2131
Flags: bus master, fast devsel, latency 0, IRQ 46
I/O ports at 3000 [size=256]
Memory at f2004000 (64-bit, prefetchable) [size=4K]
Memory at f2000000 (64-bit, prefetchable) [size=16K]
[virtual] Expansion ROM at f2020000 [disabled] [size=128K]
Capabilities:
Kernel driver in use: r8169
Kernel modules: r8169

你必須知道你所想看區塊的起始及結束,因此這是一個能夠將設定檔的一部分迅速抽出來看的偉大工具。

下面的例子展現出用花括號做區塊分隔的優勢,並在 radiusd.conf 中迅速找到 "listen" 項目:


# awk '/listen {/,/}/' /etc/freeradius/radiusd.conf
listen {
ipaddr = *
# ipv6addr = ::
port = 0
type = acct
# interface = eth0
# clients = per_socket_clients
}

 

管理使用者及檔案

員工離職,造成組織裡系統檔案的檔案擁有者及權限搞得一團糟,"find" 可以幫你迅速搞定這一切。你可以找到屬於特定使用者的所有檔案:

# find / -user carla

或是特定群組

# find / -group admins

你也可以透過 "-uid" 及 "-gid" 選項來搜尋指定的 UID 及 GID。然後你可以利用使用者名稱或是 UID 將這使用者的所有檔案移交擁有者權限給另一個使用者:

# find / -uid 1100 -ok chown -v 1200 {} \;
# find / -user carla -ok chown -v steven {} \;

當然也可以用來改變檔案的所屬群組:

# find / -group carla -ok chgrp -v admins {} \;

使用 "-ok" 選項來要求你必須對所有的變動做確認。如果你對你做的變動有信心,那麼也可以改用 "-exec"。

當員工離職,可能需要遵守政策來移除他們的檔案,而使用 "find" 可以輕鬆完成:

# find / -user 1100 -exec rm {} \;

當然你必須非常確定你有權這樣做,因為 "find" 不會嘮叨的問你確不卻定要刪除。它只會單純的執行你下達的指令。

"find"、"grep"及"awk"-讀者可以透過這篇文章中所提到的用法,以及參考它們的使用手冊中所給予的些許提示,如此一來,在 Linux 系統上找尋任何檔案就會變得非常容易。



原文出處:如何在 Linux 底下找到想找的東西? - OpenFoundry
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Linux Find Files By Date And List Files Modified On a Specific Date
Linux Find Files By Date And List Files Modified On a Specific Date

How do I find files by date under UNIX and Linux system? How search for files that created on a specific date on Linux or Unix-like system? How to get a list all files that have been modified on a specific date on Linux or Unix?

Linux and UNIX like operating systems do not store file creation time. However, you can use file access and modification time and date to find out file by date. For example, one can list all files that have been modified on a specific date.
Linux Find Files By Date - How to use 'find' to search for files created on a specific date?
Let us see how to find file by date on Linux. You need to use the NA command and NA command.

ls command example to find files by date

The syntax is as follows:
ls -l
ls -lt
ls -ltu
ls -lt /etc/ | more

Sample outputs:
drwxr-xr-x 2 root root    4096 Sep 24 11:42 sysctl.d
drwxr-xr-x 2 root root    4096 Sep 24 11:42 logrotate.d
drwxr-xr-x 2 root root    4096 Sep 24 11:42 init
drwxr-xr-x 2 root root    4096 Sep 24 11:42 init.d
drwxr-xr-x 2 root root    4096 Sep 24 11:42 dnsmasq.d-available
drwxr-xr-x 2 root root    4096 Sep 23 06:22 bash_completion.d
-rw-r--r-- 1 root root   22667 Sep 22 18:17 ld.so.cache
drwxr-xr-x 9 root root    4096 Sep 22 18:17 apparmor.d
drwxr-xr-x 2 root root    4096 Sep 21 06:21 ssh
drwxr-xr-x 2 root root    4096 Sep 20 05:35 python3.5
-rw-r--r-- 1 root root    3611 Sep 20 05:35 mailcap
drwxr-xr-x 3 root root    4096 Sep 13 12:29 apparmor
drwxr-xr-x 7 root root    4096 Aug 22 21:07 network
-rw-r--r-- 1 root root     929 Aug 22 21:07 resolv.conf
-rw-r--r-- 1 root root     492 Aug 22 21:07 fstab
drwxr-xr-x 2 root root    4096 Aug 22 16:31 cron.hourly
drwxr-xr-x 2 root root    4096 Aug 22 16:29 percona-toolkit
drwxr-xr-x 2 root root    4096 Aug 22 16:29 gdb
drwxr-xr-x 2 root root    4096 Aug 18 21:28 cron.daily
...
..
...
-rw-r--r-- 1 root root     575 Oct 22  2015 profile
-rw-r--r-- 1 root root   10368 Oct  2  2015 sensors3.conf
-rw-r--r-- 1 root root    2188 Aug 31  2015 bash.bashrc
-rw-r--r-- 1 root root      45 Aug 12  2015 bash_completion
-rw-r--r-- 1 root root     477 Jul 19  2015 zsh_command_not_found
-rw-r--r-- 1 root root     604 Jul  2  2015 deluser.conf
-rw-r--r-- 1 root root    3663 Jun  9  2015 screenrc
-rw-r--r-- 1 root root     703 May  6  2015 logrotate.conf
-rw-r--r-- 1 root root      12 Apr 30  2015 debian_version
-rw-r--r-- 1 root root     771 Mar  6  2015 insserv.conf
drwxr-xr-x 2 root root    4096 Mar  6  2015 insserv.conf.d
-rw-r--r-- 1 root root     338 Nov 18  2014 updatedb.conf
-rw-r--r-- 1 root root     346 Nov  6  2014 discover-modprobe.conf
-rw-r--r-- 1 root root    2932 Oct 25  2014 protocols
-rw-r--r-- 1 root root     887 Oct 25  2014 rpc
-rw-r--r-- 1 root root   19605 Oct 25  2014 services
-rw-r--r-- 1 root root     280 Jun 20  2014 fuse.conf
-rw-r--r-- 1 root root     497 May  4  2014 nsswitch.conf

You need to use the grep command/egrep command to filter out info:
$ ls -lt /etc/ | grep filename
ls -lt /etc/ | grep 'Jun 20'

A better and recommended solution is the find command:
find . -type f -ls |grep '2017'
find . -type f -ls |grep 'filename'
find /etc/ -type f -ls |grep '25 Sep'

find Command Example

If you need a specific date range many days ago, than consider using the find command. In this example find files modified between Jan/1/2007 and Jan/1/2008, in /data/images directory:
touch --date "2007-01-01" /tmp/start
touch --date "2008-01-01" /tmp/end
find /data/images -type f -newer /tmp/start -not -newer /tmp/end

You can save list to a text file called output.txt as follows:
find /data/images -type f -newer /tmp/start -not -newer /tmp/end > output.txt

Linux find file by date using the date command

Gnu find as various command line option to list files by a modification and access date/time stamp.
Say hello to -newerXY option for find command

The syntax is as follows:
find /dir/ -type f -newerXY 'yyyy-mm-dd'
find /dir/ -type f -newerXY 'yyyy-mm-dd' -ls

The letters X and Y can be any of the following letters:
    a – The access time of the file reference
    B – The birth time of the file reference
    c – The inode status change time of reference
    m – The modification time of the file reference
    t – reference is interpreted directly as a time

To see all files modified on the 24/Sep/2017 in the current directory:
find . -type f -newermt 2017-09-24
## pass the -ls option to list files in ls -l format ##
find . -type f -newermt 2017-09-24 -ls

OR
find . -type f -newermt 2017-09-24 ! -newermt 2017-09-25
find . -type f -newermt 2017-09-24 ! -newermt 2017-09-25 -ls

Sample outputs:
      956      4 -rw-r--r--   1 root     root          910 Sep 24 11:42 ./init.d/.depend.boot
      958      4 -rw-r--r--   1 root     root          876 Sep 24 11:42 ./init.d/.depend.start
      959      4 -rw-r--r--   1 root     root          783 Sep 24 11:42 ./init.d/.depend.stop

To see all files accessed on the 25/Sep/2017:
$ find . -type f -newerat 2017-09-25 ! -newerat 2017-09-26

OR
$ find . -type f -newerat 2017-09-25 ! -newerat 2017-09-26 -ls

List all *.c file accessed 30 days ago

Type the following command:
find /home/you -iname "*.c" -atime -30 -type f

OR
find /home/you -iname "*.c" -atime -30 -type f -ls

List all *.c file accessed more than 30 days ago

Type the following command:
find /home/you -iname "*.c" -atime +30 -type f

OR
find /home/you -iname "*.c" -atime +30 -type f -ls
List all *.c file accessed exactly 30 days ago

Type the following command:
find /home/you -iname "*.c" -atime 30 -type f

OR
find /home/you -iname "*.c" -atime 30 -ls

See also:
See -atime and -mtime FAQ for more information.

原文出處:Linux Find Files By Date And List Files Modified On a Specific Date - nixCraft
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Linux / Unix: Find Files Modified On Specific Date

Linux / Unix: Find Files Modified On Specific Date

How do I find out all files that have been modified on 2013-02-07 (07/Feb/2013) using find command under Linux / Apple OS X / *BSD and Unix like operating systems?

There are two ways to list files in given directory modified after given date of the current year. The latest version of GNU/find command use the following syntax:

Syntax

GNU/find latest version:
find /path/to/dir -newermt "date"
find /path/to/dir -newermt "Feb 07"
find /path/to/dir -newermt "yyyy-mm-dd"
## List all files modified on given date
find /path/to/dir -newermt yyyy-mm-dd ! -newermt yyyy-mm-dd -ls
### print all *.pl ###
find /path/to/dir -newermt "yyyy-mm-dd" -print -type f -iname "*.pl"

Sample outputs:


Fig.01: Find -newermt yyyy-mm-dd in action
Fig.01: Find -newermt yyyy-mm-dd in action

The other way of doing this works on the versions of find before v4.3.3:



touch -t 02010000 /tmp/stamp$$
find /usr -newer /tmp/stamp$$
rm -f /tmp/stamp$$

Examples


To find out all files that have been modified on 2013-02-07 (07/Feb/2013), enter:

2013-02-08

find /path/to/dir -type f -name "*" -newermt 2013-02-07 ! -newermt 2013-02-08

Sample outputs:


./output/tmp/rss.js-gzip-10881623-407-1360173602
./images/advanced-cache.php
./images/faq/2013/02/ir-150x150.jpg
./images/faq/2013/02/warning-40px76.png

To find out all Python files (*.py) in /home/vivek/projects that have been modified on 2013-02-07 (07/Feb/2013), enter:

-print

find $HOME/projects -type f -name "*.py" -newermt 2013-02-07 ! -newermt 2013-02-08 -print

Pass the -ls option to get detailed file listing:

-ls

find $HOME/projects -type f -name "*.py" -newermt 2013-02-07 ! -newermt 2013-02-08 -ls

To find and delete all tmp files (*.tmp) in /home/vivek/projects that have been modified on 2013-02-07 (07/Feb/2013), enter:

-delete

find $HOME/projects -type f -name "*.py" -newermt 2013-02-07 ! -newermt 2013-02-08 -delete



原文出處:Linux / Unix: Find Files Modified On Specific Date - nixCraft
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|