4

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!

Phil
  • 149
  • 2
  • 13
  • 1
    Also see [Count files, except the hidden files](https://stackoverflow.com/q/52621332/608639) and [Why does find . -not -name “.*” not exclude hidden files?](https://stackoverflow.com/q/16900675/608639) – jww Nov 16 '19 at 23:19

2 Answers2

5

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

Barmar
  • 669,327
  • 51
  • 454
  • 560
2

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 '/@.*/.*'
Camusensei
  • 1,320
  • 11
  • 20
  • Thanks, that worked great and is much more sleek than my initial command! Now I just need to figure out about the `-prune` option. – Phil Nov 17 '19 at 11:24
  • `find /path/to/start/search/ -prune -not -name '.*' -type f -mtime -2 | grep -v '/@.*/.*` I guess? (untested) – Camusensei Nov 18 '19 at 23:23