53

I want to display all the column headers when I type ls -l command in bash shell in unix/linux

When we type ls -ltr on command prompt we get something like the following.

-r--r--r--  2   makerpm   root   1898   Jan 28 14:52   sample3
-r--r--r--  2   makerpm   root   1898   Jan 28 14:52   sample1

What I want is to know whether ls has any options to display with column headers:

File_Permissions  Owner Group   Size   Modified_Time  Name
-r--r--r--  2  makerpm   root   1898   Jan 28 14:52   sample3
-r--r--r--  2  makerpm   root   1898   Jan 28 14:52   sample1
sunil_rbc
  • 772
  • 1
  • 8
  • 13
  • Pipe the result to `awk`. – Maroun Jan 28 '16 at 12:30
  • 6
    Stack Overflow is for programming-related questions and questions on *using* Linux / Unix are [off-topic](http://stackoverflow.com/help/on-topic). This question would be more appropriate for [Unix and Linux](http://unix.stackexchange.com/), [Super User](http://superuser.com/) or [Ask Ubuntu](http://askubuntu.com/) (if you’re running Ubuntu). You should also clarify your description of your desired output. – Anthony Geoghegan Jan 28 '16 at 12:48
  • could you give an example of the type of output you want? – Vorsprung Jan 28 '16 at 13:36
  • 1
    Worth noting: There's an extra column between `group` and `size` when you run `ls -l` in `/dev/`. This column indicates the major number associated with each device. – Alex Jansen Aug 02 '19 at 02:14

3 Answers3

30

exa is a replacement/enhancement for ls. If you pass on the arguments -lh with exa, it will include a header row printing the column names like so:

exa -lh

Example output:

Permissions Size User     Date Modified Name
.rwx------    19 username 29 Sep 11:25  dont_cra.sh
drw-r-----     - username 29 Sep 11:26  f1
.rw-r--r--@ 811k username 29 Sep 11:25  row_count.dat
.rw-r--r--    54 username 29 Sep 11:25  some_text.txt

You can set up an alias in .bashrc that replaces ls with exa.

Bart Schuijt
  • 591
  • 1
  • 10
  • 14
  • 6
    Usually, I avoid comments like 'Thank you!', 'Great!', etc. But I'm making an exception here. The 'exa' program is precisely what I was looking for. Actually, it's better than that. I strongly recommend the "stackoverflowers" to talk a look at the link. So, if the moderator allows me, thanks for the link; the program is great! :) - Upvoted! – Almir Campos Feb 12 '19 at 03:36
  • No exa in centos. How to install it? – Scott Chu May 05 '22 at 04:05
  • Note that if want `group` column, the option `-g` is needed. – laoyb Nov 27 '22 at 14:15
  • 1
    Literally not what was asked – Johnny Bigoode May 09 '23 at 14:15
7

The last answer using sed was slick, but unfortunately, if you have color added to your output (which most people do) it removes all color. I would like to suggest a better way, and ironically, simpler too.

First off, my .bashrc USED to have the following:

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
fi

alias ls='ls -AFhls --color --group-directories-first'

To be honest, you don't need the dircolors part, that is just a little extra I use, you could have something as simple as:

alias ls='ls --color=auto'

I wanted column headers too, Googled it, and wound up here. However after I tried what the previous user suggested, with sed and realizing everything was white, and my colors have all gone away.

That's when I tried something different in my .bashrc file, and it worked.

Simply alias ls, echo first, then place a semi-colon, then your ls command. My .bashrc file now has the following line.

alias ls='echo "Dir Size|Perms|Link Count|Owner|Group|Size|Mod. Time|Name"; ls -AFhls --color --group-directories-first'

When doing it this way, utilizing echo instead of sed, all colors continue to work.

jolammi
  • 492
  • 3
  • 16
PyTis
  • 1,054
  • 10
  • 13
5

Not sure if this is specific to my terminal's output but, this worked for me.

enter image description here

And this is the output it yields. As you can see, it preserves color using this method. Just be sure to keep the ${1} variable in double quotes so files and directories with spaces in the name won't cause an error.

enter image description here

Here is the code so you can copy and paste for testing.

long_ls() {

local VAR="Permissions|Owner|Group|Size|Modified|Name"

if [ ! "${1}" ]; then
   echo -e "$VAR" | column -t -s"|" && ls -l
else
   echo -e "$VAR" | column -t -s"|" && ls -l "${1}"
fi

}

alias lls=$"long_ls ${1}"
Yokai
  • 1,170
  • 13
  • 17