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.
EDIT - Forgot that I had a GitHub repo of some actual bash scripts. Check it out. It's a good collection. π
aviaryan / utility-bash-scripts
π€ 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"
π 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"
π 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
First published in my newsletter.
Top comments (15)
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.
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)
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
forgit reset --hard HEAD
I hate typing π
I thought so too. But "scripts" sounds better. :-p
My favorite is
ys
foryarn start
.I like the way you are thinking π
I keep all my aliases in
.bash_profile
so all I need to do is typealias
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.
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.I need to get back to using the cmd it would help with certain things.
100%. Let me know if you need help with anything. π
Yes. There are even neatly designed $10 menubar apps that do the same thing.
These are awesome.
It's all nice and useful, but tget aren't scripts.
Thanks. These certainly look rock-solid.
Really helpful scripts. Specially backup to dropbox and Kloc script.
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.