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 "{}" \;

#
print0

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

file names that contain newlines or other types of white space
# to be correctly interpreted by programs that process the find output.
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.

     

    檔案大小

    找出大於 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
    整合其他指令
    # 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