Some helpful Bash scripts I use daily

Avi Aryan - Dec 25 '19 - - Dev Community

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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]*'"
Enter fullscreen mode Exit fullscreen mode

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)" 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.


EDIT - Forgot that I had a GitHub repo of some actual bash scripts. Check it out. It's a good collection. 😀

GitHub logo aviaryan / utility-bash-scripts

🤓 Useful bash scripts to do automatable tasks with a single command

🤓 Utility bash scripts

Contributors needed Build Status

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"
Enter fullscreen mode Exit fullscreen mode

📜 SCRIPTS

🔻 Download video from YouTube in MP4 format

Script: youtube-video
Dependencies: youtube-dl, ffmpeg, aria2c (optional)

youtube-video "https://www.youtube.com/watch?v=HgfojLtSBTM"
Enter fullscreen mode Exit fullscreen mode

🔀 Merge video and audio together

Script: vamerge
Dependencies: ffmpeg

vamerge <path to video file> <path to audio file>
# the order is important, first video, then audio
Enter fullscreen mode Exit fullscreen mode

First published in my newsletter.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player