0

when I call ls in ~ i get

Documents  Downloads  Templates  Desktop  Music  Videos  Public  Pictures

If i pipe ls to head (e.g. ls | head -30) i get

Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos

I am trying to alias ls to ls | head -30 in order to not spam my terminal when doing ls in a big folder. The problem is that i prefer the first formatting (from normal ls).
Is there any way I can keep the original formatting while limiting the number of results?

Teiem
  • 103

2 Answers2

2

this works:

unbuffer ls | head

it tricks the ls command, to behave like it's writing to a terminal

1

As the man page for ls describes:

-C    list entries by columns

So,

alias ls='ls -C | head -30'

Beware that such an alias will preclude you from being able to pass any parameters to ls. For example:

ls /tmp/

will likely not do what you expect. You may find that a shell function is a better choice than an alias.

Jim L.
  • 7,997
  • 1
  • 13
  • 27
  • First of all tank you, your solution works perfectly :) Secondly, I have never written a shell function, do you by chance know if such a function already exists somewhere or where I could look for one? – Teiem Nov 19 '21 at 21:15
  • 1
    @Telem There are numerous questions on Unix.StackExchange regarding that, many of them duplicates of each other. See this one and its related and duplicate links. – Jim L. Nov 19 '21 at 21:23