Hacker News new | past | comments | ask | show | jobs | submit login
Linux Find Command Examples (thegeekstuff.com)
27 points by mlLK on May 24, 2009 | hide | past | favorite | 8 comments



A mention of xargs might be worth slipping into this. A lot of the time it doesn't really matter whether you use that or -exec, but it's faster on large lists of files. And some commands (grep) act differently if there are multiple files specified as input.


xargs was my first thought as well. I think I type in this line almost daily (why have i not made an alias already?):

find . -type f | xargs grep needle

gnu find and xargs have the -print0 and -0 arguments for piping nul terminated filenames, so special chars in file names wont bite you.


I used to use the find | xargs grep idiom a lot too until I installed ack ( http://betterthangrep.com/ ).


That example is the equivalent of 'grep -r needle .' if you have GNU grep.


Reading this article I realized I always put `find .`, i.e. explicitly specifying the current directory. I guess this is like saying `ls .` all the time, but oh well.

Sometimes I find that I need to chain a few actions for my search results that aren't possible with multiple -execs, so I employ the shell script loop:

  for js in `find . -name '*.js'`; do
    dir=`dirname $js`;
    cd $dir && make;
    java -jar jsmin.jar "$i";
  done;
Unfortunately if you've got spaces in your filenames, this will throw off /bin/sh and you have to specify delimiters or work around that.

Lastly, I've found with find that if you have a n-sized conditional where n > 1 and an -exec, you'll want to use escaped parentheses or otherwise the conditionals won't evaluate properly.

Edits: addressing formatting woes.


I always use it in the form:

  find . -iname '*.someextension' -exec ls -l {} \;
{} gets expanded to the matching file names. Of course ls -l is just an example of command to run against the file name.


sudo find / -size +1G -exec ls -lah '{}' \; | awk '{print $8" : "$5}' | tee files_over_1G.txt ### This I use alot.


man find expanded into article? nice.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: