CLI Cheat Sheet

Directories

▸ pwd Print working directory. Display path of current working directory.

▸ cd <directory> Change directory to <directory>

▸ cd .. Navigate to parent directory

▸ ls List directory contents

▸ ls -la List detailed directory contents, including hidden files

▸ mkdir <directory> Create new directory named <directory>

Output

▸ cat <file> Output the contents of <file>

▸ less <file> Output the contents of <file> using the less command (which supports pagination, etc.)

▸ head <file> Output the first 10 lines of <file>

▸ <cmd> > <file> Direct the output of <cmd> into <file>

▸ <cmd> >> <file> Append the output of <cmd> to <file>

▸ <cmd1> | <cmd2> Direct the output of <cmd1> to <cmd2>

▸ clear Clear the command line window

▸ rm <file> Delete <file>

▸ rm -r <directory> Delete <directory>

▸ rm -iR <directory> Confirm deletion of each item

▸ rm -f <file>Force-delete <file> (add -r to force-delete a directory)

▸ mv <file-old> <file-new>

Rename <file-old> to <file-new>

▸ mv <file> <directory>

Move <file> to <directory> (possibly overwriting an existing file)

▸ cp <file> <directory>

Copy <file> to <directory> (possibly overwriting an existing file)

▸ cp -r <directory1> <directory2>

Copy <directory1> and its contents to <directory2> (possibly overwriting files in an existing directory)

▸ touch <file>

Update file access & modification time (and create <file> if it doesn't exist)

Permissions

▸ chmod 755 <file>

Change permissions of <file> to 755

▸ chmod -R 600 <directory>

Change permissions of <directory> (and its contents) to 600

▸ chown <user>:<group> <file>

Change ownership of <file> to <user> and <group> (add -R to include a directory's contents)

▸ find <dir> -name "<file>"

Find all files named <file> inside <dir> (use wildcards [*] to search for parts of filenames, e.g. "file.*")

▸ find . -name '*.<ext>' -exec sed -i "" 's/<original>/<replacement>/g' {} \;

Finds the string<original>and replaces it with<replacement>in all<ext>files within the directory.

▸ grep "<text>" <file>

Output all occurrences of <text> inside <file> (add -i for case-insensitivity)

▸ grep -rl "<text>" <dir>

Search for all files containing <text> inside <dir>

Network

▸ ping <host>

Ping <host> and display status

▸ whois <domain>

Output whois information for <domain>

▸ curl -0 <url/to/file>

Download <file> (via HTTP[S] or FTP)

▸ ssh <username>@<host>

Establish an SHH connection to <host> with user <username>

▸ scp <file> <user>@<host>:/remote/path

Copy <file> to a remote <host>

Processes

▸ ps ax

Output currently running processes

▸ top

Display live information about currently running processes

▸ kill <pid>

Quit process with ID <pid>

Getting Help

▸ man <command>
▸ <command> --help

Detailed documentation about the <command>

File Permissions

Unix file permissions are set using three digits: the first one representing the owning user, the second one for its group, and the third one for anyone else.

Add up the desired access rights for each digit.

4 –– access/read (r)
2 –– modify/write (w)
1 –– execute (x)

For example, 755 means "rwx" for owner, and "rx" for both group and anyone. 740 represents "rwx" for owner, "r" for group and no rights for other users.

Combining Commands

You can combine a series of commands by separating them with a semicolon ; on the same line.

▸ cp <file1> <file2> ; cp <file1> <file3> ; rm <file1>

Additionally, it is possible to execute a command only if its predecessor produces a certain result. Code placed after the && operator will only be run if the previous command completes successfully, while the opposite || operator only continues if the previous command fails. The following command will create the folder "videos" only if the cd command fails (and the folder therefore doesn't exist):

▸ cd ~/videos || mkdir ~/videos

The "CNTRL" Key

Various keyboard shortcuts can assist you when entering text.

CNTRL+A moves the caret to the beginning of the line

CNTRL+E moves the caret to the end of the line

CNTRL+K deletes all characters after the caret

CNTRL+U deletes all characters in front of the caret

CNTRL+L clears the screen (similar to clear)

CNTRL+C aborts a running command

The "Tab" Key

TAB autocompletes paths and file names.

Instead of typing the path like so:

▸ cd ~/projects/acmedesigns/docs/

... use the TAB key:

▸ cd ~/pr[TAB]/ac[TAB]/d[TAB]

In the case that you have multiple files starting with the same characters, you can hit TAB twice to view all the possible matches and then type a few more characters.

The Arrow ↑ ↓ Keys

↑ keySteps through the last called commands

↓ keyMoves forward in the command history

history prints a list of all recent commands

Home Folder

The home folder is represented by the ~ character:

▸ cd ~
▸ cd ~/Desktop/projects/files/
▸ cd ~/Downloads/

Shortcut for typing cd /Users/your-username/

Output with "Less"

The less command can display and paginate output. Meaning, it only displays one page full of content and then waits for your explicit instructions. You'll know you have less in front of you if the last line of your screen either shows the file's name or just a colon :.

Apart from the arrow keys, hitting SPACE will scroll one page forward, b will scroll one page backward, and q will quit the less program.

Directing Output

The output of a command does not necessarily have to be printed to the command line. Instead, you can decide to direct it to somewhere else.

Using the > operator, for example, output can be directed to a file. The following command will save the running processes to a text file in your home folder:

▸ ps ax > ~/processes.txt

It is also possible to pass output to another command using the | (pipe) operator, which makes it very easy to create complex operations. E.g., this chain of commands will list the current directory's contents, search the list for PDF files and display the results with the less command:

▸ ls | grep ".pdf" | less

Extras

defaults write com.apple.finder AppleShowAllFiles TRUE

Show Mac OS X system files that start with a dot (ex: .htaccess).

Hide the system files by executing the same command, but setTRUE to FALSE. Enter killall Finder to refresh the Finder.

results matching ""

    No results matching ""