How to use color in the ls command to brighten up your directory listings.

Posted: March 27, 2013. At: 7:44 PM. This was 5 years ago. Post ID: 5521
Page permalink: http://securitronlinux.com/bejiitaswrath/how-to-use-color-in-the-ls-command-to-brighten-up-your-directory-listings/

Below we see output of the ls command when listing a directory. The –color=auto parameter allows the use of color if your terminal supports it. And since everyone either uses Konsole or Gnome Terminal; this command will always work. I am using zsh at the moment; but this works with just about any shell.

ls -hula --color=auto
total 90M
drwxr-xr-x  5 root root 4.0K  26-03-13 10:31 pm .
drwxr-xr-x 23 root root 4.0K  26-03-13 12:47 pm ..
-rw-r--r--  1 root root 918K  25-03-13 12:00 pm abi-3.9.0-030900rc4-generic
-rw-r--r--  1 root root 132K  25-03-13 12:04 pm config-3.2.0-2-486
-rw-r--r--  1 root root 132K  25-03-13 12:04 pm config-3.2.0-3-486
-rw-r--r--  1 root root 132K  25-03-13 12:04 pm config-3.2.0-4-486
-rw-r--r--  1 root root 131K  25-03-13 12:04 pm config-3.2.0-4-rt-686-pae
-rw-r--r--  1 root root 159K  25-03-13 12:04 pm config-3.9.0-030900rc4-generic
-rw-r--r--  1 root root  601  18-10-12 10:02 am config-9.0-1-486
drwxr-xr-x  3 root root 8.0K  25-03-13 12:21 pm grub
-rw-r--r--  1 root root 8.5M  29-06-12 03:50 pm initrd.img-3.2.0-2-486
-rw-r--r--  1 root root  12M  26-01-13 11:19 pm initrd.img-3.2.0-3-486
-rw-r--r--  1 root root  12M  25-02-13 04:03 pm initrd.img-3.2.0-4-486
-rw-r--r--  1 root root  12M  18-03-13 09:30 pm initrd.img-3.2.0-4-rt-686-pae
-rw-r--r--  1 root root  13M  25-03-13 12:05 pm initrd.img-3.9.0-030900rc4-generic
-rw-r--r--  1 root root 2.8M  26-01-13 02:12 pm initrd.img-9.0-1-486
drwxr-xr-x  2 root root   42  16-01-13 11:59 am iso
drwxr-xr-x  2 root root   36  16-01-13 11:59 am kernel
-rw-r--r--  1 root root 4.9M  18-10-12 10:02 am kfreebsd-9.0-1-486.gz
-rw-r--r--  1 root root 1.5M  22-08-12 03:32 pm System.map-3.2.0-2-486
-rw-r--r--  1 root root 1.5M  29-10-12 11:44 am System.map-3.2.0-3-486
-rw-r--r--  1 root root 1.5M  11-02-13 10:44 am System.map-3.2.0-4-486
-rw-r--r--  1 root root 1.6M  26-02-13 11:53 am System.map-3.2.0-4-rt-686-pae
-rw-------  1 root root 2.5M  25-03-13 12:00 pm System.map-3.9.0-030900rc4-generic
-rw-r--r--  1 root root 2.4M  05-03-12 03:08 pm vmlinuz-3.2.0-2-486
-rw-r--r--  1 root root 2.4M  26-01-13 11:19 pm vmlinuz-3.2.0-3-486
-rw-r--r--  1 root root 2.4M  25-02-13 04:03 pm vmlinuz-3.2.0-4-486
-rw-r--r--  1 root root 2.6M  18-03-13 09:30 pm vmlinuz-3.2.0-4-rt-686-pae
-rw-------  1 root root 5.3M  25-03-13 12:00 pm vmlinuz-3.9.0-030900rc4-generic

I know you can not see the colors in this code listing; but this also works with the tclsh shell on Debian.

ls -hula --color=auto
total 248K
drwxr-xr-x  10 root root    97  26-03-13 10:00 pm .
drwxr-xr-x  23 root root  4.0K  26-03-13 12:47 pm ..
drwxr-xr-x   2 root root   68K  26-03-13 12:58 pm bin
drwxr-xr-x   2 root root  4.0K  26-03-13 10:27 pm games
drwxr-xr-x  51 root root  8.0K  26-03-13 10:02 pm include
drwxr-xr-x 262 root root   56K  26-03-13 09:35 pm lib
drwxrwsr-x  10 root staff  105  26-03-13 10:02 pm local
drwxr-xr-x   2 root root  8.0K  26-03-13 10:02 pm sbin
drwxr-xr-x 437 root root   12K  26-03-13 01:20 pm share
drwxr-xr-x  15 root root  4.0K  26-03-13 10:00 pm src

