Command Line Cheat Sheet (For Cygwin in Windows)
Cheat sheets are the best thing that ever happened to my test-taking abilities, and now they can help you use command line too! This cheat sheet relates specifically to using Cygwin in Windows - things are slightly different (read: easier) for Mac and Linux users, although they are basically the same. Also note that Cygwin is different from cmd.exe in Windows - cmd.exe is never used!
Why should you even bother to use this cheat sheet and Cygwin? The command line is a very specific, powerful tool that can make tasks faster than using a GUI, because you simply type in the commands that you want. It is especially useful in running Mercurial and in navigating/editing files.
Basic Syntax
Cygwin syntax has the following structure:
COMMAND + OPTIONS/ARGUMENTS
The command runs a specific program, and the options/arguments give more information about what the program should do.
Frequently Used Commands
- To exit a program, try Esc or Q.
- cd :- change directory.
- This takes you to the home directory.
- cd followed by a file name (or series of file names) takes you to that directory. Notice that you need to give the path to the file you want starting from where you currently are located. So if you want to go to the desktop and are currently in the c/ drive, you need to enter Users/Name/Desktop to get to that directory - you cannot just enter Desktop.
- Enter cd and then the first letter of the file/folder you're looking for, then press Tab twice to get the autocomplete choices. You can then type the next letter to specify the exact file/folder you want and then press Tab again to complete the name of the file/folder.
- cd .. goes up one directory.
- cd ../.. goes up two directories.
- cd / goes to the very top directory.
- ls :- list files.
- This lists all the files/folders in the folder you're in.
- ls -la :- list all. This shows even hidden files/folders.
- ls * uses the wild card variable * to search for specific files. For example, you can use the wild card to look for all the files with the same file extensions, i.e. ls *.jpg searches for all the .jpg files in your current directory location.
- pwd :- prints working directory.
- This tells you where you are.
- mkdir :- make directory.
- Use this to make new folders - just type mkdir and then the name of the folder you want.
- Note that the folder is made in the current directory!
- touch :- makes new document.
- rm :- removes a file.
- This command is dangerous, because no Recycle Bin is used - once a file/folder is removed, you can never recover it!
- If a file won't be deleted this way, enter: rm -rf and then the file name. This is a recursive function that removes every file in a folder until it is all deleted. Also very dangerous to use!
- mv :- move.
- You can use this command to move a file to another directory.
- mv [filename] ../ moves the document to the parent directory (one up).
- mv [filename] ./ moves the document down to the current directory. Note that if you use this argument, you need to enter the full address of the file - i.e. c/Users/Name/Desktop/text.txt, not just text.txt.
- If you enter mv [filename] [newfilename] you can rename a file without moving it. This does NOT copy the file!
- cp :- copy.
- cat :- concatenate.
- This can be used to display all the text in a document you specify. This is only good for short documents - if you want to see the contents of a larger file, use "less".
- less :- a program that displays everything in a file.
- Use this for longer documents.
- To exit, use Q.
- grep :- a search function.
- diff :- difference.
- This function finds the difference between two files or folders and lists all of them.
- The syntax is: diff [file1] [file2]
- vim :- a text-editing program. Difficult to use, but lots of tutorials available online! (Use Q to exit.)
- open :- opens a file, but only on Mac/Linux. :(
- ssh :- This program allows you to access another computer server that you have the credentials to access.
- This is a very powerful tool.
- To specify a username, type in ssh [username]@[servername]
- To logout, enter logout or Ctrl + D.
- Ctrl + A :- go to beginning of line
- Ctrl + E :- go to end of line
- Ctrl + U :- deletes line
- Ctrl + L :- clears entire screen
- Ctrl + R :- autocompletes based on commands you've already entered.