Joe C
Joe C

Reputation: 2827

How to find all files in a folder w/o the folder itself

I tried the following commands:

>> ls abc
1  2  3
>> find abc -name "*" -print0
abcabc/1abc/2abc/3
>> find abc -name "*" -print0 | xargs -0 ls
abc/1  abc/2  abc/3

abc:
1  2  3

It seems that . is also found by find if I use *. Can we ask find not to return .?

I also tried -not -path ".". It does not work.

Upvotes: 2

Views: 35

Answers (2)

dawg
dawg

Reputation: 103814

You can also restrict to only files by using the -type f primary this way:

$ mkdir abc
$ touch abc/{1..3}
$ ls abc
1   2   3
$ find abc -type f 
abc/1
abc/2
abc/3

Then find will only show directories as part of the showing of file paths:

$ mkdir abc/efg
$ mkdir abc/xyz
$ touch abc/efg/{4..6}
$ find abc -type f 
abc/1
abc/2
abc/3
abc/efg/4
abc/efg/5
abc/efg/6

Last example neither . or abc/xyz are shown.

Upvotes: 1

l0b0
l0b0

Reputation: 58788

You can specify the minimal depth relative to the starting point(s) you specify:

$ cd -- "$(mktemp --directory)"
$ mkdir a
$ touch a/b a/c
$ find a -mindepth 1 -name "*"
a/c
a/b

Upvotes: 3

Related Questions