Phil
Phil

Reputation: 717

Exclude hidden files and folders in linux find

I am trying to exclude hidden files and folders when doing a find in linux.

I have to exclude files or folders that start with a dot (.hidden) but also have to exclude folders that start with an @ (like @eaDir).

So far I have the following command which seems to work but maybe there is a more elegant way?

find /path/to/start/search/ -not -path '*@eaDir*' -not -path "*/\.*" -type f -mtime -2 

I did see examples using regular expression like so:

find . \( ! -regex '.*/\..*' \) -type f

but not sure how I would also exclude @eaDir directories with the -regexoption?

I believe there can also be hidden files that start with two dots? like "..hidden"? Is this already covered with my command or would I simply add a third option like -not -path "*/\..*" to exclude those as well?

Then I saw some examples of using -prune so that find won't descend in hidden directories, however I am unsure how I would use this correclty in my example. I would be interested in this to speed things up.

Thanks!

Upvotes: 9

Views: 11516

Answers (2)

Camusensei
Camusensei

Reputation: 1553

Exclude files and folders starting with a . or an @:

find /path/to/start/search/ -not -path '*/[@.]*' -type f -mtime -2

Exclude files starting with a . and files and folders starting with a . or an @:

find /path/to/start/search/ -not -path '*/.*' -type f -mtime -2 | grep -v '/@.*/.*'

Upvotes: 10

Barmar
Barmar

Reputation: 780723

Use -not -name '.*'. This will exclude any names that begin with ..

Upvotes: 8

Related Questions