Navigating Setting and viewing variables Starting (local) repo
cd name_of_directory git init cd .. # Go up one PLANET="world" cd ~ # Go to home echo "Hello $PLANET" Starting with repo from GitHub pwd # Where am I? env # Show ALL variables # Using HTTP (prompt for pw) Listing files Run command with variable set git clone https://github.com/U/R ls # List files # Using SSH (requires setup) ls -a # See hidden DEBUG=true npm start git clone git@github.com:U/R.git ls -l # See more info ls -R # Recursive Adding and committing Bash: History git add -A # "Stage" all Moving and renaming git commit -m "Fixed :)" mv file.txt new_name.txt Finding out status mv file.txt ../new/place/ History commands git status Copying cd - # go back a directory git log cp file.txt file_backup.txt history # view all commands !! # last command you typed Learning about past cp -r directory/ backup/ sudo !! # ditto, but as sudo git log # Q to quit Deleting git show f85bfcf Shortcut: Last command <Up> git diff f85bfcf master rm file.txt git checkout f85bfcf rmdir empty_directory/ Shortcut: Search through history rm -r full_directory/ <Ctrl+R> then start typ- Branch workflow Creating ing, <Ctrl+R> to cycle back, git branch my-stuff <Enter> to run. git checkout my-stuff mkdir my_directory # After you do some work... touch empty_file.py git add -A Bash: Process management git commit -m "New logo :)" Reading data from file git checkout master cat filename.txt git merge my-stuff cat file1 file2 file3 Multiple commands Interacting with remotes (e.g. GitHub, Heroku) c1 ; c2 # run c2 after c1 Bash tricks c1 && c2 # run c2 if c1 succeeds git remote -v # check remotes c1 || c2 # run c2 if c1 fails git pull # get updates c1 & c2 # run both at once # After you do some work... Auto complete Start typing then hit git add -A <Tab>. Hit twice for options. git commit -m "it works!" Job control Redirecting output into file git push # share updates
ls -R > all_files.txt npm start & # run in bg
cat a.html b.html > c.html ps # show shell’s processes Bash: Searching cat d.txt >> c.txt # Append jobs # show bg processes fg # foreground last process Piping output Hook commands up <Ctrl+Z> # pause; put in bg find: Search by filename # keep background process [1] # Pipe output to "grep" filter disown %1 # running forever # Using wildcard for search by python start.py | grep "http" find . -name ’*.py’ # extension node run.js | tail # Only end Viewing all processes find . -name views.py # Exact find . -iname iNFo # Any case Wildcard expansions # Find modified in last 7 days ps -e # show all processes find . -mtime 7 -iname info rm *.jpg # Delete jpg files ps -ejH # show process trees rm ./**/*.jpg # ** matches dirs ps -e | grep python # filter grep: Search contents of files Running file as bash script # Search templates for "free" Killing processes grep -r free ./templates/ # Save a sequence of commands to grep -lr free . #...list names # file with "#!/bin/bash" at top kill 4264 # kill process by PID grep -ir ToDo . # Ignore case bash scrpt.sh # Always works killall python # ...or by name # Using Regular Expressions ./scrpt.sh # Works if executable kill -9 4264 # -9 "forces" kill grep -er ’(http|ftp)s?:’ .
Useful CLI tricks for macOS and GNU/Linux kickstartcoding.com A cheatsheet by Kickstart Coding