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?
- 8,900
- 36
- 90
- 133
2 Answers
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
- 2,984
- 4
- 27
- 30
-
man actually changes CWD, so you would need to use the full path. – Rob Dec 19 '12 at 18:46
-
@Rob - What do you mean that `man actually changes CWD`? – PeanutsMonkey Dec 19 '12 at 18:51
-
I have tmux set up to show my CWD in my status bar. Doing `man man` (or anything) will change my CWD while man is open. My linux machine isn't available at the moment, and I'm not sure of any other way to check CWD, but to me it seems like that's what it's doing. – Rob Dec 19 '12 at 19:56
-
@Rob - So you mean to say that just because you are using tmux, it changes the current working directory when you run the command `man {command}` from say `/home/{user}` to a location of the command `man`? – PeanutsMonkey Dec 19 '12 at 20:54
-
That would explain the behaviour I saw. Now, _why_ it's changing the CWD, that's another question... – Ash Dec 19 '12 at 21:24
-
@PeanutsMonkey I assume it would change directory anyway, but I can see it with tmux. – Rob Dec 20 '12 at 16:59
-
@Rob - Sorry for being such a n00b Rob but I still don't get, what changes directory and from what to what? – PeanutsMonkey Dec 20 '12 at 18:24
-
@Rob, @PeanutsMonkey: I've updated my answer to try and capture the difference between ``man`` and using ``less`` normally. – Ash Dec 21 '12 at 05:37
The generic way to do this is by suspending the current job, executing the command and resuming the old job.
ls | less (read text, notice the filename)
Control-z to suspend the current active command
You should get a line similar to this:[1]+ Stopped ls | less
([1] is the job number.)
rm testfile
fg or fg %1 (the 1 is the job number)
You can suspend multiple processes at the same time. E.g.
ls | less
Control-z (output: [1]+ Stopped ls | less)
man rm
Control-z (output: [2]+ Stopped man rm)
rm -i testfile*
fg %1 to resume job 1 (leaving the man page open in the background), or
fg %2 to resume job 2 (man rm)
If you have multiple suspended processes you can list them with jobs.
- 64,768
- 7
- 111
- 168