Windows Command Line Essentials

If you type help from your windows command shell session, you will get a list of all available shell commands.

1. CMD - List all files and sub-directories in a directory

:: List contents of the current directory
dir

:: List contents of the specified directory
dir c:\Users\

2. CMD - Create a new directory

:: Basic usage
mkdir <new-directory-name>

:: Alternatively, use md
md new-directory

:: Create a new directory at a specified path
mkdir c:\\temp\test2 

:: View built in help page
mkdir /?

3. CMD - Create or edit a file

notepad somenewfile.txt
  • Notepad will create a new file if it does not exist or open the existing file for editing.

4. CMD - Delete a file

del <file-path-or-name>

:: View built in help page
del/?

5. CMD - Remove a directory

rmdir <path-to-directory>

:: See built-in help page for more options
rmdir /?

6. CMD - View file contents

:: Print contents of a file to console output
type <filename>

:: View built-in help page
type /? 

7. CMD - Set or display environment variables

:: Example: Add python to Windows path
SET path=%path%;c:\python27

:: View all environment variables
SET

:: View a specific environment variable e.g. PYTHONPATH
SET PYTHONPATH

:: View build in help page
SET /?

8. Copy, Move or Rename Files

:: Copy file to a new directory 
copy <source-file-path> <destination-file-path>

:: View built-in usage guide
copy /?   


:: Copy all files and sub-directories to  a new location
> xcopy <source-directory> <destination-directory>

:: View built-in help page
> xcopy /?   


:: Move file to a new destination
> MOVE <source-path> <destination-path>

# View help page
> MOVE /?  

Note that you can use the move command to rename a file. For example move hello1.py hello.py will essentially rename hello1.py into hello.py

9. Searching text

  • Use findstr to search text using regular expressions or literal string comparisons. Example:
:: Search for the word 'hello' inside a file named hello.py
type hello.py | findstr /R "hello"


:: Search for all text files in the current directory
dir | findstr /R ".*.txt"


:: See built-in help page for more information
findstr /?

10. Command Aliasing

  • If you are already familiar with the linux bash shell, you might be used to using ls to list the contents of a directory.
  • You can create an alias on the windows command shell to achieve the same purpose. Exampple:
doskey ls=dir

:: ls will now display the contents of the current directory
ls

Note that the alias will only be usable from the current shell session.



References


Written by@NdamuleloNemakh
Welcome to my coding diary, I hope you find it useful!

GitHubTwitter