Skip to content

Instantly share code, notes, and snippets.

@culurciello
Forked from Atcold/My-Unix-shortcuts.md
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save culurciello/b088023031be99a5d973 to your computer and use it in GitHub Desktop.
Save culurciello/b088023031be99a5d973 to your computer and use it in GitHub Desktop.

My Unix shortcuts and tips

Bash

How to list disks and partitions

sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL

Searching a string within a directory

grep -Inri "<keyword to search for>" .

Show all files' name and content

find . -type f -exec sh -c "echo '{}'; cat {}; echo '\n'" \;

Show how many files each folder contains

find . -maxdepth 1 -type d -exec sh -c "echo {} \`ls {} | wc -l;\`" \;

Groups

List groups for user

groups <user>

Add new group

groupadd <group>

Add user to (secondary) group

usermod -aG <group> <user>

Group belongness inheritance

chmod g+s <folder>

Accessing permission for folders

find <folder> -type d -exec chmod g+s {} +

Ssh without password

a@A ~ $ ssh-keygen -t rsa
a@A ~ $ ssh b@B mkdir -p .ssh
a@A ~ $ cat ~/.ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
a@A ~ $ ssh b@B

Killing a process

ps -a | grep <program to kill>
kill 9 <ID of program to kill>

Checking size

du -chs *

Get info on process (RAM, time)

/usr/bin/time -v <running instruction>

Tar and untar

tar -czf myTar.tar.gz <files>
tar -xf myTar.tar.gz

Add -v for verbose output.

Logout users

who -u
kill "pid"

Ubuntu packages

Know if package is installed

apt-cache pkgnames <packageName>

Know what a package installs

dpkg -L <packageName>

Discover what shared objects are used

lld <library/executable>

Git

Reset HEAD to origin/master

git fetch origin
git reset --hard origin/master

Reset HEAD to last commit

git reset --hard

Show branch status

git fetch origin
git show origin master

Partial stash (no idea of what this does anymore)

git add fileName
git stash --keep-index

Tracking history

git log --decorate --graph -p -- fileName
git blame fileName

Remove untracked files (and directories) (and ignored files -x)

git clean -fd

Start commit message with #

git commit --cleanup=whitespace

And, you have to manually remove every "comment" line starting with #

See what you've merged

git diff <oldHash> <newHash> --stat
git diff <oldHash> <newHash> -- fileName

Discard untracked changes

In order to keep only the added changes do

git stash --keep-index

Full diff, word by word

git diff --color-words -U9999999

Pruning on-the-fly

git fetch -p

Forget/remember to track files

git update-index --assume-unchanged <file>
git update-index --no-assume-unchanged <file>

Vim

Removing trailing whitespaces

:%s/\s\+$

Pasting yanked text while in insert mode

<c>-R 0

Exploring file's location / split and explore

:Ex
:Sex

Show command and search history

q:
q/

Find within a project (excluding binaries)

:grep -rI "<text>" .
:cw

Vimdiff

Sourcing correct file during conflict

:diffg RE/BA/LO

Previous and next conflict

[c
]c

Tmux

Move windows around

move-window -t <nb>

Attach and disattach others

tmux attach -d

Mac

Show and hide hidden files in Finder

defaults write com.apple.finder AppleShowAllFiles TRUE
defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment