2012-05-15

【系統】Linux - find 指令


如果要依據最後修改時間,刪掉超過60天的檔案,可以使用如下的指令
find /path/to/folder -type f -mtime +60 -exec rm -f {} \;


Find 很熟的話,真的超好用的耶。

網路上看到一篇文章在專門說find,還挺有用的,
把重點節錄下來:
Find all files with something in their name:
find . -name "*.jpg"

Find all files that belong to a certain user:
find . -user daniel

Find only directories, regular files, links, or sockets:
find . -type d
    * -(files, links, or sockets), just substitute f, l, s for the d in the command above.

Find files that are over a gigabyte in size
find ~/Movies -size +1024M

Find only regular files, owned by daniel, that are also jpg images
find . -user daniel -type f -name '*.jpg'

Now do the same, but exclude anything named autumn
find . -user daniel -type f -name '*.jpg' ! -name autumn*

Find all files in /etc owned by root that have been modified within the last day

find /etc -user root -mtime -1  (-n and +n)
    * -atime: when (in days) the file was last accessed
    * -ctime: when (in days) the file's permissions were last changed
    * -mtime: when (in days) the file's data was last modified

Show me all files in /etc owned by root that have been accessed within the last two minutes

find /etc -user root -amin -2
    * -amin: when (in minutes) the file was last accessed
    * -cmin: when (in minutes) the file's permissions were last changed
    * -mmin: when (in minutes) the file's data was last modified

Show me all files in ~ with wide open permissions
find ~ -perm 777
    * -nouser: shows output that's not associated with an existing userid
    * -nogroup: shows output not associated with an existing groupid
    * -links n: file has n links
    * -newer file: file was modified more recently than file.
    * -perm mode: file has mode permissions.

Find all files on your system that are world writable. The 0002 denotes a 2 in the "other" field in the file permissions, which is the write bit
find / -perm -0002

Collect files that are not owned by valid users and delete them
find / -nouser -print0 | xargs -0 rm

Clean the images off of your *nix desktop
find ~/Desktop -name "*.jpg" -o -name "*.gif" -o -name "*.png" -print0 | xargs -0 mv --target-directory ~/Pictures
    ** The -print0 option terminates results with a null character instead of the default newline, making it cleaner and less likely to balk in many cases

Correct the permissions on your web directory
find /your/webdir/ -type d -print0 | xargs -0 chmod 755
find /your/webdir -type f | xargs chmod 644


Show a list of files in /etc that have been modified since last month
find /etc -mtime -30

來源:http://danielmiessler.com/study/find/

0 comments:

張貼留言