Bash Tricks
22 Jan 2021One of the great things about bash is just how flexible and configurable it is. In this post I’ll explain some changes that I use frequently.
This post is mostly for my own benefit, so I can quickly copy these configs onto a new machine and see how the layout styling works.
As a general rule I try not install any packages/plugins or anything which pushes you too far from the stock setup… e.g. zsh!
Bash Changes
Here are the changes I have made to my config in order of most important.
Git Branch Highlighting
I find knowing that I am in a git repo one of the most important pieces of information. On top of that it will tell me what branch I am on. This was inspired by the following post.
Speed Aliases
ve
Typing this in any repo will generate a virtualenv and source it automatically. I use this all the time and is great when experimenting and exploring github repos. Virtualenv is supposed to be replaced now with venv.qg
Stands for quick git. It’s ruining the git commit messages but it assumes you’ll squash later. Also it’s risky-ish since it’s adding everything with thegit add .
but each to their own.gs
Stands forgit status
. Used in conjunction with #2. There is already a program in calledgs
in/usr/bin
something to do with pdfs, i’ve never used before. Apparently if you want to not use the alias you can easily skip by escaping… so\gs
will return the Ghost shell.rl
When working with a team it’s great to send them the exact full path of the file. I use it so often andreadlink -f
takes too long.gf
Useful if you use gitflow.gf init
Audio Aliases
wavcount
Counts the number of hours:mins:secs of audio data is in a directory recursively.
rob@rob-T480s:~/projects/audio (master)$ wavcount
06:49:34
Android Changes
When I work on Android, I find the following are useful. The first is aver
which is a special alias which is exported so that it can take advantage of using a variable $1
in the middle of the command.
aver
- Stands for APK version. I can runaver apk_name.apk
it prints the APK version information using the aapt tool.
rob@rob-T480s:~/$ aver spotify.apk
package: name='com.spotify.music' versionCode='65804403' versionName='8.5.82.894' platformBuildVersionName=''
sy
- Scrcpy (presumably pronounced Screencopy) is a great tool to see the android interface on your machine. The commandsy
starts scrcpy with watch -n 2 which will try to restart it every 2 secs as soon as it ends. Useful for development purposes.
Which bash configuration file to use?
To .bashrc or .profile? That is the question.
.profile
is executed on startup only. .bashrc
is executed every time you open a new terminal. I generally use .bashrc since I think it’s more common. I also put the following snippet into my code so I can test when each gets executed. I put this in both .bashrc
and inside .profile
just append it to the end.
rob@rob-T480s:~/projects/blog/github.io (master)$ source ~/.profile
.bashrc was executed
.profile was executed
rob@rob-T480s:~/projects/blog/github.io (master)$ source ~/.bashrc
.bashrc was executed
Useful crontabs
I want to purge my ~/Downloads
folder for files and directories older than 15 days, but I want to send them to the Trash instead. The trash I want to empty at a longer every 180 days automatically on reboot only.
User: crontab -e
:
0 0 * * * find /home/username/Downloads/ -mindepth 1 -mtime +15 -exec gio trash {} \;
Root: sudo crontab -e
:
@reboot find /home/username/.local/share/Trash/expunged/ -type f -mtime +180 -exec rm {} \;
@reboot find /home/username/.local/share/Trash/files/ -type f -mtime +180 -exec rm {} \;
@reboot find /home/username/.local/share/Trash/info/ -type f -mtime +180 -exec rm {} \;