How to create batch files on Windows & Linux to automate small and complex tasks

While working on our computers we sometimes need to do repetitive tasks almost every single day. However, there is a simple way you can automate those tasks by creating batch files. A batch file is a file, which will contain the instructions that need to be executed and you can simply execute the same to get your work done. That is the simplest way you can use a batch file, however, there are even some other ways batch files can be important to you. Depending upon which system or platform you are using, creating and executing a batch file in the subject to vary and if you are using Linux operating system you might already know about these batch files, as most Linux users need to work with batch files quite some time.

In order to create a simplest batch file, you have to open a text editor, enter all the commands save it with the correct extension. After you are done creating the batch file, you can simply execute it and get your work done in no time. Well, lot of manual what is sorted out with batch files. So today, I will be explaining how you can create a batch file on Windows and Linux computers for your everyday tasks and also to open multiple applications at the same time and a number of other purposes. The process of creating batch files is different for Windows and Linux is different. So, I will be explaining the method to create a batch file and execute them one by one.

Create batch files on Windows

  • To create a batch file on Windows, simply open Notepad, Notepad++, or any other text editing app that you use for all your requirements.
  • Now, you can write the commands that you need to execute one by one in separate lines.
  • Here I am creating a batch file, which will create a new folder corresponding to the current date (not the exact date) in the ‘Daily_work’ folder on C drive of your Windows computer.
c: (Move to C drive)
cd\ (Go to the root of C drive, if the working directory is set to something else in the same drive)
cd Daily_work (Navigate to the ‘Daily_work’ folder)
mkdir %date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4% (Create the directory with the date)
Batch files on Windows and Linux 10
  • After you are done writing all the commands, you have to save the text file with the extension ‘.bat’. I am saving the file with the name ‘daily_folder.bat’.
 save the text file with the extension ‘.bat’.
  • Now, it’s time to execute the batch file by double-clicking on it, or by choosing ‘Open’ from the right-click context menu. A Command Prompt window will appear and disappear really fast if things work correctly. If it stops in the middle, there must be something wrong in the batch file. A new folder will be created in the directory that has the current date as its name after the execution is complete.
execute the batch file by double-clicking
  • As the batch file will be created in the current working directory you can even execute the batch file by navigating to the directory on command prompt and by typing in in the name of the batch file along with the extension, which is ‘.bat’.
Batch files on Windows
  • Depending upon your requirements, you can add a number of other commands and all those commands will be executed automatically on running the batch file created by you. You can display text on the screen with the ‘ECHO’ command, show a specific title in the command prompt window with the ‘TITLE’ command, or even pause the execution of the commands.
  • There are a number of ways, a batch file can be beneficial for you to make your everyday work, a lot easier.

Create batch files on Linux

Now, let’s find out how you can create a batch file and execute the same on a Linux computer.   As the commands on Linux are different from that on Windows and Command Prompt, the overall batch files will also look different in the case of Linux.

  • To create a batch file on Linux simply open your favourite text editor on Linux or use the ‘gedit’ command to use Gedit to create a new text file on your Linux computer. You can even create a batch file using ‘nano’ or ‘vi’.
  • Now, type in all the commands that correspond to your everyday work. 
  • Once you have prepared your batch file, you will have to save the file with any name, but with the extension ‘.sh’ at the end of the file name.
  • Let’s use a batch file that will create a directory with the date as the name, just the same that we did in the case of Windows.

mkdir ~/Documents/Daily_work/”$(date +”%d-%m-%Y”)”

Batch files on Windows and Linux 60
  • Now that you have written it, save it as any name, which ‘daily_bat.sh’ for me. Make sure, you keep ‘.sh’ in the end.
Batch files on Windows and Linux 70
  • Now that the file is created you will have to make the batch file executable by typing in the following command on a Linux terminal.
chmod +x filename.sh 
  • The name of the batch file for me is ‘daily_bat.sh’, and thus, my command will be as shown below.
chmod +x daily_bat.sh
Batch files on Windows and Linux 75
  • Now that the batch file has been made executable, you can execute the content of the batch file by typing in the following command.
./filename.sh
  • For me, the command will go as follows.
./daily_bat.sh
  • Here, the ‘.’ Correspond to the active directory. You can even replace the dot ‘.’  with the path to the batch file. 
Batch files on Windows and Linux 80
  • Now, the execution will start.
  • You can also execute the same directly from the graphical user interface, without touching the terminal at all.
  • Open your File Manager on Linux, which is the default Ubuntu file manager for me, and open ‘Preferences’ from the drop-down menu in the taskbar. However, it can be different, depending upon the file manager that you are actually using.
File Manager on Linux
  • Open the ‘Behavior’ tab, and mark the option that says, ‘Run them’, corresponding to ‘Executable Text Files’.
Executable Text Files
  • Now exit the window.
  • After this, you can simply double click on the batch file, or the file, and the tasks within the batch or executable text file will start getting executed right away.

Creating batch files can surely come in handy in multiple situations, and you might encounter executable text files on Linux while installing some programs, or at the time of running them.

So that was all about how you can automate different small tasks, if not big ones, with batch files on Windows and Linux. Do you have any questions? Feel free to comment on the same below.