Command Line Fun of the month

February 5, 2010, by | Start Discussion

“The real geek loves command line”. We have heard this line a lot, it’s the pickup line of geeks. So we are dedicating one section of the magazine for command line fun. Here you’ll be enlightened (if not already) by the simple gyan of command line to fulfill your shell dream. Remember we require these stuff many a times in our scripts to make them effective. So here we are with “Command line fun of the month”

As a bonus for the inaugural issue, we present you with two command line funs

1. Watching the File Count in a Directory

Many a times we feel the need of monitoring the increasing (or sometime decreasing) number of flies in a particular directory. In this issue we’ll learn how we can watch the count of files in a particular directory continuously

1. For Windows


C:> for /L %i in (1,0,2) do @dir /b | find /c /v "" & ping -n 6 127.0.0.1>nul

This command includes some useful constructs as well:

* The for /L loop, which counts from one to two in steps of zero keeps it running infinitely.
* The /b option makes the dir command drop the garbage from its output (., .., volume name, size, etc.)
* The find /c /v "" means to find, and count (/c) the number of lines that do not have (/v) nothing ("") because even a blank line has a CRLF, so it gets counted.
* And, we ping ourselves 6 times to introduce a 5-second delay. First ping happens instantly, the remaining happen once per second. Vista does have the "timeout /t -[N]" command, but ping is chosen because we wanted it to run on all windows box.
* Remember this will work for the present working directory

Actually dir command, when used like this, doesn't show files that are marked with the system or read-only attributes. To solve that replace the dir /b command with the dir /b /a command, this will show all files regardless of these attributes.

Thus, the resulting command:

C:> for /L %i in (1,0,2) do @dir /b /a | find /c /v "" & ping -n 6 127.0.0.1>nul

2. For Linux

Everyone would agree that command line activities in Linux are very easy & fun

$ while :; do ls | wc -l; sleep 5; done

Aren’t they 😉

There is another option in Linux using watch command

$ watch -n 5 'ls | wc -l'

2. Command-Line Ping Sweeper

You can find a lot of ping sweep utilities on internet but the fun & strength lies in relying on what comes as default in any system. Remember someday you might land up on a system where you might not have your favorite set of tools.

1. For Windows

Here's a Windows command to do ping sweeps at the command line:

C:> FOR /L %i in (1,1,255) do @ping -n 1 192.168.1.%i | find "Reply"

In this example FOR /L loop is a counter. The iterating variable is %i. It starts at 1, goes up by 1 upto 255 in each iteration of the loop. This example is for a /24-sized subnet. In the loop each IP is pinged only once (-n 1) with echo off (@) so that the command ping is not shown in output. The output of ping is then parsed using the find command looking for "Reply". The find command is case sensitive, so put in the cap-R in "Reply". Or, you could use /i to make the find case insensitive.

2. For Linux

The ping command in Linux is bit more powerful. Here you have the strength of a broadcast ping which can make the task very easy

# ping -b -c 3 255.255.255.255

This can do a broadcast ping & the responses can be utilized but unfortunately modern Windows machines don't respond to broadcast pings.

So to take the same approach as in the case of Windows, we can make it look as the following

$ for i in `seq 1 255`; do ping -c 1 192.168.1.$i | awk '/1 received/ {print $2}'; done

Here same as Windows option a variable i goes in loop of 255 & sends a ping request to the IP. The result of the ping is then parsed in awk.

There is one problem in the output of ping in Linux. It generally produces multiline output and we need both beginning as well as ending part of the output to see unique IP as well as positive response of ping. To solve that we can add tr \n ' ' to make the output a single line and make it easier for awk to understand.

The final command will now look like

$ for i in `seq 1 255`; do ping -c 1 192.168.1.$i | tr \n ' ' | awk '/1 received/ {print $2}'; done

All is done J

Author bio not avialable

Leave a Reply