Skip to main content

find

檔案名稱
# Find all *.bak files in the current directory and removes them with confirmation
find . -type f -name "*.bak" -exec rm -i {} \;

# find all *.out, *.bak and *.core files and then delete them
find /projectA/ \( -name '∗.bak' -o -name '*.core' \) -exec rm "{}" \;

檔名有包含空白, 換行符號等特殊字元時

find . -type f -name "*.err" -print0 | xargs -I {} -0 rm -v "{}"
  • -print0 : Force find command to print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.
  • -I {} : Replace occurrences of {} in the initial-arguments with names read from standard input. We pass {} as arg to the rm command.
  • -0 : Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
  • rm -v "{}" : Run rm command on matched files.

排除特定附檔名

find . ! -name '*.mp3' -type f | xargs cp -t Misc/
檔案大小

找出大於 1000 MB 的檔案

find / -type f -size +1000M
find . -type f -size +50000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

列出空間使用最高的檔案與目錄的清單

find . -type f -exec wc -c {} \; | sort -nr | head

# show all files/folders in current directory on current filesystem ordered by size
find . -maxdepth 1 -mindepth 1 \( -type d -o -type f \) \( -exec mountpoint -q {} \; -o -exec du -smx {} \; \) | sed "s|./||" | sort -n

# With du
du -xak . |sort -n | tail -50 
du -xah . | sort -rh | head -10
檔案時間

搬移指定日期範圍的檔案
將修改日期是 2012 - 2014 的所有檔案搬移至 /export_data/2012-2014

touch --date "2012-01-01" /tmp/start
touch --date "2015-01-01" /tmp/end

find /home/ams/ipdr -type f -newer /tmp/start -not -newer /tmp/end > 2012-2014.lst
find /home/ams/ipdr -type f -newer /tmp/start -not -newer /tmp/end -exec cp -a {}  /export_data/2012-2014 \;
find /home/ams/ipdr -type f -newer /tmp/start -not -newer /tmp/end -exec rm -f {} \;

列出 2018-03-20 至 2018-04-10 期間曾修改過的檔案

find ~/ -xdev -newermt 2018-03-20 \! -newermt 2018-04-10 -type f -ls

列出 365天以前/20分鐘以前 的所有檔案

find ./ -ctime +365 -ls
find ./ -cmin +20 -ls
  • ctime 檔案建立時間
  • mtime 最近檔案修改時間
  • atime 最近存取檔案時間

找尋3日內曾異動過的檔案,並轉換 Excel format(csv)

find . -xdev -mtime -3 -ls > found.list
sed 's/^[ ]*//g;s/[ ][ ]*/   /g' found.list | awk '{print $3","$5","$6","$7","$8" "$9","$10","$11}' > found.list.csv

列出剛剛異動過的檔案

# 最近 5 分鐘異動過的檔案(包含權限與新增檔案)
find dir -cmin -5

# 最近 10 分鐘修改過的檔案
find dir -mmin 10

目錄 data 內有許多以日期命名的小 log,需要將去年的所有 log 搬到新目錄 archive-2013
註:排除目錄 ./archive-2013

cd data/
mkdir archive-2013
find ./ -path "./archive-2013" -prune -o -name "dcdb_13*" -exec mv {} archive-2013 \;

要排除多個目錄時可以改成
find ./  \( -path "./dir1" -o -path "./dir2" \) -prune -o -name "dcdb_13*" -exec mv {} archive-XXXX \;

按時間先後作排序

find /istrpt/arlog/ -name "*.LOG" -print | xargs ls -lt
檔案權限

列出 0777 的目錄

find . -type d -perm 0777 -ls

指定權限的檔案

find ~/UbuntuMint -type f -perm 600
find ~/UbuntuMint -type f -perm 111
find ~/UbuntuMint -type f -perm 644

# Use the "-" minus prefix before the file permissions to list all 
# the files that possess at least read and write permissions for the file owner.
find ~/UbuntuMint -type f -perm -600

# This "/" will retrieve files within the "UbuntuMint" directory where 
# at least one of the categories (owner, group, or others) meets the specified permission bits
find ~/UbuntuMint -type f -perm /600

find ~/UbuntuMint -type f -perm u=rw,g=r,o=r
find ~/UbuntuMint -type f -perm u+rw,g+r,o+r
整合其他指令
# use find to list sha256sum files only
find . -type f -exec sha256sum {} +

# search for and get rid of duplicate .jpg files
find . -type f -name '*.jpg' -exec sha256sum {} + | sort -uk1,1
特定帳號的檔案
# -xdev 需指定目錄,可以避免 NAS/proc 等目錄
# NOTE: 使用 / 目錄,會使 xdev 失效
find /sp1 /home -xdev -user i00001
排除隱藏目錄檔案
# Exclude the hidden files
find /home -xdev -name "[!.]*" -type f -user i00001

# Exclude the hidden files and directories
find /home -xdev \( ! -path '*/.*' \) -type f -user i00001
find /home -xdev \( ! -regex '.*/\..*' \) -type f -user i00001
定期清除 Archive Log Files
# For Cron on Linux
*/30 * * * * find /istfdc/FDCDB/arclog/istfdc/FDCDB -name "*.LOG" -cmin +60 -exec rm -f {} \;

# For Cron on AIX
50 09 * * * find /istit1/archiveLog/smdb -name "*.LOG" -ctime +5 -exec rm -f {} \\;