And this is a good way to force the width of the ls output. This is useful sometimes when you do not want a full width ls output.

ls -w 80
awesome.jpg  dwhelper  Music	     Pash      root.jpeg  vol_1.list
Desktop      kernel    My Downloads  Pictures  Templates  xterm1.jpg
Documents    lc        MySQL	     Proliant  test	  xterm2.jpg
Downloads    Mail      out3	     Public    Videos

How to list only directories and symlinks that lead to directories with the Linux shell. This is accomplished with the command below.

ls -ld *(-/DN)
drwx------  3 john john   25  16-03-13 12:16 am .adobe
drwx------  2 john john   31  16-03-13 12:16 am .aptitude
drwxr-xr-x 25 john john 4.0K  16-03-13 12:16 am .cache
drwxr-xr-x 41 john john 4.0K  24-03-13 08:08 pm .config
drwx------  3 john john   24  30-12-12 01:25 am .dbus
drwxr-xr-x  2 john john 4.0K  26-03-13 10:17 pm Desktop
drwxr-xr-x  5 john john  111  30-12-12 01:18 am .dia
drwxr-xr-x  7 john john 8.0K  26-03-13 10:05 pm Documents
drwxr-xr-x 21 john john 8.0K  26-03-13 10:16 pm Downloads
drwxr-xr-x  2 john john 4.0K  26-03-13 10:17 pm dwhelper
drwxr-xr-x  3 john john   14  30-12-12 01:18 am .e
drwx------  3 john john   27  30-12-12 01:25 am .emacs.d
drwxr-xr-x  3 john john   19  16-03-13 12:16 am .Eterm
drwx------  2 john john 4.0K  30-12-12 01:25 am .filezilla
drwxr-xr-x  2 john john 4.0K  30-12-12 01:18 am .fontconfig
drwxr-xr-x  3 john john 4.0K  30-12-12 01:18 am .fvwm
drwx------  4 john john   31  26-03-13 10:23 pm .gconf
drwx------  4 john john   32  30-12-12 01:25 am .gegl-0.0
drwxr-xr-x 22 john john 4.0K  30-12-12 01:18 am .gimp-2.6
drwx------  6 john john   72  30-12-12 01:25 am .gnome2
drwx------  2 john john    6  30-12-12 01:25 am .gnome2_private
drwxr-xr-x  2 john john   30  30-12-12 01:18 am .gstreamer-0.10
drwx------  2 john john    6  26-03-13 09:37 pm .gvfs
drwxr-xr-x  3 john john   21  30-12-12 01:18 am .icedtea
drwxr-xr-x  2 john john   18  25-02-13 01:32 pm .icewm
drwxr-xr-x  9 john john 4.0K  30-12-12 01:18 am .jedit
drwx------  3 john john   89  30-12-12 01:25 am .kde
drwxr-xr-x  2 john john 4.0K  26-03-13 10:02 pm kernel
drwxr-xr-x  3 john john   14  30-12-12 01:18 am .libreoffice
drwx------  2 john john   43  30-12-12 01:25 am .links2
drwxr-xr-x  4 john john   41  30-12-12 01:18 am .linuxmint
drwxr-xr-x  3 john john   18  25-02-13 02:14 pm .local
drwx------  3 john john   25  30-12-12 01:25 am .macromedia
drwxr-xr-x  3 john john   19  26-03-13 10:17 pm Mail
drwxr-xr-x  2 john john   19  25-02-13 02:14 pm .mate2
drwx------  3 john john   21  30-12-12 01:25 am .mission-control
drwx------  3 john john   95  30-12-12 01:25 am .moc
drwxr-xr-x  4 john john   64  25-02-13 02:14 pm .mozilla
drwxr-xr-x  2 john john   34  30-12-12 01:18 am .mplayer
drwxr-xr-x  7 john john 4.0K  26-03-13 09:38 pm Music
lrwxrwxrwx  1 john john   10  26-03-13 09:38 pm My Downloads -> Downloads/
drwxr-xr-x  2 john john   29  26-03-13 10:15 pm MySQL
drwx------ 20 john john 4.0K  25-03-13 01:19 pm .opera
drwxr-xr-x  7 john john 4.0K  26-03-13 10:15 pm Pash
drwxr-xr-x  4 john john 4.0K  26-03-13 09:38 pm Pictures
drwx------  3 john john   18  30-12-12 01:25 am .pki
drwx------  2 john john   41  30-12-12 01:25 am .prboom
drwxr-xr-x  2 john john  152  26-03-13 10:18 pm Proliant
drwxr-xr-x  3 john john 4.0K  26-03-13 10:15 pm Public
drwx------  2 john john 4.0K  25-02-13 02:29 pm .pulse
drwxr-xr-x  4 john john   30  07-03-13 03:09 pm .shotwell
drwx------  2 john john   24  19-03-13 10:09 pm .ssh
drwxr-xr-x  2 john john    6  26-03-13 10:15 pm Templates
drwx------  5 john john   42  30-12-12 01:25 am .thumbnails
drwx------  4 john john   68  30-12-12 01:25 am .thunderbird
drwxr-xr-x  2 john john 4.0K  26-03-13 10:15 pm Videos
drwxr-xr-x  2 john john   39  30-12-12 01:18 am .wireshark
drwxr-xr-x  4 john john   52  25-03-13 11:50 am .yudit

