0

How do I omit the group column in the output of ls -hal?

I was using ls -hal|cut -f4 --complement -d ' ' and it works find most of the time, but if I run it in / I get scrambled output:

drwxr-xr-x  25 root 4.0K Dec 21 06:08 .
drwxr-xr-x  25 root 4.0K Dec 21 06:08 ..
drwxr-xr-x   root root 4.0K Jan 12 06:17 bin
drwxr-xr-x   root root 4.0K Jan 16 10:36 boot
drwxrwxr-x   root root 4.0K May  1  2018 cdrom
-rw-------   root root  56M May  1  2018 core
drwxr-xr-x  20 root 4.7K Jan  6 17:54 dev
drwxr-xr-x   root root 4.0K Oct  9 15:14 .dotnet
drwxr-xr-x 154 root  12K Jan 16 10:36 etc
drwxr-xr-x   root root 4.0K Nov 24 19:39 home

(I can't figure out why this happens, ls -hal alone gives

drwxr-xr-x  25 root root 4.0K Dec 21 06:08 .
drwxr-xr-x  25 root root 4.0K Dec 21 06:08 ..
drwxr-xr-x   2 root root 4.0K Jan 12 06:17 bin
drwxr-xr-x   3 root root 4.0K Jan 16 10:36 boot
drwxrwxr-x   2 root root 4.0K May  1  2018 cdrom
-rw-------   1 root root  56M May  1  2018 core
drwxr-xr-x  20 root root 4.7K Jan  6 17:54 dev
drwxr-xr-x 154 root root  12K Jan 16 10:36 etc

)

I also tried awk '{print $1,$2,$3,$5,$6,$7,$8,$9}' but that always messes up the alignment:

drwxr-xr-x 25 root 4.0K Dec 21 06:08 .
drwxr-xr-x 25 root 4.0K Dec 21 06:08 ..
drwxr-xr-x 2 root 4.0K Jan 12 06:17 bin
drwxr-xr-x 3 root 4.0K Jan 16 10:36 boot
drwxrwxr-x 2 root 4.0K May 1 2018 cdrom
-rw------- 1 root 56M May 1 2018 core
drwxr-xr-x 20 root 4.7K Jan 6 17:54 dev
  • Just for consideration, if you want more control over the fields, you could piece together your own ls with a variation of stat -c '%A %h %U %s %y %n' – Jeff Schaller Jan 16 at 17:07
5

There are specific ls options to hide the group column. From ls(1):

-G, --no-group

in a long listing, don't print group names

-o

like -l, but do not list group information

So you could use either ls -hao, or ls -halG.

  • 1
    Also, with cut, every instance of the delimiter character matters. – Kusalananda Jan 16 at 11:24
  • Oh, i read somewhere that you cannot select or unselect individual columns, but I guess there is an exception for that column. Thanks – Bananach Jan 16 at 11:53
0

In your case, you the problem is that there are multiple single spaces in the result of ls -alh /. Meaning that the field 4 is a different one.

What you can do is to aggregate the spaces into one:

ls -alh / | sed 's/\s\s*/ /g' | cut -f4 -d' ' --complement

But be aware that it will also remove any potential double space from the file names.

You can also learn more from this answer here:

Friendly reminder to look at your commands options. As in this case, the result can be simply achieved with the command given by JigglyNaga.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.