0

How can I exclude the current working directory (CWD) with find?

Consider my current working directory to contain the following directories (ls .):

a b c d e f g

Find command: find . -maxDepth 1 -type d will return:

.
./a
./b
./c
./d
./e
./f
./g

I tried the answer from here (How to exclude a directory in find . command): find . -maxdepth 1 -type d -path . -prune -false

While I can use ls I'd be curious how I could achieve the same with find.

So the expected output from a find command should be similar to the ls . output:

./a
./b
./c
./d
./e
./f
./g
stephanmg
  • 688
  • 4
  • 16
  • 1
    Could you please try following, `find . -maxdepth 1 -type d \( ! -name '.' \) -print` if this helps you? BTW your `maxdepth` has `D` in it which should be `d` in `find` command. – RavinderSingh13 Sep 16 '20 at 07:27
  • there is an alternative to `find` called `fd` (https://github.com/sharkdp/fd) — Using this command yields the expected result immediately `fd --type directory --max-depth 1` – mana Sep 16 '20 at 07:30
  • 1
    I'd rather not rely on external tools like `fd` because it can really be done with `find`. – stephanmg Sep 16 '20 at 07:37
  • @thanasisp eagle eye you are, I think it does. – stephanmg Sep 16 '20 at 07:46

3 Answers3

4

You can use -mindepth 1 option to avoid matching . (current) directory:

find . -mindepth 1 -maxdepth 1 -type d

To be able to match only single character directories:

find . -mindepth 1 -maxdepth 1 -type d -name '?'
anubhava
  • 664,788
  • 59
  • 469
  • 547
1

Could you please try following, assuming you are setting maxdepth to 1 because you need to get only current directory's all directories.

find . -maxdepth 1 -type d \( ! -name '.' \) -print
RavinderSingh13
  • 101,958
  • 9
  • 41
  • 77
0

I think also this should work: find . -maxdepth 1 -type d -\! -name "." -prune

stephanmg
  • 688
  • 4
  • 16