In: , , ,
On: 2004 / 05 / 01
Shorter URL for this post: http://ozh.in/t

Collection of one liners using the GNU/Linux utility "find". Can't remember where I found most of them :)

  1. # Find a file and delete it. Replace foo.bar with the file or use wild cards *.bar
  2. $ find /home -name foo.bar -type f -exec rm -f "{}" ';'
  3.  
  4. # When trying to find large files in / filesytem
  5. # the find command will return results from
  6. # other files systems. Try using the -dev option.
  7. $ find / -dev -size +3000 -exec ls -l {} ;
  8.  
  9. # This will not return files in /usr/local file system.
  10. # This will return files large then 3000 blocks
  11. # only in the root file system.
  12. $ find /usr -dev -size +5000 -exec ls -l {} ;
  13.  
  14. # Someone's done a 'cp -r' and filled up
  15. # the filesystem. Here's a simple way to locate the unwanted files...
  16. $ find . -ctime -1 -print
  17.  
  18. # Find has a partiularly powerful mechanism, -exec,
  19. # which lets you execute commands on a per-file
  20. # basis, while being selective about the files you're operating on.
  21. # Lets say we want to delete all the *.bak or *.backup
  22. # files which are older than 30 days.
  23. # Type the following all on 1 line.
  24. $ find . ( -name '*.bak' -o -name *.backup ) -type f -atime +30 -exec rm '{}' ;
  25.  
  26. # It would be even better if we only deleted
  27. # the *.bak file if the original file was still
  28. # available. In this case, we can use csh and test to help
  29. # Type the following all on 1 line.
  30. $ find . ( -name '*.bak' -o -name *.backup ) -type f -atime +30 -exec csh -c 'if ( -f $1:r ) rm $1' '{}' ;
  31.  
  32. # or if you're really paraniod, use 'test -s'
  33. # to verify that the original file has not been truncated
  34. # Type the following all on 1 line.
  35. $ find . -name '*.bak' -type f -atime +30 -exec csh -c 'test -s $1:r && rm $1' '{}' ;
  36.  
  37. # Protecting users from themselves - file permissions
  38. $ find . -type l -exec gawk 'BEGIN { "ls -lag}' /dev/null ;
  39.  
  40. # Find text in whole directory tree:
  41. $ find . -type f | xargs grep "text"
  42.  
  43. # Find and Replace in whole directory tree:
  44. $ find . -type f [-name "..."] -exec perl -pi -e 's|xxx|yyy|g' {} ;
  45.  
  46. for example:
  47. find . -type f -name "*html" -exec
  48. perl -pi -e 's|pibeta.psi.ch/~stefan|midas.psi.ch/~stefan|g' {} ;
  49.  
  50. # Show sorted file size of subtree
  51. $ du -a . | sort -r -n | less

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/t

Metastuff

This entry "Find one liners" was posted on 01/05/2004 at 12:04 am and is tagged with , , ,
Watch this discussion : Comments RSS 2.0.

No Comment yet

  1. TomBoss says:

    FirstPost
    (au cas ou)
    :P

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?