Windows Shell Commands You Must Know

Though we live in the GUI world, surrounded by icons and cursors all the way in our computers. There still remains a legacy interface intact and well maintained. As many of you would have probably guessed, we are talking about the CUI or Shell interface, or perhaps “Command Prompt” as you might be knowing it as. Ever wondered why does this exist? Continuation of a legacy? The answer is no. There is more to the shell than we think of and in this post we are going to journey through the intricacies of the shell in a simple way. Brace yourself.

cmd

Why has the shell remained as it was?

When the GUI was introduced it was a revolution. While it made day to day activities easy, there still remained tasks that required the flexibility of a CUI. Think of system admin tools, servers, telnet, ping and all. Tools that we developers use, compilers, debuggers are all command-line based. And it is worth to note that a CUI is less resource intensive that a full-blown GUI. Recovery environment still remains CUI based for the major part. That is, if your Graphics Card ever gets bricked, you’ll get that old faithful shell to carry out your repairs. Or consider the case of renaming something about a 1000 files, then with GUI you’re simply out of luck.

Let’s discover some common commands that every user using windows ought to know.

Starting the Shell

Windows Command Processor can be started by running “cmd.exe” from run dialog or from “Start>Programs>Accessories>Command Prompt”.

Note:

  • Some commands we discuss require administrator privileges. For this you need to run the Command Processor with elevated permissions. For this, right click the shortcut and select “Run as administrator…”
  • It is mandatory to enclose arguments containing spaces with quotes. If you have a quote somewhere in your arguments prefix it with a ‘\’.

Basic Commands

CD (CHDIR)

Changes the current working directory. By default, “Command Prompt” opens in your user profile directory, if you’ve launched it with standard permissions. It starts with the “System32” directory if you’ve launched it with administrator privileges. It is important to be logged into the correct directories to correctly operate the file management commands. CD without parameters prints out the current directory. Under normal conditions CD won’t change directory on a different directory. You must specify the ‘/D’ switch to allow such behaviour.

Syntax

CD [/D] <directory> ↲

Arguments:

  • directory    the directory to change the prompt into.

Switches:

  • /D    switch to directory under a different drive.
CLS

Clears the command prompt screen. Useful if lots of program output have outrun your text buffer.

Syntax

CLS

Example

CLS

Directory Changing

This is not a command but rather, a way to change the prompt to another drive. CDing to a directory under a different drive won’t work without special syntax. For example, you can’t directly change from “C:\Users” to “D:\Flex” with CD.

Syntax

<DRIVE>:

Example

D:

File and Folder Management

TYPE

The type command displays the content of a text document on the screen. Using type for binary files will result in rendering of garbage!

Syntax

TYPE <file>:

Example

TYPE file.txt

Sample Output


——————————–
This is the content of file.txt
——————————–

 

DIR

The DIR command displays a lists of files and subdirectories under a directory.

Syntax

DIR [switches] <directory>

Switches
  • /A    Displays files with specified attributes. Such as H – hidden, S – System, A – archived.\
  • /B    Display only file names and directories
  • /D    Same as wide but files are list sorted by column.
  • /O    Sort file names.
    • N – Sort alphabetically
    • S – By size (ascending)
    • E – Sort by extension
    • D – Sort by date/time (oldest first)
  • /P    Displays file information one page at a time.
  • /S    Displays files in specified directory and all subdirectories.
  • /W    Shows files in wide list format.
  • /X    This displays the short names generated for non-8dot3 file names. This is particularly useful for inputting files into legacy dos applications.
Example

DIR

Sample Output


Volume in drive C has no label.
Volume Serial Number is 78DF-949D
Directory of C:\Users\Amitosh
30-04-2014 15:25 <DIR> .
30-04-2014 15:25 <DIR> ..
26-07-2013 17:22 <DIR> Contacts
03-05-2014 17:13 <DIR> Desktop
07-05-2014 11:29 <DIR> Documents
07-05-2014 09:56 <DIR> Downloads
11-04-2014 10:55 2,148 drawing.svg
07-05-2014 09:57 <DIR> Dropbox
06-04-2014 15:04 <DIR> Links
20-02-2014 21:43 <DIR> Music
05-05-2014 12:45 <DIR> Pictures
25-11-2013 12:27 <DIR> Saved Games
21-09-2013 13:39 <DIR> Searches
07-05-2014 10:07 <DIR> SkyDrive
29-03-2014 13:45 <DIR> Videos
1 File(s) 2,148 bytes
14 Dir(s) 89,215,967,232 bytes free

COPY

Copy command is used to copy files across the file system. Copy is one of the most fundamental file operations under any operating system.

Syntax

COPY [switches] [/A | /B] <source> [/A | /B] + <source> [/A | /B] [+ …] <Destination> [/A | /B]

Arguments:

  • source        The source file(s)
  • /A        Indicates a text file
  • /B        Indicates a binary file
  • Destination    Specifies the directory and/or filename for the new file(s).

Switches

  • /V    Verifies that new files are written correctly
  • /Y    Assumes yes on all queries.
  • /-Y    Causes prompting to confirm you want to overwrite an existing destination file.
  • /L    Copy shortcuts.
Example

This will copy a file from the current directory to a subdirectory.

COPY records.db records

The copy command may be used for concatenating files with the ‘+’ operator

Example

This command will concatenate all the files specified into one

COPY table1.csv + table2.csv + table3.csv merged_tables.csv

MOVE

Move is used to move (cut-and-paste) files and directories across the windows filesystem.

Syntax

MOVE [/Y] <file(s)> <destination>

Arguments:

  • Files        The files or directories to move
  • Destination    Specifies the new location of the file. It can consist of a directory path or a file path if you are moving a single file and want to perform a “move-and-rename”

Switches

  • /Y        Assumes yes on all queries. Here it overwrites all files without prompting.
Example

This will move a file from the current directory to a subdirectory.

MOVE records.db records

RENAME

As the name suggest it renames files and folders. It is also very handy while mass renaming files or changing file extensions.

Syntax

RENAME <oldname> <newname>

Arguments:

  • Oldname    The original file name
  • Destination    Specifies the new location of the file. It can consist of a directory path or a file path if you are moving a single file and want to perform a “move-and-rename”

Switches

  • /Y        Assumes yes on all queries. Here it overwrites all files without prompting.
Example

This will rename records.db to records.

RENAME records.db records

This will rename all files with bin extension to pngs. Note this does not perform a file format conversion.

RENAME *.bin *.png

DEL (ERASE)

This command deletes one or more files and folders permanently.

Syntax

DELETE [switches] <file(s)>

Arguments:

  • Files        The files or directories to delete

Switches

  • /P    Prompts for confirmation before deleting each file.
  • /F    Force deleting of read-only files.
  • /S    Delete specified files from all subdirectories.
  • /Q    Quiet mode, do not ask if ok to delete on global wildcard
Example

This deletes the file “decoded.db”

DELETE decoded.db

This deletes all files with extension “tmp”

DELETE *.tmp

Well, this is only a few of the entire resources available under windows. A lot of hidden “gems” you can find digging the command line. Stay tuned for more awesome tips and tricks.

Leave a Reply

Your email address will not be published. Required fields are marked *