Command line cheat sheet

Note some of these commands / tricks don't apply to Windows.

Move to another directory

cd directory_name or cd directory_name/blah/de/da (/ after each directory, this is a unix / linux thing (Mac is based on linux))

Move up a directory

.. means 'one directory up' . means 'the directory I'm currently in'

To move up a directory: cd ..

Kill a running process

ctrl + c

Cycle through previous commands

Up Button

Autocomplete commands

Tab Button

You should press it all the time!

If something tab complete's it's 100% correct, if you type it it's 'probably right' -> Tab wins.

Search through previous commands

ctrl + r then type part of the command to search

Run two commands on one line

;

e.g. npm install; npm run dev

Run two commands, but stop if the first one fails

&&

e.g. npm install && npm run dev

if npm install fails, then npm run dev doesn't get run.

Log to console

echo

echo "Print this!"

See which directory you're in

pwd (present working directory)

Multi line statements

Use a single \ to continue a command onto the next line:

bigCommand \
--settings 1 \
--all-one-line 2 \
stuff

is the same as:

big command --settings 1 --all-one-line 2 stuff

Display the contents of a file

cat filename.txt

Search through previous commands

ctrl + r then type part of the command to search

See running processes

ps -ef

each process has a PID (process id)

Kill a process

kill <PID>

# example

kill 123

if you want to force it to be killed

kill -9 123

Overwriting the output of a command to a file

echo "Hello world!" > myFile.txt

This command deletes all previous contents of the file and replaces it with the output of the command.

Appending the output of a command to a file

echo "Hello world!" >> myFile.txt

This command adds the output of the command to the end of the file.

Piping

|

Piping takes the output of the command on the left, and passes it as a parameter to the command on the right.

ps -ef | grep node

The example above runs ps -ef and then runs grep node on the output.

This will produce ps -ef filtered to only the lines which contain 'node'