Ubuntu – How to modify the `ls` command

command line

When I type ls, i get the normal list, or when I type ls -la I get a details version with everything…

But I hate the layout of ls, it's just unintuitive for most of my use.

Is there some way I can modify (without changing the sorcecode of ls, bash scripts are okay however) ls so it's display instead of

-rwxr-xr-x 13 user group 123546 2011-01-01 11:11 filename0
-rwxr-xr-x 13 user group 123546 2011-01-01 11:11 filename1
-rwxr-xr-x 13 user group 123546 2011-01-01 11:11 filename2
drwx------ 13 user group 123546 2011-01-01 11:11 directory0
-rwxr-xr-x 13 user group 123546 2011-01-01 11:11 filename3
drwx------ 13 user group 123546 2011-01-01 11:11 directory1

I can get a list more like:

DIRS
directory0 1293880260 700 user:group
directory1 1293880260 700 user:group

FILES
filename0 1293880260 751 user:group
filename1 1293880260 755 user:group
filename2 1293880260 777 user:group
filename3 1293880260 705 user:group

Or some other variation.

Meanwhile, preserving ability to use flags and other options.

Best Answer

You could write a bash script called ~/bin/ls that should override /bin/ls. remember to run chmod +x ~/bin/ls.

I've just written this which seems to do most of what you want to accomplish (including passing along extra arguments)

#!/bin/bash

DIRS="`/bin/ls --color=auto -l $@ | grep ^d`"
FILES="`/bin/ls --color=auto -l $@ | grep ^\-`"

if [ "$DIRS" ]
then
    echo "DIRECTORIES"
    echo -e "$DIRS\

"
fi

if [ "$FILES" ]
then
    echo "FILES"
    echo "$FILES\

"
fi

Somebody might want to tidy that up a little or improve the output formatting, but there you go. Yours to do whatever you like with.

And here's some genuine sample output:

ls
DIRECTORIES
drwxr-xr-x 4 oli oli     4096 2010-12-16 15:40 markitup
drwxr-xr-x 7 oli oli     4096 2011-01-16 16:58 media
drwxr-xr-x 3 oli oli     4096 2010-12-16 15:41 post
drwxr-xr-x 9 oli oli     4096 2010-09-16 05:23 templates

FILES
-rw-r--r-- 1 oli oli  5361664 2010-09-06 16:32 db.db
-rw-r--r-- 1 oli oli        0 2008-12-11 09:22 __init__.py
-rwxr-xr-x 1 oli oli      542 2008-12-11 09:22 manage.py
-rw-r--r-- 1 oli oli       13 2010-03-23 18:14 settingsdev.py
-rw-r--r-- 1 oli oli     2642 2010-12-16 15:40 settings.py
-rw-r--r-- 1 oli oli     1818 2010-12-16 15:40 urls.py
-rw-r--r-- 1 oli oli      432 2010-06-22 20:54 views.py

And with arguments:

ls -a
DIRECTORIES
drwxr-xr-x  8 oli oli     4096 2011-01-12 00:46 .
drwxr-xr-x 19 oli oli     4096 2011-04-13 17:24 ..
drwxr-xr-x  6 oli oli     4096 2010-02-03 13:50 .bzr
drwxr-xr-x  4 oli oli     4096 2010-12-16 15:40 markitup
drwxr-xr-x  7 oli oli     4096 2011-01-16 16:58 media
drwxr-xr-x  3 oli oli     4096 2010-12-16 15:41 post
drwxr-xr-x  9 oli oli     4096 2010-09-16 05:23 templates

FILES
-rw-r--r--  1 oli oli       65 2010-03-27 07:58 .bzrignore
-rw-r--r--  1 oli oli  5361664 2010-09-06 16:32 db.db
-rw-r--r--  1 oli oli        0 2008-12-11 09:22 __init__.py
-rwxr-xr-x  1 oli oli      542 2008-12-11 09:22 manage.py
-rw-r--r--  1 oli oli       13 2010-03-23 18:14 settingsdev.py
-rw-r--r--  1 oli oli     2642 2010-12-16 15:40 settings.py
-rw-r--r--  1 oli oli     1818 2010-12-16 15:40 urls.py
-rw-r--r--  1 oli oli      432 2010-06-22 20:54 views.py