This command will list all of the terminals and consoles that you are currently logged in at.

finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since"
Logged in since Tue Mar 26 22:10 (EST) on tty2    1 hour 44 minutes idle
Logged in since Tue Mar 26 21:51 (EST) on tty3    1 hour 44 minutes idle
Logged in since Tue Mar 26 22:32 (EST) on pts/0 from :0
Logged in since Tue Mar 26 22:32 (EST) on pts/1 from :0

This useful BASH command will list all processes that do not belong to you.

ps uax | sort | sed -e 's/-[0-9].*$//' | uniq -c | grep -v '${LOGNAME}'

Getting back to ls; this is how to output the directory listings in all uppercase. This might be useful if you pine for the days of IBM DOS…

ls | tr a-z A-Z
TOTAL 0
DRWXRWSR-X 10 ROOT STAFF 105  26-03-13 10:02 PM .
DRWXR-XR-X 10 ROOT ROOT   97  26-03-13 10:00 PM ..
DRWXR-XR-X  2 ROOT ROOT   95  26-03-13 10:23 PM BIN
DRWXRWSR-X  2 ROOT STAFF   6  26-03-13 10:02 PM ETC
DRWXRWSR-X  2 ROOT STAFF   6  26-03-13 10:27 PM GAMES
DRWXRWSR-X  2 ROOT STAFF   6  30-12-12 01:19 AM INCLUDE
DRWXRWSR-X  6 ROOT STAFF  70  26-03-13 10:02 PM LIB
LRWXRWXRWX  1 ROOT STAFF   9  26-03-13 12:57 PM MAN -> SHARE/MAN
DRWXRWSR-X  2 ROOT STAFF  39  26-03-13 10:02 PM SBIN
DRWXRWSR-X 12 ROOT STAFF 130  24-03-13 07:23 PM SHARE
DRWXRWSR-X  2 ROOT STAFF   6  30-12-12 01:19 AM SRC

The –color=auto output even works with the most basic sh shell. But you need a terminal that can display color obviously. If you are using an old VT100 then the display might not be more than one color.

ls -hula --color=auto
total 3.9M
drwxr-xr-x   4 root root  105  26-03-13 11:37 pm .
drwxr-xr-x 437 root root  12K  26-03-13 01:20 pm ..
-rw-r--r--   1 root root 1.8M  29-10-12 10:02 am albatross-wall-source.svg
-rw-r--r--   1 root root  97K  29-10-12 10:02 am bluebird.svg
-rw-r--r--   1 root root 2.0M  29-10-12 10:02 am greybird.svg
drwxr-xr-x   3 root root   44  30-12-12 01:20 am joy
drwxr-xr-x   3 root root   44  30-12-12 01:20 am joy_inksplat

The /bin/sh shell is not much fun to use though, better to use the bash or zsh shells instead. Even csh or tcsh are better.

No comments have been made. Use this form to start the conversation :)

Leave a Reply