Coder Perfect

In Linux, the command is repeated automatically.


Is it feasible to have a command repeat every n seconds in the Linux command line?

Let’s say I’m working on an import and I’m having trouble.

ls -l

to see if the file size is getting bigger I’d like a command that makes this happen automatically.

Asked by Marty Wallace

Solution #1

Every 5 seconds, keep an eye on it…

ls -l watch -n 5

If you want to see a visual representation of the changes, use the —differences option before running the ls command.

There’s also, according to the OSX man page,

The man page for Linux/Unix can be found here.

Answered by Rawkode

Solution #2

while true; do
    sleep 5
    ls -l

Answered by Oleksandr Kravchuk

Solution #3

In Busybox, “watch” does not accept fractions of a second, but “sleep” does. If you’re concerned about that, consider the following:

while true; do ls -l; sleep .5; done

Answered by mikhail

Solution #4

sleep already returns a value of 0. As a result, I’m employing:

while sleep 3 ; do ls -l ; done

This is a fraction of a second shorter than mikhail’s answer. The fact that it sleeps before performing the target command for the first time is a small negative.

Answered by Sebastian Wagner

Solution #5

If the command includes any special characters, such as pipes or quotes, it must be padded by quotations. To repeat ls -l | grep “txt,” for example, the watch command should be:

‘ls -l | grep “txt”‘ watch -n 5

Answered by jonathanzh

Post is based on