As a Linux user, you’re probably already aware of how powerful the command line can be. With a little knowledge of Bash scripting, you can automate repetitive tasks and save yourself a lot of time and effort. In this article, I’ll share some tips and tricks for using Bash to automate tasks on Linux.
What is Bash?
Bash is a command shell for Linux, which means it provides an interface for users to interact with the operating system. It’s also a scripting language, which means you can use it to write programs that automate tasks.
Bash is installed by default on most Linux systems, so you don’t need to install anything extra to start using it. You can open a Bash shell by opening a terminal window on your Linux system.
Discover: Mastering Bash: A Cheat Sheet of the Top 25 Commands and Creating Custom Commands
How to Automate Tasks with Bash
- Writing Simple Bash Scripts
Bash scripts are simple text files that contain a series of commands. You can create a Bash script using any text editor, such as Nano or Vim. Once you’ve written your script, you can execute it by running the following command in the terminal:
myscript.sh
For example, let’s say you want to automate the process of creating a new directory and navigating to it. Here’s a Bash script that does that:
#!/bin/bash
mkdir new_directory
cd new_directory
Save this script as newdir.sh
and make it executable with the following command:
bashCopy codechmod +x newdir.sh
Now you can execute the script by running:
bashCopy code./newdir.sh
- Running Scripts Automatically
If you have a script that you want to run automatically at a specific time, you can use the cron
daemon. cron
is a built-in Linux utility that allows you to schedule tasks to run at specific times.
To use cron
, you need to create a crontab
file. This file contains a list of commands that cron
will execute at specified times. You can create a crontab
file by running the following command in the terminal:
crontab -e
This will open the crontab
file in your default text editor. You can then add a new line to the file to schedule your script to run. For example, to run the newdir.sh
script every day at 3:30 PM, you can add the following line:
30 15 * * * /path/to/newdir.sh
- Using Variables and Conditional Statements
Bash scripts can also use variables and conditional statements to make decisions based on the current state of the system. For example, let’s say you want to automate the process of checking whether a specific process is running. Here’s a Bash script that does that:
#!/bin/bash
PROCESS_NAME="myprocess"
if pgrep "$PROCESS_NAME" > /dev/null
then
echo "$PROCESS_NAME is running"
else
echo "$PROCESS_NAME is not running"
fi
This script uses the pgrep
command to check whether the process with the name myprocess
is running. If it is, the script prints a message saying so. If it’s not, the script prints a message saying the process is not running.
Linux for command: Automate tasks on a files
If you have a bunch of files to work on at once, and you need to do the same thing with every file, use the for
command. This command iterates across a list of files, and executes one or more commands. The for
command looks like this:
for variable in list
do
commands
done
I’ve added some extra spacing in there to help separate the different parts of the for
command. That multi-line command might look difficult to run on the command line, but you can use ;
to put everything on one line, like this:
for variable in list ; do commands ; done
Let’s see it in action. One way I use the for
command is to rename a bunch of files. Most recently, I had a bunch of screenshots that I wanted to rename. The screenshots had names like filemgr.png
or terminal.png
and I wanted to put screenshot
before each name instead. I ran a single for
command to rename thirty files at once. Here’s an example with just two files:
$ ls
filemgr.png terminal.png
$ for f in *.png ; do mv $f screenshot-$f ; done
$ ls
screenshot-filemgr.png screenshot-terminal.png
The for
command makes it easy to perform one or more actions on a set of files. You can use a variable name that is meaningful to you, such as image
or screenshot
, or you can use a “shorthand” variable like f
, as I did in my example. When I write scripts that use a for
loop, I try to use meaningful variable names. But when I’m using for
on the command line, I’ll usually use a short variable name like f
for files or d
for directories.
Whatever name you choose for your variable, be sure to reference the variable using $
in the command. This expands the variable to the name of the file you are acting on. Type help for
at your Bash prompt to learn more about the for
command.
Discover: Bash scripting tips that can save time on Linux
Linux conditional execution (if)
Looping across a set of files with for
is helpful when you need to do the same thing with every file. But what if you need to do something different for certain files? For that, you need conditional execution with the if
statement. The if
statement looks like this:
if test
then
commands
fi
You can also do if/else tests by using the else
keyword:
if test
then
commands
else
commands
fi
For more complicated processing, you can use if/else-if/else evaluations. I might use this in a script, when I need to automate a job to process a collection of files at once:
if test
then
commands
elif test2
then
commands
elif test3
then
commands
else
commands
fi
The if
command allows you to perform many different tests, such as if a file is really a file, or if a file is empty (zero size). Type help test
at your Bash prompt to see the different kinds of tests you can use in an if
statement.
For example, let’s say I wanted to clean up a log directory that had several dozen files in it. A common task in log management is to delete any empty logs, and compress the other logs. The easiest way to tackle this is to just delete the empty files. There isn’t an if
test that exactly matches that, but we have -s
file to test if something is a file, and if the file is not empty (it has a size). That’s the opposite of what we want, but we can negate the test with !
to see if something is not a file or is empty.
Discover: Chronological list of resources to learn Bash from complete beginner to advanced level
Let’s look at an example to see this at work. I’ve created two test files: one is empty, and the other contains some data. We can use if
to print the message “empty” if the file is empty:
$ ls
datafile emptyfile
$ if [ ! -s datafile ] ; then echo "empty" ; fi
$ if [ ! -s emptyfile ] ; then echo "empty" ; fi
empty
We can combine this with for to examine a list of log files to delete the empty files for us:
$ ls -l
total 20
-rw-rw-r--. 1 jhall jhall 2 Jul 1 01:02 log.1
-rw-rw-r--. 1 jhall jhall 2 Jul 2 01:02 log.2
-rw-rw-r--. 1 jhall jhall 2 Jul 3 01:02 log.3
-rw-rw-r--. 1 jhall jhall 0 Jul 4 01:02 log.4
-rw-rw-r--. 1 jhall jhall 2 Jul 5 01:02 log.5
-rw-rw-r--. 1 jhall jhall 0 Jul 6 01:02 log.6
-rw-rw-r--. 1 jhall jhall 2 Jul 7 01:02 log.7
$ for f in log.* ; do if [ ! -s $f ] ; then rm -v $f ; fi ; done
removed 'log.4'
removed 'log.6'
$ ls -l
total 20
-rw-rw-r--. 1 jhall jhall 2 Jul 1 01:02 log.1
-rw-rw-r--. 1 jhall jhall 2 Jul 2 01:02 log.2
-rw-rw-r--. 1 jhall jhall 2 Jul 3 01:02 log.3
-rw-rw-r--. 1 jhall jhall 2 Jul 5 01:02 log.5
-rw-rw-r--. 1 jhall jhall 2 Jul 7 01:02 log.7
Using the if
command can add some intelligence to scripts, to perform actions only when needed. I often use if
in scripts when I need to test if a file does or does not exist on my system, or if the entry the script is examining is a file or directory. Using if
allows my script to take different actions as needed.
FAQ
- Q: What is Linux?
Linux is an open-source operating system that is widely used in servers, mainframes, supercomputers, and embedded systems. It was created by Linus Torvalds in 1991 and is based on the Unix operating system.
- Q: What is Bash?
Bash is a Unix shell and command language that is used in Linux and other Unix-based operating systems. It provides a command-line interface for users to interact with the system and execute commands.
- Q: What are some common tasks that can be automated using Linux commands?
Some common tasks that can be automated using Linux commands include file manipulation (moving, copying, deleting), file searching and filtering, process management, system monitoring, and network management.
- Q: What are some advantages of using Linux for automating tasks?
Linux provides a wide range of powerful and flexible command-line tools that can be used to automate tasks. It is also open-source, free, and highly customizable, making it an ideal choice for developers and system administrators who need to automate complex tasks.
- Q: Is Linux difficult to learn?
Linux can be challenging for beginners who are not familiar with the command-line interface, but there are many resources available to help you learn. With practice and patience, you can become proficient in using Linux commands and automating tasks.
Conclusion
Bash is a powerful tool for automating tasks on Linux. With a little knowledge of Bash scripting, you can save yourself a lot of time and effort. Whether you’re automating simple tasks or complex workflows, Bash can help you get the job done faster and more efficiently.