≡ Menu

How To Execute Ping Command Only For N number of Packets

Question: When I execute ping command from the command line, it keeps sending the packets until I hit CTRL-C to terminate the ping command output. How can I execute ping command only for N number of packets and terminate the output automatically?

Answer: Use ping option -c to specify the number of packets. After sending N number of packets, ping command will terminate automatically as explained below.

Ping Command – Interactive Mode

In the following example, you have to press CTRL-C to terminate the ping command output.

$ ping 0
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.006 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.004 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.005 ms
....

Note: Press CTRL-C to terminate.

--- 0 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.004/0.008/0.023/0.007 ms
  • Note: ping 0 — pings the local host.

Ping Command – Non Interactive Mode (Specify number of packets to be sent)

In the following example, ping command will send only 2 packets and you don’t need to press CTRL-C to terminate the output.

$ ping 0 -c 2
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.006 ms

--- 0 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.006/0.015/0.024/0.009 ms

Ping Command Option -c Usages

Redirect the ping command output to a file

$ ping 127.0.0.1 -c 2 > ping-output.txt

$ cat ping-output.txt 
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.005 ms

--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.005/0.010/0.015/0.005 ms

Redirect to another process. This example shows only the ping output summary (last two lines).

$ ping 127.0.0.1 -c 10 | tail -2
10 packets transmitted, 10 received, 0% packet loss, time 8999ms
rtt min/avg/max/mdev = 0.005/0.006/0.014/0.003 ms
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • SlyCooperFan1 August 24, 2011, 4:51 pm

    This doesn’t work in Windows 7. The command is not -c, but -n. Format:

    ping WIP -n #

    Where [WIP] is the website or IP address to be pinged, and # is the number of packets to send.