Skip to content

Instantly share code, notes, and snippets.

@bstonedev
Created June 7, 2020 00:33
Show Gist options
  • Save bstonedev/75ee9777005126a84eeb485532081340 to your computer and use it in GitHub Desktop.
Save bstonedev/75ee9777005126a84eeb485532081340 to your computer and use it in GitHub Desktop.
MacOS Terminal Commands - Quick Reference
## Controls ##
control D # exit bash session
## Commands ##
file # show file type
cat # show file content in terminal
less # use page reader in termainl
open # open file
ls # list files in directory
ls a* # list all files that start with an 'a'
ls -R dir ## long form list in dir directory
ls -dl dir # show info about the directory, not the contents
mv a b ## CAREFUL: rename
mv a dir/b ## CAREFUL: move and rename
touch
mkdir # make new directory
rm file.txt ## CAREFUL!
rmdir dir ## CAREFUL: will remove empty directories only
rm -r dir ## CAREFUL: will remove directory and everything in it
rm -ri dir ## adds -i switch to recursive directory delete command
cp -R sourceDir targetDir #copy sourceDir to targetDir
cp -Ri sourceDir targetDir # adds -i switch to cp command
cp -R sourceDir1 sourceDir2 targetDir # copy two directories to targetDir
uniq file # filters out repeat lines
uniq -c file # filters out repeat lines, shows number of repeats
cut # select speific column in file
wc # count command
ls | head # see first 10 lines of the ls output
ls | tail # see last 10 lines of the ls output
## Grep ##
grep 1978 oscars.txt # search file for '1978' string in file
grep -i steve oscars.txt # search file for 'steve' string in file, case insensitive
grep -v steve oscars.txt # search file for everything except 'steve'
## Sort ##
sort file # sort alphabetically based on 1st column
sort -k2 file # sort alphabetically based on 2nd column
sort -n file # sort numerically based on 1st column
sort -nr file # reverse sort numerically based on 1st column
sort file | uniq -c # sorts file and listed unique lines with counts of each line
sort -d\; # sort file with : delimiter, instead of default space delimiter
## Output ##
ls > listing.txt # saves output to 'listing.txt' file
cat > cat-output.txt # creates a simple editor prompt which pushes content to cat-output.txt
ls >> listing.txt # appends output to the end of 'listing.txt' file
echo "hello" >> listing.txt # appends "hello" to the end of 'listing.txt' file
## Pipes ##
# takes output of one program and makes it input in another program
## Command Substitution ##
echo "hello $(whoami)" # executes whoami command within $()
echo "today is $(date)" # executes date command within $()
## Movement keys ##
Ctrl-a # Start of line
Ctrl-e # End of line
Ctrl-u # Delete from start of line
Ctrl-r # Search command history
## Wildcards ##
? # matches exactly one character
[acd7_] # matches anything with one of the characters in the list
[^acd7_] # matches anything the characters in the list
[a-z],[0-9] # matches anything with the range of characters in the list
## Brace Expansion ##
touch {a..c}{1..3}.txt # a1.txt -> c3.txt
## vi (vim) editor ##
Press "i" for insert mode
Press Esc for command mode
When in command mode...
:w to save file
:q to exit
:q! to exit without saving
## emacs ##
F10 to see menu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment