What is the equivalent option for the ls
command to activate pagination as in DOS the dir /p
does?
5 Answers
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 tols
, and the-R
argument on less*:ls -C --color=always | less -R
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
orls | 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 atman 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.
-
thanks great answer, exactly the part with the alias came to my mind when i saw the long commandline. the only disadvantage by creating the alias is that using a wildcard filter like *.pdf this isn't possible? perhaps does there exist also a solution for this?– NESJan 2, 2011 at 14:02
-
Everything is possible ;-) I added the bit about arguments, I somehow didn't think of it earlier. Jan 2, 2011 at 14:22
-
2you gave more than expected :) thanks for the detailed answer. P.S. do you know if there are differences between bash and fish that somehow prevent this alias argument passing method from working in fish? i got the alias without the argument option to work here.– NESJan 2, 2011 at 14:47
-
1Ok, here askubuntu.com/questions/19728/… the new question starts, thanks again for your helpful answers so far.– NESJan 2, 2011 at 15:14
-
1
$*
and$@
do the exact same thing. They expand to$1 $2 $3 ...
. Quoted, however,"$@"
expands to"$1" "$2" "$3" ...
while"$*"
expands to"$1 $2 $3 ..."
(that is, all arguments into one string, using the first character ofIFS
as separator). mywiki.wooledge.org/BashGuide/Parameters– geirhaFeb 3, 2011 at 13:12
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.
Try ls | less
or ls | more
. The second one is close to the DOS version.
-
1Is there perhaps another method? because the disadvantage is for me that the colorization of the shell isn't active when using a pipe?– NESJan 2, 2011 at 13:41
-
2
echo 'alias ls="ls --color=always"' >> ~/.bash_aliases
echo 'lsc(){ ls -C "$@" | less -R; }' >> ~/.bashrc
source ~/.bash_aliases && source ~/.bashrc
Pagination can be done by using the following command.
$ ls
Above command will print the out in a scrollable format.
$ ls | pager
Above command will print the out in a paginated format.
Some extra pointers:-
- Use spacebar to go to next page.
- Use q to exit the pagination.