Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I use find to find files without the preceding directory (./) in a way that also works with -exec?

My goal is to grep through an Apache log to see if any of the files have been accessed recently. This is what I have so far:

find . -max-depth 1 -type d \
  -exec grep \"GET /{}\" /var/log/apache2/ \;

I found a solution using -printf "%f\n", but that does not work with -exec.

share|improve this question
It should be -maxdepth, not -max-depth – Xiè Jìléi Nov 19 '10 at 1:13
up vote 2 down vote accepted
find . -maxdepth 1 -type d -printf "%f\0" | \
     xargs -0 -I {} grep 'GET /{}' /var/log/apache2/
share|improve this answer


grep -Ff <(find . -maxdepth 1 -type d | sed -e 's,^\.,GET ,') /var/log/apache2/

Or, if you want to substitute {} within find command:

find . -maxdepth 1 -type d -exec bash -c 'F={}; F=${F#./}; grep -F "GET /$F" /var/log/apache2/' \;
share|improve this answer

Since you're not doing a recursive search, there's no need to use find, unless you wanted to include only directories and not symbolic links to directories. Assuming there are no directories whose name begins with a dot (if there are, use for x in .*/ */; do …), the following will search the names of all directories and symbolic links to directories in the current directory:

for x in */; do grep -F "GET /${x%/}" /var/log/apache2/; done

If you were doing a recursive search, you could avoid a leading dot by searching from * rather than . (same remark as above regarding names beginning with a dot).

find * -type d -exec grep -F "GET /${x%/}" /var/log/apache2/

Another possibility is to postprocess the output of find. This is especially useful in combination with grep's ability to search multiple newline-separated patterns.

grep -F "$(find . -type d | sed -e 's!^\.!GET !')" /var/log/apache2/

Note that in all cases you're assuming there aren't any characters in the directory names that would be escaped in the Apache log. This is solvable with a little further postprocessing of the find output.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.