5

I'm running MacOS Mojave and I'm trying to get a list of owners of files in given path. I'm trying to do it like this but it doesn't work.

$ ls -l /usr | cut -f3
total 0
drwxr-xr-x  971 root  wheel  31072 23 sty 20:05 bin
drwxr-xr-x  304 root  wheel   9728 23 sty 20:05 lib
drwxr-xr-x  248 root  wheel   7936 23 sty 20:05 libexec
drwxr-xr-x   16 root  wheel    512  3 lis 10:50 local
drwxr-xr-x  239 root  wheel   7648 23 sty 20:05 sbin
drwxr-xr-x   46 root  wheel   1472  3 lis 10:41 share
drwxr-xr-x    5 root  wheel    160 21 wrz 06:06 standalone

Specifying delimiters seems to work but not for the TAB character (which should be default).

$ ls -l /usr | cut -f3 -d' '

971
304
248

239

I'm using ZSH with Oh my zsh and iTerm 2 if it matters.

  • 2
    Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls. – nohillside Jan 27 at 14:22
  • 1
    Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output. – chepner Jan 28 at 16:54
17

You can squeeze the white spaces into a single white space in ls 's output then use cut.

ls -l /usr | tr -s ' ' | cut -d ' ' -f3

but avoid parsing ls output. Here's an alternate solution.

stat -f'%Su' /usr/*
11

ls doesn't use tabs, cut doesn't work with a variable number of delimeters between fields.

ls -l /usr | awk '{print $3}'

will work, or

ls -l /usr | awk 'NR > 1 {print $3}'

if you want to skip the first line (total 0 in your example).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .