Pinterest Stumbleupon Whatsapp
Ads by Google

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 4 Ways to Teach Yourself Terminal Commands in Linux 4 Ways to Teach Yourself Terminal Commands in Linux If you want to become a true Linux master, having some terminal knowledge is a good idea. Here methods you can use to start teaching yourself. Read More , 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?

shell_script_example
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 An A-Z of Linux - 40 Essential Commands You Should Know An A-Z of Linux - 40 Essential Commands You Should Know Linux is the oft-ignored third wheel to Windows and Mac. Yes, over the past decade, the open source operating system has gained a lot of traction, but it’s still a far cry from being considered... Read More 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?

shell_script_cron_job
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.

Ads by Google

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

shell_script_firewall_rules
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

shell_script_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

shell_script_wallpaper
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

shell_script_clean_cache
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 11 Shortcuts For Learning Linux In Record Time 11 Shortcuts For Learning Linux In Record Time If you'd like to learn Linux, but want some ways to speed up the process, here are ten shortcuts you can use to learn as fast as possible. Read More to speed up the process!

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

  1. Rick Stanley
    September 6, 2014 at 2:46 pm

    As many have already said, the .sh extension is not needed and usually not reccomended. As well the shebang, "#!/bin/bash" is not required for a bash script, but highly reccomended for all scripts, as there are several scripting languages (Bash, Perl, Python, etc...), that could be used to write a script, and and different shells (Bash, sh, csh, zsh, etc...) that could also be used to run the script in.

    You are missing one point that is important for running the script a a normal executable. After creating the script "sample", you should set the executable permission on the script:

    $ chmod +w sample

    Otherwise you would have to run it using the appropriate interpreter:

    $ bash sample

  2. donespo
    September 4, 2014 at 7:40 pm

    It's a good idea to know the ins and outs of setting up a chron text file but there's a great GUI called 'Scheduled Tasks' that makes setting up chron jobs a lot easier. Look for it in the repository of your favorite distro. About 5 years ago I had to set up a PC with about 30 different repetitive tasks that ran anywhere from weekly to several times a day depending on the job (most were report writers). I had just decided to dump windows xp for good and needed to get this done quickly. This GUI saved the day for me. Been using it ever since.

    This is from the info page in Ubuntu software center:

    GNOME GUI for configuring a users' cron (automatic jobs).
    Some of its features are:
    Templates support so that you won't have to create the same task again and again.
    If run as root, you can edit any user's crontab and "at" tasks.
    Human-readable strings like "Every hour" instead of "0 * * * *".
    Advanced mode for crontab experts.
    A calendar allows you to choose the day you want a task executed.

  3. Robert N
    September 4, 2014 at 3:54 pm

    Contrary to the article, scripts do not have to have a .sh extension, or any extension at all, for that matter. File extensions have no intrinsic meaning in Linux; it's a Windows concept, and should not be carried over to other operating systems.

    Take the .sh off, and you have a command that looks like the other system commands. This is true of Perl and Python scripts as well: leave off the .pl or .py if you'd like them to appear more like the system's distributed programs.

    Having said that, there is also no other compelling reason for not using .sh on the script, and it will tell you at a glance what is contained in the file. So it's just a matter of personal preference, but doesn't matter to Linux either way.

  4. Sudeepto D
    September 4, 2014 at 11:50 am

    @Danny Stiben: Please tell me the terminal color scheme that you are using. Also please tell me what system theme are you using ..

  5. mastaeit
    September 3, 2014 at 9:02 pm

    Once again a great article. However, I don't think that .sh is a must for the name of the script.

  6. Ed
    September 3, 2014 at 8:50 pm

    "Every scripts needs to have an .sh file extension, start with the line “#!/bin/bash”"

    This is wrong on a couple of levels. First, if the shebang is calling bash, the file extension should *not* be `.sh` which is for POSIX shell scripts. Second, the file extension is unneccessary anyway, see: http://wiki.bash-hackers.org/scripting/basics

Leave a Reply

Your email address will not be published. Required fields are marked *