2.  File System and Commands  In Windows, files are organized in directories (aka folders ). The directories are organized in a hierar...

Programmer's Survival Guide for Windows CMD Shell, File System & Source-code Editors Part 2

1:39:00 AM Unknown 0 Comments

2.  File System and Commands



DirectoryStructure.png
 In Windows, files are organized in directories (aka folders). The directories are organized in a hierarchical tree structure, starting from the so-called root directory for each of the hard drive (as illustrated). A directory may contain sub-directories and files. A sub-directory may contain sub-sub-directories and files, and so on.


Windows' file system is further organized in drives, identified by a drive letter followed by a colon, e.g., C:, D: and E:. Each drive has its own root directory, such as C:\, D:\ and E:\, where the "\" (back-slash) denote the root directory of each drive.
Windows' file system is NOT case-sensitive, a rose is a Rose, and is a ROSE.

2.1  Drive, Pathname and Filename

To reference a file in Windows' file system, you need to provide the drive letter, the directory name (aka pathname) and the filename. For example, in "C:\Program Files\java\jdk1.7.0_07\bin\javac.exe", the drive is C:, the pathname is "\Program Files\java\jdk1.7.0_07\bin\" and the filename is "javac.exe". The leading "\" (back-slash) denotes the root directory for that drive. The sub-directories are separated by "\" (back-slash).
The pathname can be specified in two ways:
  1. Absolute Pathname: An absolute pathname begins from the root directory of a drive. It starts with X:\ (where X denotes the drive letter and the leading "\" denotes the root), and contains all the sub-directories leading to the file (separated by "\"). For example, "C:\Program Files\java\jdk1.7.0_07\bin\".
  2. Relative Pathname: A relative pathname is relative to the so-called current drive and current working directory. For example, if the current drive and working directory is "C:\Program Files\java\", then the relative path "jdk1.7.0_07\bin\" resolves to "C:\Program Files\java\jdk1.7.0_07\bin\". A relative pathname does NOT begin with a leading "\" (back-slash).

2.2  Current Drive and Current Working Directory

Each CMD session maintains a so-called current drive and current working directory, which is shown in the prompt in the form of "drive:\current-directory>". All relative pathnames/filenames are relative to this current drive and working directory.

2.3  Set Current Drive (x:) Command

To set or change the current drive, enter the drive letter followed by a colon (:), e.g.,
Prompt> d:    // Change the current drive to D. The prompt changes to D:\...
D:\...> c:    // Change the current drive to C. The prompt changes to C:\...
C:\...>

2.4  Change Directory (cd) Command

To change current working directory, under the current drive, use command "cd new-path" (change directory).
It is important to take note that you need to set the current drive first (via "x:" command) before setting the current directory under the current drive.
You can specify new-path in two ways: absolute or relative. An absolute path begins with a "\" or root directory. A relative path is relative to the current working directory and does NOT begin with a leading "\". For example,
Prompt> c:
   // Set current drive to C. The prompt changes to C:\...
C:\....> cd \           
   // Set current directory to the root directory of the current drive
C:\> cd Windows
   // Set current directory to "Windows" relative to current directory of the current drive
C:\Windows> cd system
   // Set current directory to "system" relative to current directory of the current drive
C:\Windows\system> cd \myproject\java
   // Set current directory absolutely to "\myproject\java" of the current drive
C:\myproject\java> cd "\Program Files\java\jdk1.7.0_07\bin"
   // Set current directory absolutely. Enclosed with double quotes if pathname contains blank.
C:\Program Files\java\jdk1.7.0_07\bin> d:
   // Set the current drive to D drive
D:\....> cd \
   // Change directory to the root of the current drive
D:\> cd Java
   // Change directory to the "Java" sub-directory of the current directory
D:\Java>
Take note that:
  1. You need to set the current drive and current directory in two commands: X: and cd.
  2. The current drive and current working directory is displayed in the command prompt before the ">".
You can cd in multiple stages (e.g., one cd for each sub-directory - recommended), or cd in one single stage with the full pathname.
Prompt> c:                                  // C:\...
C:\....> cd \                               // C:\
C:\> cd Program Files                       // C:\Program Files
C:\Program Files> cd java                   // C:\Program Files\java
C:\Program Files\java> cd jdk1.7.0_07       // C:\Program Files\java\jdk1.7.0_07
C:\Program Files\java\jdk1.7.0_07> cd bin   // C:\Program Files\java\jdk1.7.0_07\bin
C:\Program Files\java\jdk1.7.0_07\bin>
 
// Same As
Prompt> c:
C:\....> cd \Program Files\java\jdk1.7.0_07\bin
C:\Program Files\java\jdk1.7.0_07\bin>
You can use ".." (double-dot) to refer to the parent directory and "." (single-dot) to refer to current directory. For example,
C:\Program Files\java\jdk1.7.0_07\bin> cd ..   // Parent directory
C:\Program Files\java\jdk1.7.0_07> cd ..
C:\Program Files\java> cd ..
C:\Program Files>
Setting proper working directory is important. For example, to compile a Java program called "Hello.java" stored in "D:\myproject\java\":
  1. Set the working directory to "D:\myproject\java\", and reference the file relatively with filename only (without the path):
    Prompt> d:
    D:\...> cd \myproject\java
    D:\myproject\java> javac Hello.java   // Filename only, in current directory
  2. You can also refer to a file with its full path in any working directory:
    // Any working directory
    Prompt> javac d:\myproject\java\Hello.java

2.5  Directory (dir) Command

You can list the contents of the current directory via the dir command, for example,
Prompt> dir              // List of contents of the current directory
......
Prompt> dir Hello.java   // Show the file "Hello.java" only
Wildcards * and ?
You can use wildcards for pattern matching. The wildcard * matches zero or more (any) characters; ? matches one (any) character.
Prompt> dir *.java  // List files ending with ".java"
.....
Prompt> dir test*   // List files starting with "test"
.....
You could, of course, view the contents of a directory using "Computer" or "Window Explorer" more conveniently.

2.6  Shortcut Keys in CMD Shell - IMPORTANT

Previous Commands in Command History: You can use the up/down arrow keys to scroll through the previous/next command in the command history.
Auto-Complete with TAB: Type the first few characters of a filen/directory name, and press TAB key to auto-complete the file/directory name. Press TAB key repeatably to cycle through all the matches.
Copy/Paste: You need to enable Copy/Paste by clicking on the CMD icon (top-left corner) ⇒ Properties ⇒ Options ⇒ Edit Options ⇒ Check "QuickEdit Mode". Once enabled, you can right-click to copy the highlighted text, and another right-click to paste on the command-line.
ESC: Clear command-line.
HOME|END: Move the the begin/end of command line.
Ctrl-Arrow-Left|Right: Move one word to the left/right.
Tips and Tweaks for CMD
  1. CMD shell is NOT case-sensitive.
  2. The screen buffer size (controlling the amount of messages retained in the screen) can be configured under "Properties" ⇒ "Layout". You should set to a bigger number (500-2000), so that you can view more old messages.

0 comments: