Skip to content

Instantly share code, notes, and snippets.

@ozbe
Last active May 18, 2020 17:31
Show Gist options
  • Save ozbe/abf778d64bf24bd1dbe30365c1366b07 to your computer and use it in GitHub Desktop.
Save ozbe/abf778d64bf24bd1dbe30365c1366b07 to your computer and use it in GitHub Desktop.
Bash Cheat Sheet

bash cheat sheet

Delete all folders, except [NAME]

find . ! -name '[NAME]' -type d -exec rm -rf {} +

Move files, including dot (hidden) files

$ shopt -s dotglob
$ mv [SRC]/* [DESTINATION]/

Source: command line - How can I get mv (or the * wildcard) to move hidden files? - Ask Ubuntu

Check exit code from previous command

$ [SOME_COMMAND]
$ echo $?

Command history to shell script

$ tail -n [NUM_LINES_PLUS_ONE] ~/.zsh_history | head -n [NUM_LINES] > notes.sh

Track process by port and kill said process

# get process id
$ sudo lsof -i :[PORT]
# kill process
$ kill -9 [PID]

Make directory and move into it

$ mkdir [DIRECTORY] && cd $_

Operators

>> Append to file

> output to something other than stdout (replace file)

| feed output into another process

|| or operator

&& and operator

Get file chmod numerical Value (Mac)

$ stat -f “%OLp” [FILE]

List all processes

$ ps -A

Recursive grep

$ grep -R ‘unity.cloud_userid’

Copy 5 lines of the end of file to another file

$ tail -n [NUM_LINES] [SRC_FILE] > [DEST_FILE]

Extract tgz to a specific folder

tar zxvf [FILE].tgz -C [DESTINATION]

File Stats

stat [FILE]

Unzip with file wildcard

unzip ‘[FILE_PREFIX]*.zip’

Follow changes in a file

tail -f [FILE]

List files to check with symlink

ls -al

Output file to clipboard (Mac)

cat [FILE] | pbcopy

Paste to file (Mac)

pbpaste > [FILE]

output directories

find .

Make intermediate directories

mkdir -p [DIRECTORY_WITH_SUBDIRECTORIES]

Go back to previous directory

cd -

Gunzip all files in a directory

for file in *.gz; do gunzip $file; done

Get a list of name servers

dig NS [domain]

Test name server

$ dig [domain] [@_NAME_SERVER]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment