Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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

share|improve this answer
    
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 at 22:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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