Batch files in linux

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

Источник

Do batch files work on linux?

Unfortunately, many of the answers such people receive state that batch scripts cannot run on Linux. However, Linux users can indeed run batch files. Also, Windows batch files can be run on Windows like a native shell script.

What is batch file in Linux?

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. . Unix-like operating systems, such as Linux, have a similar, but more flexible, type of file called a shell script.

Can you run BAT files on Ubuntu?

bat file, it will open in gedit>Tools>External Tools>Run command>in the popped up box type ‘wineconsole cmd‘ (without inverted commas)>OK. 5. A Wine console terminal will pop up, in it type ‘start yourfilename. bat’ and hit enter.

Does cmd work in Linux?

Most of us think that Linux has terminal and we can use a command-line interface only in Linux but it is just a myth. There is a PowerShell and a command prompt in windows as well where we may execute the commands easily. But Windows and Linux have commands with the same name as well.

What files run on Linux?

A RUN file is an executable file typically used to install Linux programs. It contains program data and installation instructions. RUN files are often used to distribute device drivers and software among Linux users. You can execute RUN files in the Ubuntu terminal.

How to run .bat(Batch) Files on Linux (Ubuntu)

45 related questions found

Why is permission denied Linux?

While using Linux, you may encounter the error, “permission denied”. This error occurs when the user does not have the privileges to make edits to a file. . Other users, however, may not be allowed to make such edits. Remember that only root or users with Sudo privileges can change permissions for files and folders.

Can you run EXE files on Linux?

1 Answer. This is totally normal. .exe files are Windows executables, and are not meant to be executed natively by any Linux system. However, there’s a program called Wine which allows you to run .exe files by translating Windows API calls to calls your Linux kernel can understand.

Is Windows a Linux terminal?

Windows Terminal is a modern terminal application for users of command-line tools and shells like Command Prompt, PowerShell, and Windows Subsystem for Linux (WSL).

Is Windows Terminal like Linux?

The Windows Terminal is a modern, fast, efficient, powerful and productive terminal application. It combines many command-line tools and shells such as Command Prompt, PowerShell and WSL (Windows Subsystem for Linux).

Is CMD a terminal?

So, cmd.exe is not a terminal emulator because it is a Windows application running on a Windows machine. There is no need to emulate anything. It is a shell, depending on your definition of what a shell is.

What are .sh files in Linux?

An SH file is a script programmed for bash, a type of Unix shell (Bourne-Again SHell). It contains instructions written in the Bash language and can be executed by typing text commands within the shell’s command-line interface.

What is the Linux equivalent of a batch file?

Batch file equivalent in linux is shell script (. sh). You can use gedit, vim or any other text editor available to create one.

How do I run a .sh file in Linux?

  1. Select the file using mouse.
  2. Right-click on the file.
  3. Choose Properties:
  4. Click Permissions tab.
  5. Select Allow executing file as a program:
  6. Now click the file name and you will be prompted. Select “Run in the terminal” and it will get executed in the terminal.

Источник

Читайте также:  Linux set timezone console
Оцените статью
Adblock
detector