6

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
  • 497
  • 1
  • 6
  • 16
  • 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

6

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

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Maybe I misunderstood something but this doesn't work for me e.g. **$ find . -type d -not -name ".*"** includes files in hidden directories, as does **$ find . -not -path '*/[@.]*' -type f** Maybe it's because of my old version of find... v4.4.2, 2007 – Damo Oct 05 '22 at 07:52
  • 1
    This works for me **find . -type d \( -path "\*/.\*" \) -prune -o -name '*.txt' -print** but it's complicated to remember... – Damo Oct 05 '22 at 08:05
  • My answer is just excluding the files and directories that are returned, not the directories that are descended into. So it won't return `.foo`, but it will return `.foo/bar` – Barmar Oct 05 '22 at 14:49
5

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,475
  • 12
  • 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