Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A bash cheat sheet, created with Terminal.app noobs in mind.

Changing directories (cd)

cd /var/www/my_website (go to /var/www/my_website directory)

cd / (go to root directory)

cd .. (go up a directory)

Also, pwd will tell you the present working directory.

Listing (ls)

ls (list current directory)

ls -l (list current directory with more details, e.g. last updated date, permissions, size)

ls -al (as above, but including hidden files, e.g. .htaccess)

ls -lt (list files by last modified date, newest first; ls -ltr for the reverse)

Permissions (chmod)

chmod (change mode, e.g. permissions), e.g. chmod ugo+rwx foo to grant read, write and execute permissions for user, group and other for directory foo

E.g. chmod o-w foo (remove write permissions for other)

You can also use the octal representation, e.g. chmod 777 foo is the equivalent of chmod ugo+rwx foo

Searching (grep, find, sed)

grep foo my_document.txt (look for occurences of the word foo in the file my_document.txt)

grep string directory (look for a string within all files in a directory)

grep -r -i string directory (as above but does the search recursively and case insensitively)

find . -name foo.php (look for file with name foo.php in the current directory and sub-directories)

find . -maxdepth 1 -name foo.php (as above, but limit the search to the current directory)

find . -maxdepth 1 -name "foo.*" (example of using a wildcard; here look for files named foo with any file extension)

Chaining

Use the pipe symbol to chain together and refine searches, e.g.:

grep foo error_log | grep bar

This looks for the string foo within error_log, then looks for the string bar within the results of the first search.

Search and replace

sed -i -e "s/jello/hello/" greeting.txt (look for occurrences of the string "jello" in greeting.txt and replace them with "hello". i = amend the file in place)

find . -name "*.txt" -exec sed -i '' "s/jello/hello/" {} \; (look for all .txt files and replace occurrences of "jello" with "hello" [this is the BSD implementation of sed, which, for example, runs on macOS])

Deleting

find . -name "foo" -delete (find all files with the filename foo and delete them)

find . -empty -type d -delete (find all emptry directories and delete them)

Viewing files (more, less)

more foo.txt (view contents of foo.txt)

less access.log (useful for looking up large log files. Allows you to view the content of a file in reverse order. Type > to go to the end of the file, then b to go up a page and Space to go down a page. Q to quit.)

The results of actions can also be piped into more for paginated viewing. E.g. grep foo error_log | more will let you page through results of what you've just grepped for. Use Space to go forward a page, or Return to go forward a line.

Editing and creating files and directories

sudo nano foo.txt (use built in text editor "nano" to edit file foo.txt)

If you run the above command and foo.txt doesn't exist, nano will create it for you (but you need to do a Ctrl + O within nano to write the file).

You can also write results, e.g. of a search, out to a file, e.g.:

grep foo error_log > /home/username/search_result.log

mkdir foo (create directory foo)

Deleting (rm)

rm foo.txt (delete file foo.txt)

rm -r bar (delete directory bar)

Copying and moving (cp, mv, scp)

cp source_file destination_file (make a copy of source_file and name it destination_file)

cp -r source_folder /destination/directory (copy source_folder to /destination/directory. The path of the copied directory will be /destination/directory/source_folder)

mv source_file destination_file (move a file or directory)

mv source_file .. (move source_file up a directory)

Use scp to copy files between two machines. E.g.:

From this machine to another:

scp /path/to/local/file username@hostname:/path/to/remote/file

From another machine to this one:

scp username@hostname:/path/to/remote/file /path/to/local/file

Connecting via SSH to server

ssh username@123.45.67.89 (connect to server; alternatively use domain name in place of IP address)

exit (close connection)

Concatenating

You'll need to create new files (see Editing and creating files and directories above) and run the below as scripts. For example, to run a script named concatenate.sh, do a sh concatenate.sh.

Concatenates all files in directory foo to output.txt:

#!/bin/bash
shopt -s extglob
cat ./foo/* > output.txt
shopt -u extglob

Concatenates all files except file_2.txt in directory foo to output.txt, making sure file_1.txt goes at the top of the outputted file:

#!/bin/bash
shopt -s extglob
cat ./foo/file_1.txt ./foo/!(file_1.txt|file_2.txt) > output.txt
shopt -u extglob

Miscellaneous

  • passwd (change your password)
  • df -h (get disk usage)
  • du -h folder_name (get size of folder)

Useful keyboard shortcuts

Ctrl + C usually quits the current process, e.g. if you're paging through search results or a large file.

+ K clears the screen.

Ctrl + K deletes characters from the cursor to the end of the line.

Ctrl + A moves cursor to the beginning of the current line; Ctrl + E moves it to the end.

Ctrl + _ to undo typing.

In Terminal.app, hold down and the pointer will change to a crosshair. You can then click to place the cursor anywhere within the currently editable area. Useful in nano. Can be quicker than using the arrow keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment