Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to pass a command alias/function without reducing the functionality of said command?

Tags:

linux

bash

unix

I have an function ll that currently expands into this:

function ll () 
{ 
    ls -lh --color "$@" | grep "^d";
    ls -lh --color "$@" | grep "^-" | grep -v "~";
    ls -lh --color "$@" | grep "^l"
}

What this does is sort the listed folder into showing directories first, then files, then links. However, I find that such approach reduces the functionality of the ls command, for instance if I try to call ll /bin /tmp, I will get a mix of files from both folders.

Is there a general rule of thumb to pass command aliases/functions such that full functionality of those commands is not reduced? If there isn't, how can I fix my ll command so that I retain the sorting, but full functionality of ls is not lost?

Please note that I currently have bash version 3.2.25(1)-release on my system (ls version 5.97), so --show-directories-first flag is not available to me.

EDIT:

This is the function I ended up using, I modified it slightly so that ll would work without any args:

function ll () {
  if [ $# -eq 0 ]; then set -- .; fi
  for d; do
    ls -lh --color "$d"|awk '$1~/^d/{i=0} $1~/^l/{i=1} $1~/^-/{i=2} NF>2{print i OFS $0}' | sort -n -k1,1 | cut -d ' ' -f2-
  done
}
like image 825
IDDQD Avatar asked Jan 12 '23 12:01

IDDQD


1 Answers

Handle each argument to ll separately:

function ll () 
{ 
    for d in "$@"; do
        ls -lh --color "$d" | grep "^d";
        ls -lh --color "$d" | grep "^-" | grep -v "~";
        ls -lh --color "$d" | grep "^l"
    done
}
like image 120
chepner Avatar answered Jan 16 '23 02:01

chepner