An off the wall idea
Disclaimer: This is going to be overkill for most of you. But I've been doing this since 1995 and I have the skills to make my world exactly how I want it. So, I why not?
I really like using different sorting methods with ls
, especially -rt
(which is sort by r
everse modified t
ime). So, I decided to try something crazy and use awk to do my sorting.
# save as ~/.ls.awk
# inpsired by http://superuser.com/questions/448291/how-can-i-make-ls-show-dotfiles-first
{
if($1 == "total"){
print $0
next
}
# may need to adjust $9 to match your name column
if(match($9, /^(\033\[[0-9]*m)*\./)) # optionally look past xterm highlighting like: ^[[34m
df[++dd] = $0
else
nf[++nn] = $0
}
END{
while (++d in df)
print df[d]
while (++n in nf)
print nf[n]
}
Now instead of defining a bash alias, I define a bash function (because aliases can only append arguments at the end, but functions can use them anywhere)
ll(){ CLICOLOR_FORCE=1 ls -lhA $* | awk -f ~/.ls.awk; }
To see the results
Let's create some sample files:
for n in 4 .4 3 .3 2 .2 1 .1; do touch $n; sleep 1; done
Using plain ls
$ ls -lA
total 8
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .1
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .2
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .3
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .4
-rwxr-xr-x 1 bbronosky staff 285 Nov 29 13:14 .ls.awk
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 1
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 2
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 3
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 4
$ ls -lArt
total 8
-rwxr-xr-x 1 bbronosky staff 285 Nov 29 13:14 .ls.awk
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 4
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .4
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 3
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .3
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 2
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .2
-rwxr-xr-x 1 bbronosky staff 0 Dec 1 00:25 1
-rw-r--r-- 1 bbronosky staff 0 Dec 1 00:25 .1
Using my function that filters with awk
$ ll
total 8
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .1
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .2
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .3
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .4
-rwxr-xr-x 1 bbronosky staff 285B Nov 29 13:14 .ls.awk
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 1
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 2
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 3
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 4
$ ll -rt
total 8
-rwxr-xr-x 1 bbronosky staff 285B Nov 29 13:14 .ls.awk
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .4
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .3
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .2
-rw-r--r-- 1 bbronosky staff 0B Dec 1 00:25 .1
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 4
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 3
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 2
-rwxr-xr-x 1 bbronosky staff 0B Dec 1 00:25 1
You can see my implementation of this here https://github.com/RichardBronosky/dotfiles/commit/6170c0a9
What's most important about this is that it is a framework for tweaking your ls
output. You can do anything you want with that awk filter. You might want it to be directories, then dotfiles, then everything else. Once you know how to handle your xterm colors, it's not too difficult. It's totally up to you.