BASH - Find

By kimot, 25 December, 2022

To find file/s with specific name 
find /directory_to_search -name file_name 
when omitted QSYS.LIB 
find / -type d -name QSYS.LIB -prune -o -name 'file_name' -print 
or 
find / -type d -name QSYS.LIB -prune -o -print | grep 'file_name' 
or  
find / -path /QSYS.LIB -prune -o -name 'file_name' -print

 

To find files with specific permission in current directory 
find . -perm 0600

To search in several directories 
find manual clients proposals -perm -0600

To find files changed during latest 24 hours 
find . -ctime 1

To find regular files with multiple links 
find . -type f -links +1

To search for all files that are exactly 414 bytes in length 
find . -size 414c

To find file/s containing inside specific string  
find directory_to_search -type f -exec grep -l "specific_string" {} \;

To find files older than x days and sort result by date/time
find /some_path_to_search/ -mtime +365 -printf "%T@ %Tc %p\n" | sort -n
 

To find and delete *.pdf files older than 30 days:
find /some_path_to_search/ -mtime +30 -type f -name ''*.pdf'' -delete

Usage :   find [-H | -L] Path-list [Expression-list]
-H symbolic links are followed
-L symbolic link in library are followed
Expression-list:
-amin n/-n/+n       access time  
-atime n/-n/+n      access time (days)  
-cmin n/-n/+n       modification time  
-ctime n/-n/+n      modification time (days)  
-cpio Device        write into Device in cpio format
-depth
-ea                 if file has access control information or extended attributes set
- exec Command      if Command return 0 as exit status
-follow             symbolic and hard links are followed
-fstype Type        if file belongs to file system of this type
-group Group        if file belongs to specified Group
-inum n             if file has i-node matching n
-links n            if file has n links
-regex              if entire pathname matches regular expression
-long               prints all characters of each user/group
-ls                 print with statistics
-mmin n/-n/+n       modification time
-mtime n/-n/+n      modification time (days)
-name File
-newer File         if current file is modified more recently than File
-nogroup
-nouser
-ok Command         (same as -exec)
-perm OctalNum      if permission code matches OctalNum
-perm Mode
-print              displays current path name 
-prune              stops the descent of the current path name if it is a directory
-size n             if file is is n of blocks long (512 bytes)
-regex expression
-regextype Type     Basic/Extend
-size nc            file is exactly nc bytes long
-type Type          b-block, c-character, d-directory, f-plain file, l-symbolic link, p-FIFO, s-socket
-u User             file belong specified user
-xdev               prevent find from traversing a file system different from one specified by Path parameter

Tags