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.
- A custom command to control the brightness of the screen
- A script to open any file in any folder by typing only a part of the filename, and
- 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
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)
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}
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
Now, type in the command
:~$ brightness
and press ENTER.
Open Sesame! The screen dims.
When we ENTER,
:~$ brightness 1
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
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}" "{}" &
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)>
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
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>
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)