How to activate pagination for ls command?

What is the equivalent option for the ls command to activate pagination as in DOS the dir /p does?


Solution 1:

There's no straightforward equivalent in ls itself, but there's the less utility, which will format the output of any command as seperate pages, scrollable by line or page:

ls -C | less

Where -C triggers column display. Use lah as arguments (ls -lah) to get a line by line display with all files being displayed (include hidden ones), and human readable filesizes.

  • To get colours to show up properly, you need to add the --color=always argument to ls, and the -R argument on less*:

    ls -C --color=always | less -R

    alt text
    this shows 'ls -ah --color=always | less -R'

In contrast to more, less will let you scroll through the output. It's also a bit faster for very large listings.

The pipe works like this:

Every program has input and output, a pipe redirects the output of one program (ls) to the input of another program (less). And less simply expects input which it then formats.

  • A more old-school dos equivalent would be pg:

    ls | pg
    

You can also

  • Use ls | head or ls | tail to display only the first or last part of the output
  • Use watch "ls" to keep the display open, updating it every few seconds to watch changes
  • Use banner $(ls) if you're sitting really far away from the screen. (;

  • If you find all of that too long to remember, you can set up an alias for it:

    Open ~/.bash_aliases with a text editor and add something like this to it:

    alias lsp="ls -ah --color=always | less -R"
    

    (this is a script that is run every time a new virtual terminal is started up, you should set up all your permanent aliases there)

    Now you can just type lsp, or whatever name you choose.

    If you want to be able to pass further arguments to your alias, we need to define a function instead:

    lsp(){ ls -ah --color=always "$@" | less -R; }
    

    A function, principally looking like this: name(){ commands; }; can accept arguments, $1, $2, $3 and so on. $@ means "every argument, if any".

    You can now run something like lsp *.py, lsp -C, and so on. We insert the arguments at the point where they get passed to ls. We could also have inserted $* for less, if it were the important command. You can see all of ls' arguments at man ls (worth a read).


*: The reason for this is, that whenever you Pipe something, it detects a Terminal (actually the other program) not capable of displaying colour. "--color=always" forces ls to ignore this. The -R switch makes less repaint the screen, escaping the colours properly.

Solution 2:

I am not sure if there is some ls command for pagination. However, you may use a pipe and less, like this:

ls | less

And use q to exit.

Solution 3:

Try ls | less or ls | more. The second one is close to the DOS version.