sunil_rbc
sunil_rbc

Reputation: 842

How to display column headers for 'ls -l' command in unix/linux?

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

Upvotes: 59

Views: 62873

Answers (3)

PyTis
PyTis

Reputation: 1134

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.

Upvotes: 8

Yokai
Yokai

Reputation: 1220

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}"

Upvotes: 6

Bart Schuijt
Bart Schuijt

Reputation: 625

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.

Upvotes: 33

Related Questions