A bunch of useful bash commands.
Profile with perf
.
perf record -g ./a.out
perf report -g graph,0.5,caller
List installed paquets.
dpkg --get-selections > .paquets
Add user to group sudo.
usermod -a -G sudo <user>
Larger than 1MB.
find . -type f -size +1M
At most 5 days old (modification time).
find . -type f -newermt '-5 days'
Replace tabs by spaces in a project.
mkdir tmp
find . -name "*.cpp" -type f -exec bash -c 'expand -t 4 "$0" > tmp/expanded && mv tmp/expanded "$0"' {} \;
rm -R tmp
Count lines of sources files in project.
find src -name *.[hc]pp -print0 | xargs -0 wc -l
Find instances of "foo" in a project and print the filename/line numbers.
find src -name *.[hc]pp -exec grep -H -n "foo" {} \;
Replace "foo" by "bar" in a project. Pro tip: sed accepts any delimiter character.
find src -name *.[hc]pp -exec sed -i "s#foo#bar#g" {} \;
Set good default permissions after transfer through non-Unix filesystem.
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
Compress PDF files larger than 1MB.
find . -type f -name "*.pdf" -size +1M -exec xz --verbose {} \;
Sum sizes of all PDF files.
# "tr" merges delimiters together.
# The 5th column contains the file size.
# "paste -d+" concatenates lines with a "+" delimiter.
# "bc" calculates the input.
find . -type f -name "*.pdf" -exec ls -l {} \; | tr -s ' ' | cut -d' ' -f 5 | paste -sd+ | bc
Squash all git commits.
git reset $(git commit-tree HEAD^{tree} -m "Initial commit")
Diff directories.
git diff --no-index foo/ bar/
Sync with upstream repo.
git remote -v
git remote add <remote-name> <url>
git fetch <remote-name>
git rebase <remote-name>/master
git push
git branch -d fix-***
Create pull request.
git checkout -b fix-***
git commit ...
git push --set-upstream origin fix-***
Fetch branch from contributor.
git remote add <remote-name> <url>
git remote add -t <branch> <remote-name>/<branch>
git checkout <branch>
# Alternatively
git checkout --track <remote-name>/<branch>
Make a local branch track a remote branch.
git branch --track <branch> <remote-name>/<branch>
Prune branches that have been removed remotely.
git fetch --prune
Clone with submodules.
git clone --recurse-submodules https://github.com/USERNAME/REPOSITORY
Sync submodules to fetched version on main repo.
git fetch
git rebase
git submodule status
git submodule update
Add tags.
git tag -a <tag> <commit>
git push --follow-tags
Don't save my password thanks.
git config --global --add credential.helper ""
Set editor properly for commit messages.
git config --global core.editor $(which vim)
Manually edit config.
git config --global --edit
Useful aliases (see https://stackoverflow.com/questions/1057564/pretty-git-branch-graphs).
alias.lg=log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
Setup SSH authentication.
git remote set-url --push origin git@github.com:USERNAME/REPOSITORY.git
Get rid of The following packages have been kept back
.
apt-get --with-new-pkgs upgrade