Skip to content

Instantly share code, notes, and snippets.

@ygoex
Forked from donbrae/Bash cheat sheet.md
Last active July 10, 2019 11:13
Show Gist options
  • Select an option

  • Save ygoex/00905422a228ac52d012aba646a1c092 to your computer and use it in GitHub Desktop.

Select an option

Save ygoex/00905422a228ac52d012aba646a1c092 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 to root directory)

cd ~ (go to home directory)

cd .. (go up a directory)

cd - (go to previous 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 -la (as above, but including hidden files, e.g. .htaccess)

ls -laZ (as above, but including SELinux context and permissions). If we see a ? after typing ls -laZ, that means that SELinux is disabled.

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

Permissions (chmod)

chmod (change mode: permissions).

Usage: chmod ugo+rwx foo to grant read, write and execute permissions for user, group and other for directory foo

We can also come across other values:

  • chmod +x is equal to chmod ugo+x (Based on umask value). Grants execute permisions for user group and other. When we don't specify which one of the owner, group or others is our target, in case of x it will considers all of them.
  • chmod 755 is equal to chmod u=rwx,go=rx

A little bit of explanation:

  1. + means add this permission to the other permissions that the file already has.
  2. = means ignore all permissions, set them exactly as I provide.
  3. - means remove this permission to the other permissions that the file already has.

The binary logic behind octal representation:

        Symbolic:  r-- -w- --x  |  421
        Binary:    100 010 001  |  -------
        Decimal:    4   2   1   |  000 = 0
                                |  001 = 1
        Symbolic:  rwx r-x r-x  |  010 = 2
        Binary:    111 101 101  |  011 = 3
        Decimal:    7   5   5   |  100 = 4
                   /   /   /    |  101 = 5
        Owner  ---/   /   /     |  110 = 6
        Group  ------/   /      |  111 = 7
        Others ---------/       |  Binary to Octal chart

Using 755 you are specifying:

  • 7 --> u=rwx (4+2+1 for owner)
  • 5 --> g=rx (4+1 for group)
  • 5 --> o=rx (4+1 for others)

So chmod 755 is like: chmod u=rwx,g=rx,o=rx or chmod u=rwx,go=rx.

More about octal representation can be read here

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 -rwl "ip_address_regex" .(look for a string within all files in the current directory, and print the files that match the search)

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

Ampersand Operator: Parallel commands

You can also use the & operator to run two commands at the same time on Unix

npm run script1.js & npm run script2.js

You can also use Parallelshell to run commands in parallel - cross platform.

The Logical AND Operator (&&)

A && B –– Run B only if A succeeded

Chaining commands (Waits for each command to finish successfully before starting the next) is possible with the && operator. This is very similar to pre- or post- hooks.

The AND Operator would execute the second command only, if the execution of first command succeeds.

The Logical OR Operator (||)

A || B –– Run B only if A failed

Execute a second command only if the first command does not succeed.

The Semicolon Operator (;)

A ; B –– Run A and then B, regardless of the success or failure of A

The semicolon operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds.

Pipe Operator (|)

This | operator is very useful where the output of first command acts as an input to the second command, 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.

Output Operator

> Output to file

grep 'John Doe' bigFileWithNames.txt > linesWithJohnDoe.txt

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 -i foo.txt (same as previous, but requires confirmation before deleting)

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 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

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)

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)
  • history (shows the terminal history)
  • history -c (clears the terminal history)
  • history > /Users/my-user/Desktop/latest-history.txt (outputs the terminal history into a text file)
  • df -h (get disk usage)
  • du -h -s folder_name (get size of folder)

Scripts random

JPG quality reduction

Reduces image quality of JPGs in current directory by 85% in order to reduce file size. Requires imagemagick (brew install imagemagick).

#!/bin/bash

for i in *.jpg; do
convert "$i" -quality 85% "${i%.jpg}-new.jpg"
done

webp conversion

Converts JPGs in current directory to webp format. Requires imagemagick (brew install imagemagick).

#!/bin/bash

for F in ${PWD}/*.jpg ; do cwebp $F -o ${PWD}/`basename ${F%.jpg} `.webp ; done

Shortcuts to clear lines in terminal

Ctrl+U - clear all the current line from the end to the beginning only if the cursor is at the end of the line.

Ctrl+K - clear all the content from the beginning or middle of the line until the end.

Ctrl+W - clear the previous word in the current line. For example if you have typed a command like git diff /path/to/some/file and you want to delete just the last parameter to the command, Ctrl+W is very useful.

Ctrl+A + Ctrl+K - move the cursor at the beginning of the line and clear all the current line from the beginning to the end.

Ctrl+E + Ctrl+U - move the cursor at the end of the line and clear all the current line from the end to the beginning.

The commands Alt+b and Alt+f move back and forth by one word at a time.

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