Thursday, February 27, 2014

Linux Find Command Options

Here's a list of some useful find commands. I keep this handy when I can't remember something. Hope it helps some out.

FIND Commands 
  • Find all files owned by gid XXX and change to gid YYY 
    • find /path -group 200 -exec chgrp 203 {} \; 
    • Example: 
      • GID for XXX = 32840 
      • GID for YYY = 32121 
    •  [root@hostname]#  find /path -group 32840 -exec chgrp 32121 {} \;  
    • find / -user todd -exec cp -varf {} /filelist \;  
  • Find all files named X and chmod 777 
    • find /path/to/file -name filename  -exec chmod 777 {} \;  
  • Find large files 
    •  find /var -type f -size +500k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }  
  • List files by size in directory 
    •  for X in `du -s * | sort -nr | cut -f 2`; do du -hs $X; done  
  • Find file that has findfile in the current directory and all subdirectories: 
    • find . -name \*findfile\* 

  • Find file older than X 
    • find /path/dir -mtime +X -delete  (x=days, ie +14 would be two weeks) 
find / -mount -size +500M -print 

If you are unable to determine where the space is being used, run the following command: 
[root@server]# find / -size +10240000c -exec du -h {} \; | less 
This command provides a list of all files that are larger than 10MB in size.  

Note: Another useful command is du -h --max-depth=1 <dir>. This command lists the directories within a given filesystem that contain the largest files. 


No comments:

Post a Comment