Pergunta

I'm listing just the file basenames with an ls command like this, which I got from here:

ls --color -1 . | tr '\n' '\0' | xargs -0 -n 1 basename

I would like to list all the directories in the first column, all the executables in the next, all the regular files last (perhaps also with a column for each extension).

So the first (and main) "challenge" is to print multiple columns of different lengths.

Do you have any suggestions what commands I should be using to write that script? Should I switch to find? Or should I just write the script all in Perl?

I want to be able to optionally sort the columns by size too ;-) I'm not necessarily looking for a script to do the above, but perhaps some advice on ways to approach writing such a script.

Foi útil?

Solução

#!/bin/bash

width=20

awk -F':' '

/directory/{
  d[i++]=$1
  next
}

/executable/{
  e[j++]=$1
  next
}

{
  f[k++]=$1
}

END{
  a[1]=i;a[2]=j;a[3]=k
  asort(a)
  printf("%-*.*s | \t%-*.*s | \t%-*.*s\n", w,w,"Directories", w,w,"Executables", w,w,"Files")
  print "------------------------------------------------------------------------"
  for (i=0;i<a[3];i++)
    printf("%-*.*s |\t%-*.*s |\t%-*.*s\n", w,w,d[i], w,w,e[i], w,w,f[i])
}' w=$width < <(find . -exec file {} +)

Sample output HERE

This can be further improved upon by calculating what the longest entry is per-column and using that as the width. I'll leave that as an exercise to the reader

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top