The Linux terminal gets a lot of use, but I've found learning the Windows command prompt can be useful too. From writing a quick script to executing a command remotely, it helps to know some basic commands. There are many similar commands, and it doesn't take long to become command-line interface ambidextrous (although I still find my fingers occasionally wanting to type 'ls' or 'pwd' on Windows!). This is a chart to help someone that knows one command line, transition into learning another command line. It is not an exhaustive list of commands.
Note in the examples below I use the default configuration of $
for Linux shell and >
for Windows command-prompt.
Help
$ man ls > dir help
$ ls --help > dir /?
These are the most common ways to get help directly from the command line. Sometimes tools don’t follow convention so you might have to try it without any parameters, maybe a short help -h
, or other combinations.
Change Directory
$ cd > cd
$ cd .. > cd ..
$ cd / > cd\
Current directory .
vs parent directory ..
usage is similar.
Output Current Directory
$ pwd > cd
> chdir
Create Directory
$ mkdir > mkdir
$ mkdir -p
On Linux -p
creates parent folders while on windows Command Extensions must be enabled. /e:on
from cmd or setlocal EnableExtensions
from a script.
Delete Directory
$ rm -rf > rm
> rmdir /s
Be careful with these ones! On Linux you might want to use the safer rm -ri
which will prompt you.
File
$ rm > del /s /q
List Directory
$ ls > dir
$ ls -alF > tree
$ ls -R
I would explore the options on Linux for ls
by running man ls
. I generally prefer ls -alF
to show all information and ls -alFrt
to show latest changed files.
Copy
$ cp > copy
$ cp -r > xcopy
> robocopy
robocopy
is a "robust copy" tool and requires a separate download on some versions of Windows, great for copying large data sets.
Move/Rename
$ mv > ren
> rename
> move
Print File
$ cat > type
For large files, you can pipe output to read it easier.
View File
$ less > more
And/Or Logic
$ && > &&
$ || > ||
Piping
$ | > |
Used to send output of one command to another.
Write/Read file
$ > > >
$ >> > >>
$ < > <
>
overwrites while >>
appends to file. To output errors also in Windows you will need to add 2>&1
.
Search for files
$ find > cd\
> dir *file*.* /s /p
dir
acts as find on Windows.
Search in files
$ grep > find
> findstr
findstr
supports regular expressions similar to grep
.
$ echo > echo
echo.
for newline in Windows.
Kill Process
$ kill > taskkill
$ kill -9 > taskkill /IM notepad.exe
Compare files
$ diff > fc
> comp
Edit File
$ pico > edit
$ vi
On Windows it's usually easier to just run <your favorite editor> filename.txt
instead of using the edit command. On Linux you have a variety of choices from VIM to Emacs and many more. I tend to use vi more on Linux since it usually comes included by default, where VIM or Emacs might not be installed.
Replace
$ sed > for
Sadly there isn't a nice built-in tool for Windows. Your stuck with using the for
command or finding a sed for Windows tool.
Clear terminal
$ clear > cls
Wait
$ sleep > sleep
> ping -n <seconds+1> 127.0.0.1 > nul
The second command is a hack to target XP users that don’t have the optional sleep command installed.
OS Version
$ uname -a > ver
$ cat /etc/redhat-release > systeminfo | findstr /C:"OS"
$ cat /etc/lsb-release
Information in /etc
is usually distribution specific.
Environment Variables
$ env > set
$ export var=value > set var=value
$ echo $var > echo %var%
Sorting
$ sort > sort
File Permissions
$ chown > attrib
$ chmod > attrib +h
$ mv filename .filename > cacls
> takeown
These commands are very different, I recommend reading help files.
Machine Name
$ hostname > hostname
> %computername%
hostname
on Linux can also temporarily change the name.
Pinging
$ ping > ping
Memory
$ free > mem
$ top
Services
$ service <serviceName> start/stop > net start/stop <serviceName>
> sc create/delete
Restart OS
$ shutdown -r now > shutdown -r -t 0
IP
$ ifconfig > ipconfig
Print Time & Date
$ date > date /T
> time /T
Generally Windows commands and parameters are case-insensitive while Linux is inherently a case-sensitive environment. For example, you can have two folders names Folder
and folder
on Linux, but Windows will treat these as the same folder. So watch your character case on Linux, but on Windows it's your preference. I like to stay consistent with the script I'm editing or use lowercase for a new script because all uppercase makes me FEEL LIKE I'M YELLING IN MY SCRIPT.
For more complicated Windows scripts, I recommend learning PowerShell which is included Windows 7 and up. You still have to write regular bat scripts in order to work on all versions of Windows out of the box, but not for much longer since Windows XP support ends April 8, 2014. Although, some versions of Windows Server 2003 will still be supported until July 14th, 2015.
This was just a small subset of similar commands. To learn more you can find Windows CLI documentation here for XP and here for 7 and Server versions. I highly recommend the DosTips website for creating more in-depth scripts.
Comments
comments powered by Disqus