In: , , ,
On: 2004 / 05 / 01 Viewed: 38671 times

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

CODE:
  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. # It would be even better if we only deleted
  26. # the *.bak file if the original file was still
  27. # available. In this case, we can use csh and test to help
  28. # Type the following all on 1 line.
  29. $ find . ( -name '*.bak' -o -name *.backup ) -type f -atime +30 -exec csh -c 'if ( -f $1:r ) rm $1' '{}' ;
  30. # or if you're really paraniod, use 'test -s'
  31. # to verify that the original file has not been truncated
  32. # Type the following all on 1 line.
  33. $ find . -name '*.bak' -type f -atime +30 -exec csh -c 'test -s $1:r && rm $1' '{}' ;
  34.  
  35. # Protecting users from themselves - file permissions
  36. $ find . -type l -exec gawk 'BEGIN { "ls -lag}' /dev/null ;
  37.  
  38. # Find text in whole directory tree:
  39. $ find . -type f | xargs grep "text"
  40.  
  41. # Find and Replace in whole directory tree:
  42. $ find . -type f [-name "..."] -exec perl -pi -e 's|xxx|yyy|g' {} ;
  43.  
  44. for example:
  45. find . -type f -name "*html" -exec
  46. perl -pi -e 's|pibeta.psi.ch/~stefan|midas.psi.ch/~stefan|g' {} ;
  47.  
  48. # Show sorted file size of subtree
  49. $ du -a . | sort -r -n | less

Related posts

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. You can trackback this post from your own site

No Comment yet

  1. 1
    TomBoss France »
    said, on 04/May/04 at 12:13 am # :

    FirstPost
    (au cas ou)
    :P

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It