8

I'm trying to get all files by mask in some directory without recursively searching in subdirs. There is no option -maxdepth 0 in AIX for that.

I've heard about -prune, but still can't get how it works.

I guess the command should look something like

find dir \( ! -name dir -prune -type f \) -a -name filemask

but it doesn't work.

Could you please write a correct command for me and explain how it will work?

UPD

It seems command

find dir ! -path dir -prune

prints all files and catalogs in dir, but not files and catalogs in dir/*, so I can use it for my case.

Vikora
  • 125
  • In which way does it not work? – Stéphane Chazelas Jun 23 '17 at 15:48
  • @stéphane-chazelas, my question is not answered in "Limit POSIX find to specific depth". In my case there is no need to use -path for find – Vikora Jun 23 '17 at 15:49
  • Yes, I've reopened. It's unclear what you're problem is and what you want to do. Beside the superfluous (/) and the dir/dir issue mentioned in my answer, it seems to me your answer should work at what I think you want to do (find files called filemask in dir (and dir only)). – Stéphane Chazelas Jun 23 '17 at 15:54

2 Answers2

11

You'd want:

find dir/. ! -name . -prune -type f -name filemask

Or:

find dir ! -path dir -prune -type f -name filemask

To find the regular files called filemask in dir without searching in sub-directories of dir.

With find dir ! -name dir -prune, you'd have issues if there was a dir/dir directory.

The dir/. approach works around that because find will not come across any other file called . than that dir/. file passed as argument.

The -path approach works around it by looking at the file path of the files (as opposed to just the name), -path dir will match on dir, but not on dir/dir (so dir will be the only directory it will not prune). -path may not be available in older versions of AIX though.

More generally, for the standard equivalent of GNU's -maxdepth n, see Limit POSIX find to specific depth?

  • Could you please explain how it works? In the first method file name looks like dir/./filename (with dot!), It looks weird. OK, try understand the second one... Does ! relate to -path dir only? – Vikora Jun 23 '17 at 16:01
  • @Vikora, see edit. – Stéphane Chazelas Jun 23 '17 at 16:05
  • Find ... -path ... executes without errors, but there is not -path in man find. – Vikora Jun 23 '17 at 16:16
  • @Vikora, -path (initially a BSD extension also supported by GNU) was only added in the 2008 edition of the POSIX standard. Possibly AIX added it for conformance some time after that but forgot to update the documentation. – Stéphane Chazelas Jun 23 '17 at 16:43
  • 2
    Confirming Stéphane's comment above that find in AIX 6.1 and 7.1 both support -path and neither have it documented in their corresponding man pages. – Jeff Schaller Jul 03 '17 at 13:38
4

In AIX you can use -prune option.

find ./* -prune