简体   繁体   中英

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

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.

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.

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

在此处输入图片说明

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.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM