cmd - find

最後更新: 2019-09-18

 

 


常用 Opts

 

time (-mtime)

Unit                  # n x 24 小時

+n                    # for greater than

-n                     # for less than n

n                      # n 當天 24 小時內的 file

i.e.

# n*24 hours ago

-mtime n

# file less than 24 hours ago

find $HOME -mtime 0

user

-uid n

-user <uname>

group

-gid n

-group gname

-iname          case  insensitive

 


Size

 

-size n[cwbkMG]

args

'b'    for 512-byte blocks

'c'    for bytes

'k'    for Kilobytes

'M'    for Megabytes

'G'    for Gigabytes

# 找出大過 10M 的信

-size +10M

 


Type

 

-type f | d | l

 


Permission

 

-perm

find . -perm 664         <-- permission 是 644

-664              <-- 最少有的意思 (This will match a file which has mode 0777)[without regard to the presence of any extra permission bits]

                           "All of the permission bits mode are set for the file."

/222              <-- Search for files which are writable by somebody

                           "Any of the permission bits mode are set for the file."

       find . -perm /220
       find . -perm /u+w,g+w
       find . -perm /u=w,g=w

# To find all SGID files:

find / -xdev -type f -perm +g=s -print

# To find all SUID files

find / -xdev -type f -perm +u=s -print

 


exec

 

find . -type f -exec file '{}' \;

remark

'{}' 與 \; 之間要有空格

 


find 與 xargs

 

find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

解釋

find 的 -print0

# print the full file name on the stout followed by a null character

(instead of the newline character that -print uses)

xargs 的 -0

# Input items are terminated by a null character instead of by whitespace

 


Looking for Files NOT owned by someone

 

find . \! -user foo -print

 


symbolic

 

-L     Follow symbolic links

 

# fine link file only

find . -maxdepth 1 -type l

 


Depth

 

-maxdepth levels

-mindepth levels

 


A pipe "|" in linux 's find -exec command

 

# top level shell to perform the piping

# 缺點是不知每目錄內郵件數量

find . -type d -name cur -exec ls -1 "{}" \; | wc -l

# execute via sh

 * 注意: This "{}" in your first command may even lead to code injection.

 * 不建議用, 檔名有空格都已經出事.

    (literally named " & rm -rf ~ & : ".txt)

find . -type d -name cur -exec sh -c 'echo {}; ls -1 {} | wc -l' \;