Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

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

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

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