5 Beginner Linux Setup Ideas For Cron Jobs & Shell Scripts

One of the best productivity features of Linux is bash scripting. With it, you can do a complex series of tasks in one quick go so it’s great for elaborate and repetitive needs. Bash scripts and cron jobs are also a great way to get to know terminal commands, as both make use of terminal commands that get used repeatedly.

Interested in using fun tools to learn terminal commands? Following are are five ideas for shell scripts and cron jobs to get you started. But first, a little introduction.

What Are Shell Scripts?

Like I mentioned earlier, bash scripts (also called shell scripts) are simply lists of commands that are executed in order. They’re usually created to bring together a collection of commands that need to be run in order to complete a certain task (whatever you need your computer to do for you). They’re then useful because you just have to work hard once to write all the commands, and then it does the work much faster every time you use it. You can use the script however many times you need to, so it just makes life a lot easier.

All you need is a bit of knowledge of the bash scripting language (which is all about controlling the flow of the script, including loops, variables, and so on) and a good feel for various Linux commands. For example, these 40 essential commands should definitely be known in order to write effective scripts. If you don’t know all of this yet, don’t worry! That’s why you’re doing this — the best way to learn this is to dive in head-first.

When creating a bash script, there are a few things you need to know. Every scripts needs to have an .sh file extension, start with the line “#!/bin/bash”, and comments can be made on a per-line basis with a #. Each new line is also a new command, and $1, $2, $3… are all parameters. You can use however many you need.

What Are Cron Jobs?

Cron jobs are simply bash scripts that run when you boot up your computer and when certain time conditions have been met. For them, you write your script, save it, and then run a command to add a new cron job that points to the location of the newly-saved script.

Once you’ve created the script, you’ll need to create a .txt file that uses the following format: 0-60 <minutes>, 0-23 <hours>, 1-31 <days>, 1-12 <months>, 0-7 <days of week with both 0 and 7 meaning Sunday>, and then the path of the script. For each position where the value doesn’t matter (such as the day of the week), you can just replace the number with an asterisk. Then run crontab /path/to/file.txt, obviously replacing the path with the actual one. You’ve now set up a cron job!

Script Ideas

Now that you know what a bash script and cron job are, here are some ideas you can try to implement yourself. For all of these ideas, I’m sure you’ll be able to find code that does exactly these things, but where’s the fun in that?

Batch Renaming

Let’s say you have a bunch of pictures in a folder, but they all have very strange names. Instead of keeping those unhelpful names, you could instead create a script that will take the name of the folder and count incrementally in order to create new names for all of those images. It might not be the most useful thing in the world, but it’s a great start in practicing your bash scripting skills.

Firewall Rules Switcher

If you’re an online gamer, chances are that you’ll need to keep some ports open for the games to work or perform as well as they should. Since you should try to keep a tight firewall by default, you may want to create two scripts — one that can open up the ports needed for gaming, and another one to close them back up. That way, you can enjoy your games when you’re playing and have a secure working environment when you’re not.

Batch Image Resizing

As MakeUseOf’s newsletter editor, I constantly have to get images and resize them for use in the newsletter. Creating a script that can resize a batch of images in one go is extremely useful to save time and energy. If you have a similar need, make yourself a script that can do this!

Automatic Wallpaper Rotation

Don’t like staring at the same wallpaper every day? You could make a script that can pick an image at random from a folder and apply it as the wallpaper. You can then just use it as a script to run it on demand, or you can add it as a cron job so it’ll set a new wallpaper every day.

Automatic Removal Of Trash, Caches, and More

Another great cron job to create is one that can empty out the trash and any other locations that may contain temporary or junk files (such as your browser’s cache). While it wouldn’t be able to run right before you shut down, it could be set up to run as soon as you turn on your computer, which in the long-run achieves the same exact thing. Making it a cron job will let you “set it and forget it” while keeping those locations from taking up too much of your storage space.

The one tip I can provide here is to make sure that you’re only deleting the contents of the folders in question, and not the folders themselves. rm -rf /path/to/folder is different to rm -rf /path/to/folder/*.

Get Scripting

These five scripting ideas should give you a head start in creating bash scripts on Linux. I know that a few of these ideas may seem a little difficult, and I really wanted to add some code to help you guys out, but I think it’s best if you learn on your own. The Internet is a fantastic resource for scripting, so I’m sure you’ll find answers very quickly. Once you’ve gotten a taste of what it’s like to do scripting, there’s no limit to what it can do for you.

Need more ways to learn about Linux? Check out these 11 shortcuts to speed up the process!

What are some of the best scripts you’ve written? Let us know in the comments!

Wes Hopkins Authentic Jersey C. J. Prosise Jersey