Bash Script Helpers for Every Developer
By: Shinichi Okada
Tags: Terminal, Command Line, Git, Bash, Dry

4 Bash Script Helpers to Get More Done

DRY (Don’t Repeat Yourself), Automate Them

Image by Andrew Martin from Pixabay

Introduction

When you are using terminal and developing your awesome apps, you find yourself repeating a series of commands often.

In this article, you will find bash scripts that you can use to automate some of your workflows.

Checking your command history

If you are interested in finding out your 10 most used commands stored in your history you can run:

history | sed -e 's/ *[0-9][0-9]* *//' | sort | uniq -c | sort -rn | head -10

My 10 most used commands stored in my history.

I used ls 887 times which is 10% of the total numbers. I can use a space before commands to remove from the command history list.

What are your 10 most used commands?

How to set up your environment to run Bash Script on your computer

The bin directory and PATH

Let’s create a directory and add the path to the terminal config file (.bashrc, .zshrc). By doing this you can run your script as myscript in any directory.

In your home directory, create the bin directory:

$ mkdir bin

Add the path to this directory in your terminal configuration file. (either, .bashrc or .zshrc)

export PATH="$PATH:$HOME/bin"

Reload the config file:

# for .zshrc

$ . ~/.zshrc

# or

$ source ~/.zshrc

# for .bashrc

$ . ~/.bashrc

# or

$ source ~/.bashrc

We will store all our scripts in this bin directory. You need to make Bash Script files executable:

$ chmod 755 bash_file_name

1. Run Gitig when .gitignore is not working

When I modify .gitignore , I have to run the following commands. This recursively removes all unstaged and paths from the index. And it adds all files, commit, and push to the origin main.

$ git rm -rf --cached .

$ git add . # I use Oh-my-zsh alias ga .

$ git commit -m "Gitignore update" # I use alias gcmsg "Gitignore update"

$ git push origin $(current_branch) # I use alias ggp

Gitig will do that for you.

$ gitig

> Removing cached from shinokada/bin.git (fetch) shinokada/bin.git (push) ...

...

...

> Running git add . ...

> Running git commit -m ...

[main 1b1a63b] > Gitignore updated at 2020-12-13 13:02:10

3 files changed, 7 insertions(+), 19 deletions(-)

create mode 100644 .gitignore

delete mode 100644 .vscode/launch.json

> Running git push origin main ...

Enumerating objects: 6, done.

Counting objects: 100% (6/6), done.

Delta compression using up to 4 threads

Compressing objects: 100% (3/3), done.

Writing objects: 100% (4/4), 407 bytes | 407.00 KiB/s, done.

Total 4 (delta 2), reused 0 (delta 0)

remote: Resolving deltas: 100% (2/2), completed with 2 local objects.

To github.com:shinokada/bin.git

c6b79de..1b1a63b main -> main

> Gitignore updated at 2020-12-13 13:02:10

Here is the Gitig Bash Script.

2. Cleanit to move files from Download to Trash

This script moves files which are older than 20 days from ~/Download directory to ~/.Trash directory.

The default target directory is the ~/Download .

Use -f switch (default is ~/.Download) to change the target directory.

switch (default is ~/.Download) to change the target directory. Use -d switch to set days to indicate the maximum days to keep files.

switch to set days to indicate the maximum days to keep files. Use -t switch (default is ~/.Trash) to set the directory where you want files to move to.

Example 1: Moving files that are older than 20 days in ~/Download to ~/.Trash .

$ cleanit

cleanit example. Image by Author.

Example 2: Remove files that are older than 2days from ~/.Trash . Warning: This will remove files/dirs permanently from ~/.Trash .

$ cleanit -d 2 -f trash

cleanit -d 2 -f trash result. Image by Author.

Example 3: Move files that are older than 10 days in ~/Mydir to ~/.Trash .

$ cleanit -d 10 -f ~/Mydir

Example 4: Move files that are older than 5 days in ~/Mydir to ~/Anotherdir .

$ cleanit -f ~/Mydir -d 5 -t ~/Anotherdir

Here is the Cleanit Bash Script.

How to run a cronjob with Python-crontab

Rather than running the above script, you can set up a cronjob to clean your Downloads and Trash directories.

You can set up a scheduled job using a cronjob/launchd on Linux or macOS or using Python-crontab.

For macOS users, you need to give Full Disk Access to usr/sbin/cron .

Finding the cron path. Image by Author.

Open System Preferences > Security & Privacy > Full Disk Access, then click to unlock to make changes. Click the plus sign to add your cron path. You need to hit CMD+Shif+. to make invisible items visible.

Adding cron to Full Disk Access on macOS. Image by Author.

Let’s set up a cronjob using Python-crontab.

$ mkdir ~/Cron

$ cd ~/Cron

$ touch cleanit_job cleancron.py

$ chmod 755 cleanit_job

$ pip install python-crontab

# if you are macOS user

$ brew install terminal-notifier

We create the ~/Cron directory and cleanit_job and cleancron.py files within the directory. We make the cleanit_job executable. Then install python-crontab and terminal-notifier .

The python-crontab reads and writes crontab files and accesses the system cron. The terminal-notifier is a command-line tool to send macOS User Notifications.

Let’s write the code for cleanit_job :

cleanit_job bash script.

How to store the command’s output to a variable

In order to store the cleanit outputs in a variable, we can use $(cmd) , for our case $(cleanit) . Then we can use the variable in the notifier message.

Crontab file

Let’s fill up the code in the cleancron.py .

We import Crontab , remove all the previous cronjobs, create a new cronjob, and set how often we want to run the job. We set it every day at 10 AM. We write the cronjob and list all the cronjobs at the end.

On a terminal, let’s run it:

$ python cronjob.py

python cleancron.py output. Image by Author.

To check your cronjob:

$ crontab -l

crontab -l output. Image by Author.

Terminal-notifier result on the screen. Image by Author.

3. Replaceit to replace spaces with underscores

This script replaces spaces in file names and directories with underscores as the default.

$ replaceit

Replacing a space to _. Image by Author.

Using -f and -t options, you can replace any letter.

$ replaceit -f _ -t -

Replacing _ to -. Image by Author.

More usages:

# Replacing _ to space

$ replaceit -f _ -t " "

# Replacing - to _

$ replaceit -f -

Here is the Replaceit bash script.

4. Gitstart to initialize your Git repository

Gitstart does the followings:

Pulls .gitignore when you specify a programming language.

when you specify a programming language. Creates a README file.

Runs gitinit .

. Creates a repo at Github.

Add/commit/push to the Github repo.

$ cd my_new_repo

$ gitstart python

Gitstart in action. Image by Author.

Conclusion

I hope you find them useful. The Bash script is a great tool to automate your workflows. Let me know what you repeat often during your development in the comment.