14

Simple question, I'm running the following find command:

find . -type d \( -path ./.git -o   \
                  -path ./log -o    \
                  -path ./public -o \
                  -path ./tmp       \) \
                  -prune -o         \
                  -print

To list all the files in my directory, excluding the specified directories.

This works great, however, what I want to also do is exclude any actual directories from the output, so if there is a directory structure as follows:

test
  -- foo.text
  -- bar.text

when I run my command I'd like to see:

./test/foo.text
./test/bar.text

instead of:

.
./test
./test/foo.text
./test/bar.text

Can anybody help?

TheDelChop
  • 249
  • 1
  • 2
  • 4

3 Answers3

16
find . -type f

will do it. You can do exec if you want to operate on file names.

unxnut
  • 5,908
  • 2
  • 19
  • 27
7

Just use ! -type d:

find . -type d \( -path ./.git -o \
                  -path ./log -o \
                  -path ./public -o \
                  -path ./tmp \) -prune -o \
       ! -type d -print
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
1

Here's one way to do it. I'm piping your output from find (using xargs) to a little bit of bash which asks the question "Is this not a directory?" and if it's not, it echoes it to your terminal.

Here's the whole she-bang:

find . -type d \( -path ./.git -o -path ./log -o -path ./public -o -path ./tmp \) -prune -o -print | xargs -i bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi'

Here's just my addition:

xargs -i bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi'

To explain:

xargs -i replace string "{}" with arguments (those that are piped in)

bash -c commands read from string

if [ ! -d "{}"]; is this a directory.

echo "{}" echo the find result.

fi; finish if.

dougBTV
  • 2,494
  • 1
  • 17
  • 17
  • There, you're turning the filename into shell code. Typical privilege escalation path. As one can create a file called `$(do-what-I-want)` for instance to have _you_ do what he wants. – Stéphane Chazelas May 23 '13 at 20:12
  • This is a cool workaround when one not knowing how to use `-o ! -type d`. Cool trick! – azbarcea Aug 13 '20 at 22:16