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.
🤓 Useful bash scripts to do automatable tasks with a single command
🤓 Utility bash scripts
Utility bash scripts to do automatable tasks with a single command. We have scripts to download youtube videos, download music from youtube, convert media files, etc.
Contribute and add your secret script.
📝 NOTES
Download scripts download to ~/Downloads/ folder. For videos, they download to ~/Downloads/Videos and for audio, they download to ~/Downloads/Music.
For best results, clone this git repo to a fixed location on your computer and add it to $PATH.
cd~
git clone https://github.com/aviaryan/utility-bash-scripts.git utility-scripts
cd utility-scripts
export PATH="$(pwd):$PATH"