Improved ls command to list your linux files.


If you use Linux on a daily basis, then you probably run the 'ls' commands several times a day to list files. This is one of the most used commands on Linux (aside from 'cd') and still, it is sometimes a mess. With the following script, you can "wrap" the normal 'ls' command to:
  1. Highlight folders with colors, to better distinguish them from files.
  2. Sort folders before regular files
  3. Sort hidden folders and hidden files last
  4. Print information in a human-readable format


Comparison

Just to show how the better-ls script differs from a standard ls call, let's compare them side by side. On the top we have 'ls' and 'ls -la', as usually used; and on the bottom we have better-ls. Please note that the bash promt is different because I was using my fancy bash-prompt script.
You'll agree with me that you get much more information at a single glance, and without over-cluttering the terminal.





Source

You can find the latest iteration of this script in my GIT repository, among many other useful bash scripts.

To install it, simply copy-paste it into your user's '.bashrc' file, or alternativelly, source it.

As you'll notice, this script is simply a wrapper that calls 'ls' with different arguments. By default, it will use the following unless you manually call 'ls' with your own.
  •  --color=auto: colorize the output.
  • -l: show output as list (column).
  • --human-readable: show file sizes in bytes, kilo-byes, mega-bytes...
  • --time-style=long-iso: show timestamps in an easy to read format.
  • --group-directories-first: self-explanatory ;)
  • -d: list the folders/files of the specified argument, rather that their content.
    • -d . and -d .. : list the implicit . and .. folders
    • -d * -lA: list all non-hidden files.
    •  -d .!(|.) -l: list all hidden files (starts with .), but omit "." and ".." .
#!/bin/sh
################################################################################
##  FUNCTION                                                                  ##
################################################################################
function beter-ls()
{
 if [ $# -eq 0 ] # If no arguments passed
  then
  ## First implied . and .. directories
  /usr/bin/ls -d .  -l --color=auto --human-readable --time-style=long-iso --group-directories-first;
  /usr/bin/ls -d .. -l --color=auto --human-readable --time-style=long-iso --group-directories-first;

  echo ""
   
  ## First visible directories, then visible files
  /usr/bin/ls -d * -lA --color=auto --human-readable --time-style=long-iso --group-directories-first;
  
  echo ""
  
  ## Then hidden directories and finally hidden files
  ## -d  list folders/files themselves, not their content
  ## .!(|.) anything starting with '.' and not followed by '|.', meaning either nothing or another '.'  
  /usr/bin/ls -d .!(|.) -l --color=auto --hide='..' --human-readable --time-style=long-iso --group-directories-first;  
  
 else
  ## Add user argument
  /usr/bin/ls -la --color=auto --human-readable --time-style=long-iso --group-directories-first "$@"; 
 fi
}



################################################################################
##  ALIAS                                                                     ##
################################################################################
alias ls='beter-ls'


### EOF ###

If you like the script, or would like to share your own, don't hesitate to leave a comment below.

2 comments :

  1. Heads up! If your 'ls' binary does not reside in '/usr/bin/' (as in Arch Linux) you will have to change the above script. For example, in Ubuntu you have to replace every instance of '/usr/bin/ls' with '/bin/ls' (because Ubuntu stores binaries in '/bin' ).

    ReplyDelete
  2. Anonymous2/12/22 02:04

    the portion that lists the hidden directories and folders does not work.
    -bash: syntax error near unexpected token `('

    I see this issue is fixed in your git repo :)

    ReplyDelete