Skip to content

Instantly share code, notes, and snippets.

@serrodcal
Last active June 29, 2021 07:15
Show Gist options
  • Save serrodcal/d33263561ab5f958ea1662cae818627f to your computer and use it in GitHub Desktop.
Save serrodcal/d33263561ab5f958ea1662cae818627f to your computer and use it in GitHub Desktop.
A Cheat Sheet for MacOS commands

MacOS commands cheatsheet

List

$ ls

Copy a file

$ cp file.txt directory/

Duplicate a file

$ cp file.txt file.txt.back

Copy a directory in a recursive way

$ cp -a sourceDirectory/ targetDirectory/

Move a file

$ mv file.txt directory/

Rename a file

$ mv readme.txt README.md

Delete a file

$ rm directory/

Delte a directory

$ rm -rf directory/

Merge directory

$ rsync -a images/ images2/
$ rsync -av images/ images2/

Create a new file

$ touch file.txt

Create a new file with some content

$ ehco "some information" > file.txt

Create a new directory

$ mkdir directory/
$ mkdir -p src/main/java/com/package

Show file content

$ cat file.txt
$ less file.txt

Show information for a given file

$ stat -x file.txt

Show file/directory size

$ du -sh directory/

Zip a directory

$ zip -r target.zip directory/

Unzip a directory

$ unzip target.zip

Show zip's content

$ zipinfo target.zip

Show a tree

$ tree
$tree -L 1

brew install tree required.

Find a file

$ find . -iname "*.info"
$ find . - -mtime +5
$ find . -name "file.txt" -delete

Show calendar

$ cal
$ cal 11 2020

Get date

$ date
$ date -d "+7days"

Simple calculator

$ bc

Show running process

$ pstree

brew install pstree required.

Kill a process

$ kill <pid>

Kill all the process with the same name

$ killall <name>

Send request

$ curl http://localhost:8080/hello
$ curl ifconfig.me
$ curl -X POST http://localhost:8080/increments -H 'Content-Type: application/json' -d '{"key":"count","value":0}' -w '\n' -iv
$ curl -X POST --cert client.crt --key client.key --cacert ca.crt --pass <passwd> https://localhost:8080/files -H 'Content-Type: multipart/form-data' -F 'file=@"./README.md"' -F 'payload="{\"path\": \"./README.md\"}";type=application/json' -w '\n' -k

Filter/Find inside files

$ grep file.txt
$ grep -rnw . -e 'Some content'

Open file/directory

$ open file
$ open directoy/

Append lines at the end of a file

$ echo "some information" >> file.txt

Show disk usage

$ df -h

Check if there is a process listening in a given port

$ nc -zv <host> <port>

Who is listening on a given TCP port

$ lsof -iTCP:8080 -sTCP:LISTEN

Hotkeys

Ctrl + A  Go to the beginning of the line you are currently typing on
Ctrl + E  Go to the end of the line you are currently typing on
Ctrl + L  Clears the Screen, similar to the clear command
Ctrl + U  Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H  Same as backspace
Ctrl + R  Lets you search through previously used commands
Ctrl + C  Kill whatever you are running
Ctrl + D  Exit the current shell
Ctrl + Z  Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W  Delete the word before the cursor
Ctrl + K  Clear the line after the cursor
Ctrl + T  Swap the last two characters before the cursor
Esc + T   Swap the last two words before the cursor
Alt + F   Move cursor forward one word on the current line
Alt + B   Move cursor backward one word on the current line
Tab       Auto-complete files and directory names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment