Saturday, September 5, 2009

Execute multiple commands with exec and find

Is it possible to execute multiple commands using exec on the Linux/UNIX find command output ?

The answer is "yes". Each -exec action is to be associated with a escaped semi-colon (\;)


I had to find files named "1251936000.log" and then need to perform two actions on it:

- Count number of lines in the file.
- Do a "ls -l" listing of the file.

And the find command I wrote with exec:

$ find . -name 1251936000.log -exec wc -l {} \; -exec ls -l {} \;


6924 ./lv1/1251936000.log
-rw-r--r-- 1 root root 977264 Sep 4 00:17 ./lv1/1251936000.log

Related post:
- A set of posts on Linux/UNIX find command.


Greg said...

I need to do something similar but using wildcards. The -exec doesn't seem to recognize wildcards.

find ./ -type d -name blablabla -exec mv {}/*.foo\ Logs {}/.LogFiles/ \;

Is there a way to have find discover your path and still use wildcards?

Greg said...

Can a wildcard be used with find -exec? I'm trying to get this work but can't figure out why it won't.

find ./ -name directoryname -exec mv {}/*.log {}/.LogFiles/ \;

Maybe there's a better way to do it?

© Jadu Saikia