Linux/Unix desktop fun: sl – a mirror version of ls

in Categories Command Line Hacks last updated March 31, 2018

One of the most common mistakes is typing sl instead of ls command. I set up an alias, i.e., alias sl=ls; but then you may miss out the steam train with a whistle.

sl is a joke software or classic UNIX game. It is a steam locomotive runs across your screen if you type “sl” (Steam Locomotive) instead of “ls” by mistake. Now there is a twist to older sl command.

sl – a mirror version of ls

From the blog post:

I didn’t like it and made another program of the same name. My sl just mirrors the output of ls. It accepts most ls(1) arguments and is best enjoyed with -l.

source code

The program is written in the bash shell. Here is the source code:

#!/bin/bash
# sl - prints a mirror image of ls. (C) 2017 Tobias Girstmair, https://gir.st/, GPLv3
 
LEN=$(ls "$@" |wc -L) # get the length of the longest line
 
ls "$@" | rev | while read -r line
do
	printf "%${LEN}.${LEN}s\\n" "$line" | sed 's/^\(\s\+\)\(\S\+\)/\2\1/'
done

Run it as follows

First create ~/bin/ directory using the mkdir command:
$ mkdir ~/bin/
Next, store above source code. cd into the ~/bin/ using the cd command:
$ cd ~/bin/
$ vi sl

Save and close the file. Set permission on your shell script using the chmod command:
$ chmod +x sl
Test it:
$ ls -l
$ ./sl -l

Sample outputs from sl command:

txt.qaf.detaeler.km >- txt.smc.detaeler.km 05:41 32 ceD 91    keviv keviv 1 xwrxwrxwrl
qaf.detaeler.km                            72:41 11 ceD 709   keviv keviv 1 x-rx-rxwr-
etalpmet.qaf.detaeler.km                   34:51 61 voN 121   keviv keviv 1 --r--r-wr-
txt.qaf.detaeler.km                        85:00 01 beF 014   keviv keviv 1 --r--r-wr-
spit.detaeler.km                           94:41 32 ceD 709   keviv keviv 1 x-rx-rxwr-
etalpmet.spit.detaeler.km                  84:41 32 ceD 121   keviv keviv 1 --r--r-wr-
ssr.setadpu.km                             95:00 7  naJ 618   keviv keviv 1 x-rx-rxwr-
etalpmet.ssr.setadpu.km                    24:22 2  naJ 463   keviv keviv 1 --r--r-wr-
txt.ssr.setadpu.km                         22:12 02 beF 4221  keviv keviv 1 --r--r-wr-
hs.014.xnign                               43:11 6  naJ 684   keviv keviv 1 x-rx-rxwr-
hs.103.moc.tfarcxin                        5102  52 rpA 631   keviv keviv 1 x-rx-rxwr-
etacsufbo                                  5102  91 luJ 9931  keviv keviv 1 --r--r-wr-
hs.lapyap                                  84:41 02 ceD 865   keviv keviv 1 x-rx-rxwr-
txt.lapyap                                 7102  03 naJ 4131  keviv keviv 1 --r--r-wr-
hs.daolputsop                              3102  13 ceD 135   keviv keviv 1 x-rx-rxwr-
hs.daolpuerp                               3102  13 ceD 734   keviv keviv 1 x-rx-rxwr-
hs.niamod.eralfduolc.lla.egrup             7102  81 yaM 6401  keviv keviv 1 x-rx-rxwr-
nohtyp                                     05:20 5  beF 6904  keviv keviv 2 x-rx-rxwrd
ls                                         92:61 13 raM 672   keviv keviv 1 x-rx-rxwr-
resu.tidder.ecruos                         7102  42 naJ 911   keviv keviv 1 x-rx-rxwr-
014.deteled.sgat                           95:32 02 raM 97732 keviv keviv 1 --r--r-wr-
hs.teewt                                   53:10 62 naJ 58653 keviv keviv 1 x-rx-rxwr-
tob-rettiwt                                90:32 4  beF 6904  keviv keviv 2 x-rx-rxwrd
smc.elif.daolpu                            7102  9  nuJ 907   keviv keviv 1 x-rx-rxwr-
qaf.elif.daolpu                            7102  9  nuJ 807   keviv keviv 1 x-rx-rxwr-
pit.elif.daolpu                            7102  9  nuJ 907   keviv keviv 1 x-rx-rxwr-
hs.egamidaolpu                             3102  81 tcO 3911  keviv keviv 1 x-rx-rxwr-
nalnoekaw                                  00:41 21 tcO 1325  keviv keviv 1 x-rx-rxwr-
2x                                         7102  52 nuJ 017   keviv keviv 1 x-rx-rxwr-

How to setup bash shell alias

The syntax is:
alias name=value
Add the following to the ~/.bashrc file:
echo 'alias sl="/home/$USER/bin/sl -l"' >> ~/.bashrc
Load it:
$ source ~/.bashrc
Test it:
$ sl
sl - a mirror version of ls command

How to verify sl command execution path

Use the type command or command command as follows:
$ type -a sl
sl is aliased to `/home/vivek/bin/sl -l'
sl is /home/vivek/bin/sl
sl is /usr/games/sl

$ command -V sl
alias sl='/home/vivek/bin/sl -l'

You can temporarily disable an alias using any one of the following method:
"command"
\command"
\sl
\ls
command ls
command sl

For more info see this page.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):

Start the discussion at www.nixcraft.com