9

I have a number of files with very long names. Please, is there a way to use ls -C and truncate the file names to get more columns for an overall view?

Or is there a better way to get a compact listing?

9

You could do something like:

ls | cut -c1-20 | columns -W "${COLUMNS:-80}"

columns example

(that's columns with an s from GNU autogen). Or:

ls | cut -c1-20 | column -c"${COLUMNS:-80}"

column example Using column as found on BSDs or in bsdmainutils on Debian or its derivatives.

zsh also has support to print things in columns, so you could define a function like:

setopt extendedglob
c() print -rC$[COLUMNS/(($1)+2)] -- "${(M)@[2,-1]##?(#c0,$[$1])}"

And use it as:

c 20 *.txt

To print the list txt files in columns, truncated to 20 characters.

And to make it a bit crazier, you could add:

command_not_found_handler() {(($1)) && c "$@"}

That way, you can also do:

20 *

Or even:

8+8 *

zsh example

  • Brilliant, thank you. On my Fedora 12 system I used the ls | cut -c1-20 | columns -W "${COLUMNS:-80}" line, and got all my files on one screen. – Harry Weston Feb 3 '14 at 22:48
0

Assuming an 80 column screen, I like to use

ls -F | perl -lne 's/(.{34}).*/$1... /s ; print' | column -x | more

enter image description here

use ls -FC instead to sort by recency


(Sidenote: my ls already scrolls because it's aliased to ls -CF --color | more -r. Not sure that's best practice...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.