Repeat command automatically in Linux

前端 未结 13 1630
滥情空心
滥情空心 2020-12-04 04:54

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

Say, I have an import running, and I am doing

ls -l


        
相关标签:
13条回答
  • 2020-12-04 05:20

    "watch" does not allow fractions of a second in Busybox, while "sleep" does. If that matters to you, try this:

    while true; do ls -l; sleep .5; done
    
    0 讨论(0)
  • 2020-12-04 05:20

    sleep already returns 0. As such, I'm using:

    while sleep 3 ; do ls -l ; done
    

    This is a tiny bit shorter than mikhail's solution. A minor drawback is that it sleeps before running the target command for the first time.

    0 讨论(0)
  • 2020-12-04 05:22
    while true; do
        sleep 5
        ls -l
    done
    
    0 讨论(0)
  • 2020-12-04 05:24

    Watch every 5 seconds ...

    watch -n 5 ls -l

    If you wish to have visual confirmation of changes, append --differences prior to the ls command.

    According to the OSX man page, there's also

    The --cumulative option makes highlighting "sticky", presenting a running display of all positions that have ever changed. The -t or --no-title option turns off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.

    Linux/Unix man page can be found here

    0 讨论(0)
  • 2020-12-04 05:24

    You can run the following and filter the size only. If your file was called somefilename you can do the following

    while :; do ls -lh | awk '/some*/{print $5}'; sleep 5; done

    One of the many ideas.

    0 讨论(0)
  • 2020-12-04 05:27

    A concise solution, which is particularly useful if you want to run the command repeatedly until it fails, and lets you see all output.

    while ls -l; do
        sleep 5
    done
    
    0 讨论(0)
提交回复
热议问题