Skip to content

Instantly share code, notes, and snippets.

@donbrae
Last active June 1, 2022 06:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save donbrae/1853f28b9f5e19ce39c7 to your computer and use it in GitHub Desktop.
Save donbrae/1853f28b9f5e19ce39c7 to your computer and use it in GitHub Desktop.
A bash cheat sheet

Changing directories (cd)

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

cd .. (go up a directory)

cd / (go to root directory)

cd ~ (go to home directory)

Also, pwd will tell you the current (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)

Root user privilages (sudo)

You may need to prefix some of the commands below (e.g. nano, mkdir and chmod) with sudo, which allows you to run commands as a ‘superuser’. You'll just need to type your usual admin password (e.g. your login password if you’re running Terminal on macOS, or the password of the server root user).

Permissions (chmod)

chmod (change mode), 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)

find . -type f -name "*.sh" (look for all hidden shell scripts)

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 runs, for example, on macOS])

Search and delete

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

find . -empty -type d -delete (find all empty 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

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 and optionally rename a file or directory)

mv source_file .. (move source_file up a directory)

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

From current machine to another:

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

From another machine to current one:

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

An a -r option in order to copy a folder:

scp -r username@hostname:/path/to/remote/folder /path/to/local/folder

Connecting via SSH to server

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

ssh -i ~/.ssh/id_rsa -l root 123.45.67.89 (connect to server as root user using private key id_rsa)

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

Accept arguments in a script

E.g.: sh foo.sh bar (run the script foo.sh, passing in the variable bar).

To check for argument bar in foo.sh:

if [ "$1" == "value" ] # If the first argument (bar) is equal to "value"
then
	// 
else
	// 
fi

Miscellaneous

Useful keyboard shortcuts (macOS Terminal app)

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