How to do some powerful dos commands part1 Posted By Adam Reilly on July 22, 2009

Despite it’s simple appearance, the humble Command shell can be an extremely powerful tool for automating repetitive or difficult system tasks.  Many people are scared away by the lack of GUI elements, but this can be a tremendous asset in terms of making processes consistent and repeatable.

The first command we’ll look at may be familiar, most people have seen, heard of or learned the dir command at some point.  When run without any arguments, it prints a list of files in the current directory along with some file-system metadata.  You may not be aware that dir can be run with several flags and parameters that can modify it’s behavior.  For instance, typing dir *.txt will filter the list of files according a pattern, in this case it will only list files with a txt extension.

Dir can also be used to search beyond the current directory.  Typing “dir /s” will cause it to find all files in the current directory, and the begin searching folders, subfolders and subsubfolders, until it’s searched the entire tree.  Multiple flags and arguments can be passed into the command at one time to combine functionality, such as typing dir /s *.txt causes all text files in all sub folders to be printed to the screen.

To see a full list of dir (and many other dos commands) capabilities, run it with the “/?” option.

A pair of commands that people are less familiar with are the type and sort commands.  Type is useful for displaying the contents of text files without opening an editor.  The file that is displayed is clearly not in order, using the sort command can help.

While the output certainly appears to have been sorted, there was a little too much output to fit into one terminal window.  In order to get around this, I’m going to introduce a special operator in DOS called the pipe.  It’s the vertical bar on the keyboard [ | ].  It literally tells the DOS prompt to take any output from one command and pass it into another.  We’ll need the help of another command called more, whose only job is to receive output and display it one screen at a time.  Using this combination, we can see that the data is correctly sorted.

This may seem like a lot of work to sort a list of numbers, but it’s a simple example that illustrates one of the most powerful features of working with a command prompt.  Taking several simple programs and chaining them together with pipes gives the user a great deal of flexibility in getting things done with a small set of tools.

Let’s consider another simple example.  Assume we want to find names, sizes and modified dates for all text files within a nested directory structure.  Oh, and we want that sorted by the modified date.

First, we’ll type the dir command with the /s flag to search all sub-directories.  Then, we’ll pipe that output to the sort command and finally, we’ll use a new command called find, to exclude any non-file entries.

As you can imagine, this is much faster than trying to search by hand, and has the advantage of windows search in that the output can be captured.  Just knowing a few commands and tricks to tie them together can make your life a lot easier.

Comments

Post A Comment

Categories

Jul 2010

S M T W T F S
       1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Sign me up for Logik news!