DEV Community

Cover image for Some helpful Bash scripts I use daily
Avi Aryan
Avi Aryan

Posted on

Some helpful Bash scripts I use daily

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.

Top comments (15)

Collapse
 
kogans profile image
Stanislav Kogan

There is actually a nasty reason for this: thermal management in macbook pros is so bad, that sleep is often needed to prevent it from melting down.
Why would anybody spend money on this nasty platform is beyond me.

Collapse
 
aviaryan profile image
Avi Aryan • Edited

I have been looking to upgrade my machine but all the premium non-mac laptops (dell XPS 15, surface laptop, HP Spectre, Thinkpad x1 carbon) cost the same or more than the new MacBook Pro 16".

Which non-MacBook laptop would you recommend?

Also, I hear they fixed the thermal management in new 16'".

(talking about Indian prices here)

Collapse
 
thomasjunkos profile image
Thomas Junkツ

Thank you for the nice tips!

You should switch the to "Bash aliases" πŸ˜‰

I love to alias the hell out of my terminal. Mostly two or three letter aliases

grh for git reset --hard HEAD

I hate typing πŸ˜‚

Collapse
 
aviaryan profile image
Avi Aryan

I thought so too. But "scripts" sounds better. :-p

My favorite is ys for yarn start.

Collapse
 
thomasjunkos profile image
Thomas Junkツ

I like the way you are thinking πŸ˜‰

Collapse
 
henrysabio profile image
Henry

I keep all my aliases in .bash_profile so all I need to do is type alias and it automatically responds back with a list of aliases I’ve made.

No need for an alias for my aliases.
But then again macOS runs the login shell .bash_profile by default every time.

So I guess this would indeed be useful and not so redundant for anyone who’s machine is setup differently.

Collapse
 
aviaryan profile image
Avi Aryan

It works for me too since I have source(d) that file into .bash_profile. But it lists everything and so finding my custom aliases will be a pain.

Collapse
 
kleene1 profile image
kleene1

I need to get back to using the cmd it would help with certain things.

Collapse
 
aviaryan profile image
Avi Aryan • Edited

100%. Let me know if you need help with anything. 😁

Collapse
 
aviaryan profile image
Avi Aryan

Yes. There are even neatly designed $10 menubar apps that do the same thing.

Collapse
 
tylerlwsmith profile image
Tyler Smith

These are awesome.

Collapse
 
kogans profile image
Stanislav Kogan

It's all nice and useful, but tget aren't scripts.

 
aviaryan profile image
Avi Aryan

Thanks. These certainly look rock-solid.

Collapse
 
shubhamshubhankar profile image
Shubham Subhankar Sharma

Really helpful scripts. Specially backup to dropbox and Kloc script.

Collapse
 
aviaryan profile image
Avi Aryan

Thanks. I was anxious why no one was talking about the Dropbox script.

It helps me avoid using solutions like TimeMachine and BackBlaze because all my important stuff is backed up every day.