66

I have not found a reason why Mac's find does not have the option -printf. Apple normally decides to take options out which are not orthogonal to the other commands?

How can you reach the same result as the following command in Mac without coreutils?

find . -printf "%i \n"         // command in Ubuntu
tripleee
  • 158,107
  • 27
  • 234
  • 292
Léo Léopold Hertz 준영
  • 126,923
  • 172
  • 430
  • 675

5 Answers5

59

It's not that Apple removes options, it's that OS X's UNIX underpinnings are mostly derived (circuitously) from FreeBSD, many parts of which can be traced back to the original UNIX... as opposed to the GNU utilities, which are re-implementations with many features added.

In this case, FreeBSD's find(1) doesn't support -printf, so I wouldn't expect OS X's to either. Instead, this should work on a BSD-ish system:

find . -print0 | xargs -0 stat -f '%i '

It'll fail on a GNU-userland system, though, where you'd write xargs -0 -r stat -c '%i ' because xargs(1) and stat(1) behavior is different.

ephemient
  • 189,938
  • 36
  • 271
  • 385
  • 3
    For some reason this prints numbers instead of strings in iTerm2 on OS X 10.8.4. Instead, `gfind` from the `indutils` package of MacPorts suggested by dmckee works fine. – 0 _ Oct 13 '13 at 18:46
  • That's because %i prints the inode. – Tyler A. Mar 07 '17 at 16:15
  • 1
    @TylerA. how to print file names instead of numbers? – mesqueeb Jul 05 '20 at 02:32
  • @mesqueeb `man stat` gives you the different format values. it looks like replacing `%i` with `%N` will give you the file name, e.g. `find . -print0 | xargs -0 stat -f '%N '` – bmaupin Feb 24 '21 at 14:27
  • 1
    %N prints file full path, what if I only want file's basename? – Bruce Sun Feb 04 '22 at 16:17
39

MAc OS X find binary doesn't support the -printf command. Install brew install findutils on your mac. This install gfind with -printf option.

ch271828n
  • 13,130
  • 3
  • 38
  • 64
abkrim
  • 3,190
  • 7
  • 37
  • 58
21

Well, ephemient and bendin nailed the cause.

I'd add that there is nothing stopping you from installing GNU find (from the findutils) if you need it. If you use fink there is a findutils package. MacPorts has it too.

Community
  • 1
  • 1
dmckee --- ex-moderator kitten
  • 94,778
  • 23
  • 139
  • 230
7

Alternatively, you could just

find . -type f -exec stat -f "%z %N" {} \;

Granted, this isn't how you would do the same thing on linux, but works for MacOS

find . -type f -exec stat -c "%s %N" {} \;

produces similar (not same, but close) output on linux.

dmckee --- ex-moderator kitten
  • 94,778
  • 23
  • 139
  • 230
Sam
  • 71
  • 1
  • 1
3

Ubuntu ships with the GNU version of find, which is more featureful than Mac OS X's find, which is of BSD lineage.

In fact, most of the Ubuntu's user-land utilities are from the GNU project. (Thus you'll sometimes hear Linux-based systems referred to as "GNU/Linux".)

bendin
  • 9,256
  • 1
  • 38
  • 37