I posted a tweet about how investing time learning Bash scripting and various command-line tools is always worth it.

Every time I learn a command-line tool, I am like how did I work without this. Command-line tools are awesome. In this email, I will talk about a few bash scripts or aliases I use regularly. You should find a few of them useful in your digital life.

Preventing system sleep in Mac

System sleep in Mac can be a huge problem if you have an ongoing task like a large download as it stops the task. So I use this alias to prevent my Mac from sleeping.

alias nosleep=caffeinate -t 360000

I place this in my .bashrc file. The parameter 360000 is seconds to keep awake so this command keeps my Mac awake for 100 hours, enough time to finish that download.

Counting lines of code in a project

While working on some large projects, I like to keep track of how much work has been done. This one-liner helps to do so.

alias sloc="git ls-files \"*.js*\" \"*.scss\" | xargs wc -l"

This is currently set to include js, jsx , json and scss as source code files. Here is how the output looks like.

View list of aliases in your source file

If you have lots of aliases in your source file and tend to forget what you named them, this command will help. I keep all my aliases in a file called mymacrc which is further included in .bashrc using source ~/mymacrc.

So if I want to see the list of all aliases, I use this.

alias lsalias="grep -in --color -e '^alias\s+*' ~/mymacrc | sed 's/alias //' | grep --color -e ':[a-z][a-z0-9]*'"

Remove Dangling Docker images

Dangling Docker images are temporary images created by Docker to run a container. You can see them by running docker images. They have no name and are usually copies of their parent image. Although, they still take up disk space and for some reason, aren’t removed by Docker as quickly as they should be.

To remove them, I have this one-liner.

alias dcdangling="docker rmi \$(docker images -f \"dangling=true\" -q)" 

Back up key files to Dropbox

Dropbox recently stopped supporting sync within symlinks. If you are like me, you probably used Dropbox just for that. Needless to say, I was disappointed. But then I found another way to sync to Dropbox. By using rsync.

But rsync doesn’t work with Dropbox directly. There is a wrapper over rsync called rclone that can do this for us.

Here is the code that syncs a folder in my HOME directory called syncs/osxdropbox to the Dropbox root folder called osxrsync.

rclone sync -L --exclude ".DS_Store" /Users/aviaryan/syncs/osxdropbox dropbox:osxrsync

This directory, in turn, contains symlinks to all other folders I want to sync. For example, here is the list of folders I am syncing to Dropbox.

v="/Users/aviaryan"
d="$v/syncs/osxdropbox"

ln -s "$v/scripts" "$d/scripts"
ln -s "$v/.ssh" "$d/.ssh"
ln -s "$v/Documents" "$d/Documents"
ln -s "$v/AlfredSettings" "$d/AlfredSettings"
ln -s "$v/mymacrc" "$d/mymacrc"

And you can see the same in my Dropbox.

But running rclone every time is not a good idea. To do it automatically, I use the system’s cron.

# edit crontab
crontab -e
# add a line for our cron job
0 14 * * * rclone sync -L --exclude ".DS_Store" /Users/aviaryan/syncs/osxdropbox dropbox:osxrsync

The above cron job runs the dropbox backup every day at 2 pm. Since my system is usually switched on at 2 pm, it works great for me.