Skip to content

Instantly share code, notes, and snippets.

@thejmazz
Last active June 29, 2017 20:26
Show Gist options
  • Save thejmazz/b5695f768ef7d899a294 to your computer and use it in GitHub Desktop.
Save thejmazz/b5695f768ef7d899a294 to your computer and use it in GitHub Desktop.
Misc command line things that are good to know

tmux

sed (Stream Editor)

  • sed s/-/\ /g substitute globally (- to spaces in this case)

users and groups

  • list all users: cut -d: -f1 /etc/passwd OR compgen -u
  • list all groups: compgen -g
  • see logged in users: users
  • get groups current user belongs to: groups
  • make a user: sudo adduser username
  • default user settings in /etc/adduser.conf (you can configure default groups, skel directory, etc.)
  • skeleton directory default: /etc/skel (may need to mkdir)
  • delete a user and their home folder: sudo deluser username --remove-home

git

  • if you are new to using git:
    1. Don't use the desktop gui: if you can cd, ls, you can use git
    2. *before each commit, before each commit, run git status. you should understand M, untracked, new file, etc. Until you can are confident what git status is going to say, (which is never 100%), you should run git status before committing.
  • git checkout -b branchName origin/branchName to fetch remote branch
  • git push origin --delete branchName to delete a remote branch (do git branch -d <branch_name> to delete local branch)
  • git clone -b <branch> <remote_repo> to clone a repo at a specific branch (useful for putting gh-pages branch in dist folder)
  • git checkout HEAD -- my-file.txt to hard reset a specific file
  • git reset --hard to hard reset all modified files back to their state at the specified commit (or latest). leaves new files (but don't quote me on that)
  • git reset to go back to last commit. leaves files modified and new files. i.e. git status will show the same as it did just before git commit
  • git reset --hard to simulate a rm project, git clone project, but it might keep your new files?
  • git reset --soft to keep the changes. can basically commit with new message at this point.
  • git tag -a v0.1.0 -m "0.1 Release" to tag the current commit, git push origin --tags to push tags
  • git subtree
  • renaming a branch:
    1. If it is your current branch just do. git branch -m new_name.
    2. If it is another branch you want to rename. git branch -m old_name new_name.
    3. If your branch was pushed, then after renaming you need to delete it from the remote git and ask your new local to track a new remote branch. git push origin :old_name.

storage

  • df -h to get disk usage
  • sudo du -sh ./* to see how much space each file/file in cwd is using
  • sudo du -sh ./* | gsort -rh to sort by size. sort on linux. brew install coreutils on osx

curl

Simple get:

curl http://localhost:3000/test

Use -O to send output to name of url. -o thingy.html into a file.

JSON POST:

curl -X POST http://localhost:3000/user/signup -H "Content-Type: application/json" -d'{"username":"bob", "password":"Password1"}'

Tar

compress zee file verbosely!

tar czfv output.tar.gz file-or-folder

Note: -czfv will not work, but -zcvf will. The first is easier to remember, but it takes a little while to also remember it does not use a dash.

extract zee file verbosely!

tar xzfv thingy.tar.gz

Look inside a tarball without extracting:

tar -tf filename.tar.gz

Networking

Check which ports are used

netstat -tulpn

On OSX:

lsof -n -i:3001 | grep LISTEN

kill it:

kill `lsof -n -i:3001 | grep LISTEN | tail -n1 | awk '{print $2}'`

Grep for a specific port:

netstat -tulpn | grep :80

Get private IP:

curl -w "\n" http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address

Processess

Get PIDs:

ps -aux | grep name

or

pgrep name

Vim

You should also know you way around Vim. Good not just for editing, but also viewing long log files for example.

  • "*y" to copy into system clipboard
  • :%!python -m json.tool to format JSON
  • :e! to reload current file back in
  • :tabedit . to open a new tab
  • :set paste to turn off autoindent when pasting, :set nopaste to go back to normal
  • $ to go to end of line
  • G to go end of file, 42G to go line 42
  • :set scrollbind in each pane to bind scrolling, or :windo set scrollbind to do it for every open pane at once
  • :read !echo $PATH to read the output of a command into the current position
  • /canBeRegex to search; set hlsearch to have results highlighted, :noh to turn off highlighting until next search
  • vim +PluginInstall +qall to install vundle plugins from outside vim
  • za, zc to open/close folds
  • with vim-foldsearch
    • :Fw to show lines which contain word under cursor. use case: show all lines with o in .obj file
    • :Fs to fold lines with match last search. use case: search for /^[og] to find all object and group declarations, then fold to see them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment