Search code examples
bashshellzsh

How to take first n rows of `ls` command


ls command outputs it's result by rows:

AndroidSandbox/                    kotlin-native-intellij/
arc-theme-macos/                   kotlin-native-sandbox/
BobkoLauncher/                     kotlin-ref-delegation/
...

How to take first n lines of this output? ls | head -n 2 doesn't work because it will take two first files but I need two first lines of output.


Solution

  • Another note: both -C and -x when piped will use a default screen width. So you might want:

    ls -C --width="$(tput cols)" | head