Skip to main content
3 of 5
added 17 characters in body
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

The format specifiers of -printf in GNU find takes width and alignment (etc.) qualifiers, just as the printf() C function, meaning you can align the data left or right and, crucially for your current project, allocate a certain width for the data.

Default output:

$ find . -printf '%M %u %g %p\n' drwxr-xr-x kk wheel . -rw-r--r-- kk wheel ./.zshrc -rw-r--r-- kk wheel ./file drwxr-xr-x root wheel ./rootstuff -rw-r--r-- root wheel ./rootstuff/SECRET -rw------- kk wheel ./.viminfo 

Specifying widths (6) for user and group columns (right-justified):

$ find . -printf '%M %6u %6g %p\n' drwxr-xr-x kk wheel . -rw-r--r-- kk wheel ./.zshrc -rw-r--r-- kk wheel ./file drwxr-xr-x root wheel ./rootstuff -rw-r--r-- root wheel ./rootstuff/SECRET -rw------- kk wheel ./.viminfo 

Same, but left-justified:

$ find . -printf '%M %-6u %-6g %p\n' drwxr-xr-x kk wheel . -rw-r--r-- kk wheel ./.zshrc -rw-r--r-- kk wheel ./file drwxr-xr-x root wheel ./rootstuff -rw-r--r-- root wheel ./rootstuff/SECRET -rw------- kk wheel ./.viminfo 

With file sizes in bytes, allocating 5 digits (zero-filled, because just showing how it could be done):

$ find . -printf '%M %-6u %-6g %05s %p\n' drwxr-xr-x kk wheel 00512 . -rw-r--r-- kk wheel 00000 ./.zshrc -rw-r--r-- kk wheel 00095 ./file drwxr-xr-x root wheel 00512 ./rootstuff -rw-r--r-- root wheel 00000 ./rootstuff/SECRET -rw------- kk wheel 00922 ./.viminfo 

Note that the ls command may well allocate widths for its columns dynamically based on the actual need whereas your find -printf command would have to use static widths, unless you run find twice to first compute the needed space for each column and then again to actually format the output (this is why the find -ls output is so wide, it does not use a two-pass approach and instead just gives each column ample space in the hope that everything will align somewhat nicely).

Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k