Is there a way to dynamically refresh the less command?
I like the watch command, but it has its limitations. I’m curious to know whether I could mimic the functionality of watch with less . I’m mainly looking for the ability to scroll through my directory as it dynamically gets modified via a running script.
I am looking for tail -f foo.log | grep bar , and to be able to dynamically change the grep command with restarting the tail/grep pipeline.
11 Answers 11
In less , you can type F ( Shift + F ) to keep reading at the end of a file (like tail -f ); you can type :e and a file name to view a different file, but unfortunately, if you enter the name of the current file, less doesn’t reload the file. However there’s a trick to make it re-read the current file, suggested by sabgenton: type :e and enter the name of a non-existent file; that causes less to display an error message and then reload the current file.
If you’re looking for an alternative to watch ls , here are a few:
- Modern file managers (e.g. Nautilus, Thunar, Konqueror, Dolphin, Finder) refresh views in real time.
- Emacs doesn’t have real-time refresh, but with auto-revert-mode , it will reload the file or directory every 5 seconds (the delay is configurable).
- Although w3m is primarily a web browser, it makes a passable directory and text file viewer. Press R to reload the (local) URL.
For me :e randomnamejkdlfjldf fails to load the non-existent filename then reloads back to the current file 😀
@sabgenton Thanks, I didn’t know that. That’s a great trick. I added it to my answer, but you could post it as an answer of your own (which I hope Zaid would accept).
@CMCDragonkai No, less +F only watches for appended content. Use :e nonexistentfile to re-read completely changed input.
@PaulWagland No, R doesn’t reliably refresh the file. It refreshes the screen, and wipes some input buffers, but it doesn’t actually reload data from the file in all circumstances (only if the file is large? I didn’t dig deeply into the code).
Shift+F will make less similar to the command tail -f . That is, it gets refreshed if more data is appended to the file.
Note that this only works for appends. If lines are removed or edited in-place, less won’t show those changes.
balki My experience is that this kills the command piping the data if you are using $COMMAND | less . I wonder if there is a nice way around this? You can do stuff like $COMMAND > /tmp/file & less /tmp/file but that’s not very nice.
@AttRigh I never killed my command (however i read logs of running commands this way, i don’t pipe into less, technically)
This emulates pressing «F» within the editor.
man pages can be very informative. Don’t be intimidated by them. Among everything else, man less says you can use the R command to:
R Repaint the screen, discarding any buffered input. Useful if the file is changing while it is being viewed.
(I realize this question is over 6 years old, but it comes up on google searches, so I’m not the only one that clicked the link to get here.)
This repaints the screen in case it was disrupted by output from another program. It doesn’t consistently reload the file. I haven’t dug deeply into the code to see what it does, experimentally short files don’t get reloaded. I am definitely not intimidated by man pages and I say this description is poorly worded: “repaint” does not convey that input is re-read, and the actual behavior does not quite fit “useful if the file is changing” (maybe it’s a bug? I don’t know since I don’t know what the command is supposed to do)..
I’m no expert, and haven’t read less’s source code either, but what it does is forget what it already knows about the file & reloads it, refreshing its output based on the input it reloads. The question reads «Is there a way to dynamically refresh the less command?» If I had read the question’s explanation more thoroughly before answering, I might not have answered, since they go on to say they want «to scroll through my directory as it dynamically gets modified via a running script.» less wouldn’t be the right command for that.
Works fine for me, even when using files 3 bytes long. I can’t imagine why it wouldn’t work for larger files too.
Great! For me it works for both short and long files. I just noticed two possible problems: 1. less did not reopen the file using its filename — i.e. when the inode of the file changed (like in overwriting the file using mv ) then the old file content stayed (probably the old inode was reread) — 2. The first displayed line probably starts at the same byte offset as in the original file content. So when the line lengths change the first displayed line could be incomplete and the displayed lines could move up / down.
How to refresh in Ubuntu?
One of the best things about Linux is diagnosing just about everything on the terminal. Whether it’s system errors or a simple desktop environment refresh, you need to type in a few commands, and you’re good to go.
Restarting our computers can often take a lot of time, even with SSDs speeding up the process as the computer has to load all the required files and any programs required to run at boot in the memory. If you’ve accidentally broken something, resetting your desktop can be quicker.
In this article, we’re looking at how you can refresh different desktop environments in Ubuntu and other Linux distros without having to restart your PC.
Refreshing Ubuntu desktop environments
There are two major Linux desktop environments that we’ll be covering.
GNOME
GNOME has been the default Ubuntu launcher since Ubuntu 17.10. You can reset the desktop environment using either the GUI or the terminal.
If you’re using the GUI, follow these steps:
Step 1: Press Alt + F2 to open the command box. Note that if you’re on a laptop, you may also have the press the Fn key if the aforementioned key combination doesn’t work.
Step 2: Type r and hit enter.
If you’d rather use the terminal, just run the following command.
gnome-shell --replace & disown
On Unity
Follow these steps if you’re running an older version of Ubuntu that uses Unity as the desktop environment.
Step 1: Press Alt + F2 to open the command box. Note that if you’re on a laptop, you may also have the press the Fn key if the aforementioned key combination doesn’t work.
Step 2: Type unity and hit enter.
Resetting desktop on other Linux distros
If you’re using any other Linux distro, here’s how you can reset your desktop.
Cinnamon
Cinnamon provides an easy way to reset the desktop straight from the GUI. All you have to do is right-click any desktop panels, head over to Troubleshoot and click Restart Cinnamon.
Alternatively, you can also use the Ctrl + Alt + Esc keyboard shortcut to reset your desktop. Do keep in mind that this shortcut is exclusive to Cinnamon.
KDE Plasma
Unlike GNOME or Cinnamon, you will have to resort to the command line to reset your desktop here. Just enter the following command in the terminal and hit enter.
kquitapp5 plasmashell && kstart5 plasmashell
As soon as you hit enter, your desktop will go blank and then restart. The whole process might take a couple of seconds. If you’re using a lot of Plasma applets, you might want to refresh your configuration file as well. You can do that by using the following command.
mv ~/.config/plasma-org.kde.plasma.desktop-appletsrc old-configuration
Restart your desktop again to see any changes.
Xfce
Resetting the Xfce desktop is a two-step process. First, you have to restart all the panels and the window manager itself.
xfce4-panel -r && xfwm4 --replace
However, at times simply resetting the desktop environment might not be enough. You can reset the desktop to its default state by using the commands below.
xfconf-query -c xfce4-panel -p / -R -r xfce4-panel -r
LXDE
Use the following command to restart the LXDE desktop environment.
lxpanelctl restart && openbox --restart
Alternatively, if you want to reset the desktop to its default state, you can use the following command to move the configuration files forcing LXDE to regenerate them.
mv ~/.config/lxpanel lxpanel.bak
Yadullah Abidi
Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he’s not, he streams himself racing virtual cars. He’s been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: [email protected]
reset command in Linux with Examples
reset command in Linux system is used to initialize the terminal. This is useful once a program dies leaving a terminal in an abnormal state. Note that you may have to type reset to get the terminal up and work, as carriage-return may no longer be work in the abnormal state. Also, the terminal will often not going to echo the command. Syntax:
reset [-IQVcqrsw] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]
Example: Suppose we have a terminal screen with a bunch of commands written on the screen and when we will use the reset command then we will have initialized terminal screen as shown below: Before using reset command After using reset command Options:
- –help: It will print the general syntax of the command along with the various options that can be used with the reset command as well as gives a brief description about each option.
- reset -c: Sets the control characters and modes.
- reset -e: This option set the erase character to ch.
- reset -l: This option do not send the terminal or tab initialization strings to the terminal.
- reset -i: This option set the interrupt character to ch.
- reset -k: This option set the line kill character to ch.
- reset -m: This option specify a mapping from a port type to a terminal.
- reset -Q: This option do not display any values for the erase, interrupt and line kill characters. Normally,test displays the values for the control characters which is differ from the system’s default values.
- reset -q: The terminal type is been displayed to the standard output, and the terminal is not been initialized in any way. The option ‘-’ by itself is itself equivalent but archaic.
- reset -r: This option Prints the terminal type to the standard error output.
- reset -s: This option prints the sequence of shell commands to initialize the environment variable TERM to the standard output.
- reset -V: This option reports the version of ncurses which was used in this program, and exits.
- reset -w: This option resizes the window to match the size deduced via setupterm. Normally this has no effect, unless setupterm is not been able to detect the window size.