Listing Files with the Dir Command
The dir command is one of the most helpful command-line programs. Although it's often more efficient to use Explorer to view folder contents, dir has a few tricks up its sleeve that make it worth knowing about.
Without any command-line arguments, dir prints a listing of files and directories (folders) in the current directory. Here's a sample listing:
Volume in drive C has no label. Volume Serial Number is 7AC1-7AC5 Directory of C:\Users\bknittel\Documents 05/27/2010 06:27 PM <DIR> . 05/27/2010 06:27 PM <DIR> .. 04/30/2009 06:23 PM <DIR> Fax 05/21/2009 04:15 PM <DIR> Remote Assistance Logs 05/26/2009 06:28 PM <DIR> Book Files 04/30/2009 06:23 PM <DIR> Scanned Documents 05/27/2009 05:02 PM 28,672 book outline.xls 1 File(s) 28,672 bytes 6 Dir(s) 1,132,052,480 bytes free
(If you've only ever used Macintosh- or Windows-based computers, you might find it hard to believe that not too many years ago, this was the only way to view and locate files.)
So what are . and ..? These two odd entries represent the current directory and its parent directory.They serve no real purpose today; they're the software equivalent to your appendix, a remnant of MS-DOS's distant Unix ancestry, and you can ignore them.
The rest of the listing shows the names of the directory's files and subdirectories as well as their sizes and last-modified dates.The listing ends with a summary of file counts and sizes.
In Reference List below, I showed all the command-line options you can use with dir. In this section, I go over a few of the ones that are particularly useful.
In this tutorial:
- The CMD Command-Line
- CMD Versus COMMAND
- Running CMD
- Opening a Command Prompt Window with Administrator Privileges
- CMD Options
- Disabling Command Extensions
- Command-Line Processing
- Console Program Input and Output
- Using the Console Window
- I/O Redirection and Pipes
- Copy and Paste in Command Prompt Windows
- Command Editing and the History List
- Name Completion
- Enabling Directory Name Completion
- Multiple Commands on One Line
- Grouping Commands with Parentheses
- Arguments, Commas, and Quotes
- Escaping Special Characters
- Configuring the CMD Program
- The Search Path
- Changing the Path
- Predefined and Virtual Environment Variables
- Setting Default Environment Variables
- Built-in Commands
- Extended Commands
- Listing Files with the Dir Command
- Paginating Long Listings
- Printing Directory Listings
- Sorting Listings
- Locating Alternate File Streams
- Setting Variables with the Set Command
- Conditional Processing with the if Command
- Scanning for Files with the for Command
- Using the for Command's Variable
- Processing Directories
- Numerical for Loop
- Getting More Information