DEV Community

Vinod Mathew Sebastian
Vinod Mathew Sebastian

Posted on • Edited on

How to Save Time and Improve Workflow by Automating with Bash Scripts

The command line is the fastest, easiest, and sometimes the only way to work with a computer. Ask to substitute spaces with underscores from filenames - a few hundred of them in a folder - and you know what I mean.

Even better, we can write bash scripts and use them. We can reuse them for repetitive tasks. And save a lot of time and improve workflow in the process.

Here, I am going to write three scripts that automate regular tasks.

  1. A custom command to control the brightness of the screen
  2. A script to open any file in any folder by typing only a part of the filename, and
  3. Another script to replace spaces in filenames with underscores.

Prepping

We need a folder to save our scripts. In the home directory, make a new folder.

:~$ mkdir automation_scripts
Enter fullscreen mode Exit fullscreen mode

This is the directory we save all our scripts. We need to add this to the $PATH variable to make our scripts executable from any location on this computer. And they become commands.

Locate the .bashrc file and open it with a text editor. Append this line to the end of the .bashrc file

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

Run the command source $HOME/.bashrc for the changes to take effect.

If we do this, there is no need to restart the system.

Now we shall write and save scripts inside this folder.

Change the display brightness with a custom command

We can use the xrandr command for changing brightness.

From the man pages:

It is used to set the size, orientation and/or reflection of the outputs for a screen. It can also set the screen size.

The syntax is:

xrandr --output value --brightness value

Firstly, we have to find the --output for our use case.

Type xrandr --q on the command line and press ENTER.

We get something like this:

Screen 0: minimum 320 x 200, current 1366 x 768, maximum 8192 x 8192
eDP-1 connected primary 1366x768+0+0 (normal left inverted right x axis y axis)
Enter fullscreen mode Exit fullscreen mode

Another command is xrandr --listmonitors

See the line eDP-1 connected primary --

So here --output shall be supplemented with eDP-1.

Just be sure to replace it with whatever you get from your screen.

The brightness value can be anything between 0.1 and 1.

While 1 is the normal display brightness, anything below 0.5 shall be pretty dark. So use it with caution.

Now we create a file called brightness inside automation_scripts and add the following lines.

#1 /bin/bash
# A simple script to control brightness of the screen.

param=$1

xrandr --output eDP-1 --brightness ${param:-0.8}

Enter fullscreen mode Exit fullscreen mode

Let me explain the code a little bit.

I'm using the positional parameter $1 inside the script. It takes the value we give when we invoke it. With ${param:-0.8} we set a default value of 0.8 for --brightness.

Enter the following command to make it executable:

:~$ chmod +x ~/automation_scripts/brightness
Enter fullscreen mode Exit fullscreen mode

Now, type in the command

:~$ brightness
Enter fullscreen mode Exit fullscreen mode

and press ENTER.

Open Sesame! The screen dims.

When we ENTER,

:~$ brightness 1
Enter fullscreen mode Exit fullscreen mode

It becomes brighter again.

From now onward, every time we need to change the brightness, no need to type

xrandr --output value --brightness --value

Simply type brightness and a value between 0.1 and 1.

Usage:

brightness <value>
#A value between 1 and 0.1
Enter fullscreen mode Exit fullscreen mode

If we don't enter anything, brightness assumes the default value of 0.8.

This is cool. Isn't it?

Open any file using a script

Commands like open and xdg-open opens any file with the default application. But only if we know the filename. What if we only know only a part of the filename?

For this, we can make a script called fileopen inside the folder automation_scripts.

#! /bin/bash
#Open any file from anywhere on the computer by partly typing the file name.

path=$3

app=$2

ls $3 | grep -E "$1" |  xargs -I {} "${app}" "{}" &
Enter fullscreen mode Exit fullscreen mode

Code Explanation: The ls command lists folder contents, and it is piped into grep which searches for the pattern supplied by us at invocation. The xargs command then makes it the argument for the app we specify to open it with.

The syntax is:

fileopen <filename> <appname> <path/to/file/(optional)>
Enter fullscreen mode Exit fullscreen mode

If we know that there is a song with 'my_heart' somewhere in the filename, we can simply type fileopen my_heart vlc assuming that we are in the same directory and VLC media player has been installed.

Another example: fileopen "some_text_file" xed or

fileopen "some_pdf" xviewer $HOME/pdf/

Substituting spaces in filenames with underscores

In the introduction, I have talked about substituting the spaces in the filenames with underscores. Because when you download something from the internet, say, YouTube videos, the filename might contain spaces. If the filename contains spaces, we can't open it with a script like fileopen from the previous example.

Firstly, we need to remove the spaces from the filename.

So, we make another script called remove_spaces.

#! /bin/bash

dir=$1

for file in "$dir"/*.* ; do

temp=${file//" "/_}

mv "$file" "$temp"

done
Enter fullscreen mode Exit fullscreen mode

Code Explanation: This script loops through every item inside the folder, replaces spaces with underscores in the filename, and saves it to a variable called temp. Then the original file is renamed with the temp variable.

Usage:

remove_spaces </path/to/directory>
Enter fullscreen mode Exit fullscreen mode

The spaces inside the filenames shall all be replaced with underscores.

Closing Thoughts

I have created three commands above: brightness fileopen and remove_spaces with their syntax. Likewise, we can write scripts for automating anything that we regularly do. I hope the examples illustrated above inspire you to write bash scripts for your use cases, save valuable time and improve your workflow.

Happy Automating!

Top comments (0)