Subscribe to Windows IT Pro
June 28, 2001 12:00 AM

Shell Scripting 101, Lesson 7

Windows IT Pro
InstantDoc ID #21630
Rating: (0)
Downloads
21630.zip

You can easily join commands with command symbols. You can use four symbols: pipe (|), ampersand (&), double ampersand (&&), and double pipe (||).

The | Symbol
The | symbol pipes the output from one command (Command A) to another command (Command B). You place the | symbol between the commands:

Command A | Command B

You often have to filter a command’s output before using it. Thus, you’ll probably use the | symbol often to pipe output to the Find command. The Find command searches a command’s output (or text from an input file) for the string you specify. The Find command displays any line that contains the string and discards all the other lines.

For example, suppose you want to know when the C:\winnt directory was created. As I discussed in "Shell Scripting 101, Lesson 2," the Dir command lists information, including creation date and time, about the subdirectories and files in the specified drive or directory. In this case, you use the command

Dir /ad C:\

The /a switch followed by the letter d specifies that you want to list only directories on the C drive.

You now have the information you want, so you just need to pipe it to the Find command to filter out the information you don’t want. As the command

Dir /ad C:\ | Find /i "winnt"

shows, you use the | symbol to pipe the Dir command’s output to the Find command and specify "winnt" as the string for which to search. You need to enclose the string in quotes. The /i switch after Find makes the search case insensitive. As Figure 1 shows, when you run this command, you receive just the line that contains information about the \winnt directory.

Related Content:

ARTICLE TOOLS

Comments
  • Naufal
    8 years ago
    Jan 20, 2004

    When I run



    Dir /ad C:\\ | Find /i "winnt"



    it does NOT just give me the search directory (i.e., /winnt) but also all the contents under it. Is there are way to just get the /winnt directory as the result vs. all the underlying contents also?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.