Improving default ls behavior
Jun 27, 2017 · 498 words · 3 minutes read
One of the first bash commands everyone learns is ls
— to list directory contents.
Default ls
is ok but not super great.
Here is the default output:
root@21c5143c6ed7:/# ls
bin boot dev etc home lib lib64 media mnt opt
proc root run sbin srv sys tmp usr var
The default output prints the directory contents on a line wrapped to the width of the screen. This means it’s really wide, and even though there is spacing, it’s hard to read. Plus the only output is the content names — theres no other information (e.g. file sizes, permissions, etc.).
Making it better
There are several options that improve the output. From the man page:
-a Include directory entries whose names begin
with a dot (.).
-h When used with the -l option, use unit suffixes:
Byte, Kilobyte, Megabyte, Gigabyte, Terabyte
and Petabyte in order to reduce the number of
digits to three or less using base 2 for sizes.
-l (The lowercase letter ``ell''.) List in long
format. (See below.) If the output is to a
terminal, a total sum for all the file sizes
is output on a line before the long listing.
These options do a few things, namely prints the entire directory contents (including hidden files/directories) in a long list format (one item per line) with human-readable file sizes. So now the output looks like this:
root@21c5143c6ed7:/# ls -ahl
total 72K
drwxr-xr-x 33 root root 4.0K Jun 21 02:05 .
drwxr-xr-x 33 root root 4.0K Jun 21 02:05 ..
-rwxr-xr-x 1 root root 0 Jun 21 02:05 .dockerenv
drwxr-xr-x 2 root root 4.0K Aug 14 2015 bin
drwxr-xr-x 2 root root 4.0K Apr 10 2014 boot
drwxr-xr-x 5 root root 380 Jun 21 02:05 dev
drwxr-xr-x 64 root root 4.0K Jun 21 02:05 etc
drwxr-xr-x 2 root root 4.0K Apr 10 2014 home
drwxr-xr-x 12 root root 4.0K Aug 14 2015 lib
drwxr-xr-x 2 root root 4.0K Aug 14 2015 lib64
drwxr-xr-x 2 root root 4.0K Aug 14 2015 media
drwxr-xr-x 2 root root 4.0K Apr 10 2014 mnt
drwxr-xr-x 2 root root 4.0K Aug 14 2015 opt
dr-xr-xr-x 180 root root 0 Jun 21 02:05 proc
drwx------ 2 root root 4.0K Jun 21 02:05 root
drwxr-xr-x 7 root root 4.0K Aug 14 2015 run
drwxr-xr-x 2 root root 4.0K Aug 20 2015 sbin
drwxr-xr-x 2 root root 4.0K Aug 14 2015 srv
dr-xr-xr-x 13 root root 0 Jun 21 02:05 sys
drwxrwxrwt 2 root root 4.0K Aug 14 2015 tmp
drwxr-xr-x 11 root root 4.0K Aug 20 2015 usr
drwxr-xr-x 12 root root 4.0K Aug 20 2015 var
As you can see, this output is much more informative. It includes the file/directory permissions, number of links, owner name, owner group, file size (human-readable format), last modified timestamp, and name.
Since these flags add so much more information, I add this alias to my .bashrc:
alias ls='ls -ahl --color=auto'
Note that --color=auto
is implicit on some systems but not all, so I specify it here.