Linux – How to execute a Linux command whilst using the less command or within the man pages

lesslinuxman

I generally pipe the command ls with less and would like to execute a command while it is paging e.g. I come across a file that I would like to delete so I would like to execute the command rm {filename} whilst still paging. I would also like to hope I can use the same method while perusing man pages. If not how is it different?

Best Answer

You can access the command line using bang (!) within less.

So for example, if you type:

touch temp.txt
ls | less
!rm temp.txt

And temp.txt should be gone.

Edit: By default it seems that man now uses less to page (for some reason I thought it used more, maybe in the past it did). You can use the same trick, but it requires the full path (eg. /home/user/...) to get it to work.

This is because invoking man changes the current working directory. On my machine (xubuntu, using xfce-terminal) it goes to /usr/share/man. If your console displays the CWD you can see it change, or you can see it from within man by entering:

!pwd
Related Question