In Command: The Windows Command Prompt
Written by MrBlueSky on Tue Oct 16, 2007 3:45 pm in blog A Programmers Thoughts under In Command -
104 views
In Command: The Windows Command Prompt
A lot of (recurring) administrative tasks can be handled by the right use of the commandline interface. Although this is common knowledge among Linux users, most Windows users seem to ignore the command prompt. This is too bad, because using the commandline at the right time, for the right job, can save you a lot of work and time. Not despite its lack of a nice graphical user interface, but because you are not hindered by a bloated, shiny interface.
But to maximize the profit of the commandline, it pays off to take a little time to get prepared first.
The Command Prompt
The commandline interface in Windows is called the Command Prompt. It is located in the Start menu, under Accessoires. Lets first create a shortcut to the Command Prompt. Right click on the Command Prompt-menu item and choose Send to->Desktop. Now, go to the desktop, right click on the newly created shortcut and choose Properties. Now you can change some properties of the commandline interface.
You can change the font (in the Font tab), the cursor size (Options tab), some colors (Colors tab) and the window size (Layout tab) among other things. I suggest you make these two changes:
- In the Edit tab, enable both the Quickedit Mode and Insert mode. This enables easy copy/pasting between the command prompt and other applications.
- In the Shortcut tab, change the "Start in" setting to "%HOMEDRIVE%\" (don't forget the trailing backslash). This makes the command prompt always start in the root of your main drive.
Equipping Your Toolbox (Windows)
Linux has a large set of handy tools and commands for use on the command line. These enable you perform all kinds of tasks quickly. Unfortunately, the Windows command line interface only comes with some basic tools and commands, like DIR, SORT, TYPE, etc.
Luckily for the Windows-using Power User a lot of these Linux tools have been ported to Windows. Some of these tools are great to have in your toolbox and can greatly enhance the commandline interface.
Lets take file as an example. The file command can tell the type of almost any kind of file. This is great if you encounter a file and you don't know what to do with it, especially when the extension doesn't give you a clue. First we have to install it:
Installing the GNU tools
The tools can be downloaded and installed from http://gnuwin32.sourceforge.net/. For example, to install the file tool, go to the list of available tools at http://gnuwin32.sourceforge.net/packages.html. Scroll down until you see the line containing the 'File' package, and click 'Setup' on the right of it. Download and execute the file. A setup wizard guides you through the setup.
When prompted for the directory to install in, choose a generic name, for example 'tools', or 'gnu'. If you install more tools you can put them all in the same directory. This way you have only one directory to add to your PATH later on. You can choose 'Full installation', or select only the binaries.
After installing file you have to add its directory to your PATH. This way Windows knows where to find the file command, without you having to specify the folder:
- RIGHT-click on "My Computer" on the desktop
- Choose 'Properties'
- Click the 'Advanced' tab
- Click the 'Environment Variables' button below
- In the LOWER box ('System variables'), select 'Path' (case doesn't matter) and click the Edit-button
- Add the location in which you installed the file-tool to the path. For example: if you installed it into the folder C:\tools, and the current value of path is "%SystemRoot%\system32;%SystemRoot%", change it to "%SystemRoot%\system32;%SystemRoot%;c:\tools\bin".
When you installed the tool to \tools you have to add \tools\bin to the path. Likewise, if you installed the tool to \gnu you have to add \gnu\bin to the path.
If you want to install additional tools later on, you can install them into the same directory (ie. c:\tools or c:\gnu). You don't have to change the path-variable again when you install additional tools, as long as you install them to the same directory.
Open a new Command Prompt and type 'file'. If everything is oke, the file command is found and a short help message is displayed, starting with "Usage:". Lets see if it works. Go to a random directory on your harddisk, use "dir" to see which files it contains, and choose a random file. Now type "file <filename>" and press enter. You will see something like this:
| Code: |
|
C:\WINDOWS>file Usage: file [-bciknNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file... file -C -m magicfiles C:\WINDOWS>file explorer.scf explorer.scf; ASCII text, with CRLF line terminators C:\WINDOWS>file clock.avi clock.avi; RIFF (little-endian) data, AVI, 321 x 321, 1.00 fps, video: RLE 8bpp, audio: (mono, 8000 Hz) C:\WINDOWS>file Zapotec.bmp Zapotec.bmp; PC bitmap data, Windows 3.x format, 96 x 96 x 8 C:\WINDOWS> |
Great. There are a lot of tools at http://gnuwin32.sourceforge.net/packages.html which are usefull to enhance your work at the commandline. I will introduce you to some of them in my In Command blogs.
Some Tips
Here are some tips to maximize your comfort when working with the Command Prompt:
- After you typed the first few characters of a filename, hit the TAB key. The filename will be auto-completed. The first file in the current directory which has the same first characters is used. If this isn't the file you want, press TAB again, and the next file is picked. You can keep on pressing TAB until the right file is choosen.
- Use the up and down keys to scroll through your command history. By pressing up once, you can repeat the last command you executed. Or use F7 to display the command history, select a previous command and execute it again by hitting ENTER.
- When using a filename with spaces in it, put the name between double quotes, or else you will get an error message:
| Quote: |
|
C:>cd "Program Files"\"Mozilla Firefox" Or: C:>cd "Program Files\Mozilla Firefox" |
When you use TAB, the double quotes are automatically added when necessary.
| Code: |
|
C:>dir \windows | more |
The output will be printed page for page, waiting for you to press a key after each page.
| Quote: |
|
C:\WINDOWS>pushd \ C:\>pushd "Program Files\Microsoft Office" C:\Program Files\Microsoft Office>popd C:\>popd C:\WINDOWS> |
1 blog comments below
meaya on Tue Oct 16, 2007 11:11 pm