I want to know exactly what the command watch is and how it is used to watch files and folders. How do you operate it from a command-line terminal?
- 3,409
2 Answers
From man watch
watch - execute a program periodically, showing output fullscreen
Say you want to monitor your network device status, you can use in terminal,
watch -n 2 nmcli dev
It will give you output as below which will be updated in every 2 seconds (as I used -n 2)
Every 2.0s: nmcli dev Sat Jan 18 23:09:35 2014
DEVICE TYPE STATE
eth0 802-3-ethernet connected
eth1 802-11-wireless unavailable
If you want to keep watch on the changes of your files folders in a directory, use
watch -n 5 ls /path/to/directory
It will show you the list of files and folders in that directory which will be updated in every 5 seconds.
- 46,120
If you are referring to the command watch, it basically run a command every so often, by default every 2 seconds, and shows the output fullscreen.
For a fairly lengthy example, open terminal, and enter:
sleep 5; echo "hello world" >> ~/newfile.txt
This will wait 5 seconds, and then output "hello world" to the text file ~/newfile.txt
Don't execute it, but open a new - Ctrl+Shift+T.
Enter this, and run it:
watch -n 2.5 ls ~
This will tell watch to run the command ls ~ (which lists the contents of the home directory ~), every 2.5 seconds -n 2.5.
Go back to the tab with the sleep.., and run that command, then switch back to the tab with the watch command in. You should see the newfile.txt ventually appear in the output.
For more info, view the manual page for watch with:
man watch