Command line basics
The Command Line
The command line is a more intimate way of interacting with your computer (than say, Finder on Mac or Explorer on Windows). You might also hear folks referring to the command line as the shell or terminal .
I’ve put together a short introduction as well compiled a ton of common shell commands! I won’t repeat them all here now, but remember pwd
, ls
, cd [foldername]
, cd ..
are really helpful when you are moving around your computer using the command line. A few other things to keep in mind:
- You can add flags to commands to instruct shell to do specific things or structure its output in a specific way. A few helpful flags to add to the
ls
command:-l
: displays more information about your files and folders (you’d enter it asls -l
in your shell)-h
: makes the displayed filesize numbers human readable!-a
: all, shows you hidden files, usually important stuff your computer doesn’t want you to deal with.- You can use a bunch of flags together
ls -lh
orls -lha
- A few helpful
cd
commands:cd ..
moves you “up” one directory- When you see ~ in terminal, it means you’re in your home directory.
cd ~
: takes you directly to your home directorycd ~/Downloads
: takes you directly to your Downloads foldercd [drag the folder from Finder into Terminal]
: it’ll autocomplete the path to that folder! This only works on Macs.
- If your command line is running something it shouldn’t be, you can usually use
Ctrl+C
to escape/exit. clear
will do exactly as it sounds; your shell will be cleared of all commands and output.- File and folder names have to be typed exactly as is!
cd desktop
andcd Desktop
are different (you probably want to use the latter). - Besides using
cat
to see the entire file, you can also take a peak at the beginning or end of a file.head -n 20 [filename]
: see the first 20 lines; change the number to see more or less lines.tail
: use tail to see the end of the file; structure the command the way that thehead
command is structured directly above.
Using tab autocomplete
You should be using tab every single time you type a file. Programmers are lazy! We hate typing!
If pressing tab does not autocomplete your folder or filename, then you may need to enter a few more letters to disambiguate which folder or file you mean.
Also, make sure that autocomplete gave you the ENTIRE folder or file name. Sometimes, you will have to enter the next letter hit TAB again, especially if there are spaces in your filename.
Side note: DON’T PUT SPACES OR APOSTROPHES IN YOUR FILE NAMES.
Other fun commands
grep
: allows you to search for a specific set of characters inside of a filegrep [search term] [filename]
grep "[search term]" [filename]
grep -r "[search term]" [directory or folder name]
: this searches all the files in the specified directory for your search term- Note that the search terms are case-sensitive
man [command]
: see the manual entry for that command. Ex:man wc
- use
q
to escape from the manual
- use
wc -l [filename]
: get the line count for the given file
Piping a way of connecting different commands together on the terminal.
grep " n." [filename] | wc -l
: displays the line count of the output ofgrep " n." [filename]
(rather than the whole file)- More on piping.