Search This Blog

Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Execute a command on the results of find command

  • execute the command multiple times on each individual result file:
    find . -type f -exec echo {} \;
    The result should look like:
    ./1
    ./3
    ./2
  • execute the command only once on all result files:
    find . -type f -exec echo {} +
    The result should look like:
    ./1 ./3 ./2



Example: Remove .DS_Store and ._* files:

find . -type f -name '.DS_Store' -exec rm -f {} \;
find . -type f -name '._*' -exec rm -f {} \;

see also

Count lines of Java source code

find . -type f -name "*.java" | xargs cat | grep [alnum{}] | wc -l
or shell script could be:
#!/bin/bash

[[ -z $1 ]] && dir=. || dir=$1

find $dir -type f -name "*.java" | xargs cat | grep [alnum{}] | wc -l