Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
1  
welcome to AU! ..:: AskUbuntu Tour ::.. –  precise Jan 19 at 6:33

2 Answers 2

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.

share|improve this answer
    
Even you can use watch -d ls /path/to/directory. It will update the list in every 2 seconds by default. –  souravc Jan 18 at 18:14

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 -nls ~

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.