A Better ls Command

The ls command is a command to list files on a UNIX-like system. It is probably one of the most used command. However, a plain ls command without any polishing may look really “plain”. Here, we will slightly configure this command to make it more usable:

  • More colorful output
  • Automatic pagination for long file lists
  • File type indication
  • Human readable sizes
  • Natural ordering of files

Screenshots:

Output of ls before configuration

Output of ls before configuration

Output of ls after configuration

Output of ls after configuration

ls pagination

Pagination for ls

In this post, the version of the ls program we will use is GNU ls—that is, the ls program from GNU Coreutils. This is because it provides all the features we want to have and is perhaps the most powerful ls command in the world, at least as far as I know. You can execute ls --version to check whether you are using GNU ls: If it prints ls (GNU coreutils), it means you are using GNU ls; if not, you can install it on your system. Usually it is the default ls on GNU/Linux and cygwin. If you are not on GNU/Linux or cygwin, here is the installation instruction for macOS and FreeBSD.

For the impatient, add the following to your ~/.bashrc if you use bash or ~/.zshrc if you use zsh:

if ls --color -d . >/dev/null 2>&1; then  # GNU ls
  export COLUMNS  # Remember columns for subprocesses.
  eval "$(dircolors)"
  function ls {
    command ls -F -h --color=always -v --author --time-style=long-iso -C "$@" | less -R -X -F
  }
  alias ll='ls -l'
  alias l='ls -l -a'
fi

The ll and l commands are for the long-format output.

If it does not work for you, make sure you have restarted your shell and check the output of which ls. If it shows ls is an alias, which is possibly set by some system-wide configuration file, you need to unalias it by adding unalias ls to your ~/.bashrc or ~/.zshrc.

Step by Step to Build the Configuration

Better Default ls Command Line Options

To use better default options for the ls command, add the follows to your ~/.bashrc or ~/.zshrc:

alias ls='ls -F -h --color=always -v --author --time-style=long-iso'
alias ll='ls -l'
alias l='ls -l -a'
  • -F: Indicate different types of files in the output. For example, directories are appended with /, symbolic links are appended with @, sockets are appended with =.
  • -h: Display human readable file sizes. For example, instead of 625616, 611K is displayed.
  • --color=always: Display colorful output.
  • -v: Display files in the natural order. For example, in a directory with files 1.txt, 2.txt, 3.txt and 10.txt, instead of displaying them with a naive alphabetical order:
    10.txt 1.txt 2.txt 3.txt

    using the more natural order

    1.txt 2.txt 3.txt 10.txt
  • --author: Display the author of the file in the long format.
  • --time-style=long-iso: Display time using ISO standard. You can remove it if you don’t like ISO style time.

Better Colors

By default, the ls command only displays different colors for different file types, such as directory, symbolic link. To use different colors for different file extensions, add the follows to your ~/.bashrc or ~/.zshrc:

eval "$(dircolors)"

This command loads the default color database file and sets the environmental variable LS_COLORS which is used by ls to determine the color for each file extension. Alternatively, you may consider this collection of extension color mappings for LS_COLORS.

Pagination for Long Lists of Files

One issue when using the ls command is that when the file list is long, the display is deeply scrolled down. A popular solution on the Internet is to use ls | less so that the list can be viewed from a terminal pager. Unfortunately, it puts all files in one column and loses colors. In addition, it also paginates the output even the list is short. To solve these issues, we can use the -C of ls and the -R, -X and -F options of less: (replace the ls alias above with the following code)

export COLUMNS  # Remember columns for subprocesses.
function ls {
  command ls -F -h --color=always -v --author --time-style=long-iso -C "$@" | less -R -X -F
}
  • export COLUMNS: Make subprocesses know the number of columns of the screen.
  • -C: List files in columns.
  • -R: Cause ANSI “color“ escape sequences to be displayed in their raw form. To put it simple: Keep the colors.
  • -X -F: Prevent clearing screening when exiting and exit if the entire list can be displayed in one screen. With these two options, if the entire list can be displayed in one screen, less automatically exits while keeping the contents.

Here, we also use a function instead of an alias so that any additional arguments can be passed to the ls command before the pipe. Now, when the listing is long, you should automatically view it in the terminal pager less: Press j (or ) and k (or ) to move forward and backward, Ctrl+f (or PageDown) and Ctrl+b (or PageUp) to scroll one screen forward or backward. You can further improve your experience with less by looking into this post.

Conclusion

Enjoy your brand new ls command!

2 thoughts on “A Better ls Command

  1. rphan

    I’ve had something very similar for… way too many years.

    Try this out for your less command. You might like it.

    less –quit-if-one-screen -X –QUIT-AT-EOF

    Reply

Leave a Reply

Your email address will not be published.