Linux version of a .cmd file
Categories:
Migrating .cmd Files to Linux: A Comprehensive Guide

Learn how to convert and adapt Windows batch (.cmd) files for execution on Linux and Ubuntu systems, covering shell scripting fundamentals and common command equivalents.
Windows batch files, typically ending with the .cmd
or .bat
extension, are powerful tools for automating tasks on Windows operating systems. However, they are inherently incompatible with Linux environments. When migrating applications or workflows from Windows to Linux (such as Ubuntu), you'll often encounter the need to translate these batch scripts into a Linux-compatible format, most commonly shell scripts (e.g., Bash scripts).
Understanding the Core Differences
The fundamental difference between Windows batch files and Linux shell scripts lies in their command interpreters and syntax. Windows uses cmd.exe
(or PowerShell), while Linux primarily uses Bash (Bourne Again SHell) or other shells like Zsh, Ksh, etc. This means that not only do the commands themselves differ, but also the way variables are handled, conditional logic is expressed, and loops are constructed.
flowchart TD A[Windows .cmd File] --> B{Identify Script Purpose} B --> C{Break Down into Individual Commands} C --> D{Find Linux Equivalents} D --> E{Adapt Syntax & Logic} E --> F[Linux Shell Script (.sh)] F --> G{Test & Debug} G --> H[Deployment]
Workflow for migrating a .cmd file to a Linux shell script.
Common Command Equivalents
Many Windows commands have direct or similar counterparts in Linux. Understanding these equivalences is crucial for a smooth migration. Below is a table of frequently used commands and their Linux alternatives.
File Operations
Windows Command | Linux Equivalent | Description |
---|---|---|
dir | ls | List directory contents |
cd | cd | Change directory |
copy | cp | Copy files |
move | mv | Move/rename files |
del | rm | Delete files |
mkdir | mkdir | Create directory |
rmdir | rmdir | Remove empty directory |
type | cat | Display file content |
echo | echo | Print text to console |
findstr | grep | Search for text in files |
System Commands
Windows Command | Linux Equivalent | Description |
---|---|---|
set | export (for env vars), VAR=value | Set environment variables |
pause | read -p "Press Enter to continue..." | Pause script execution |
start | nohup command & or command & | Run command in background |
taskkill | kill | Terminate processes |
ipconfig | ip a or ifconfig | Display network configuration |
Control Flow
Windows Command | Linux Equivalent | Description |
---|---|---|
if | if | Conditional execution |
for | for | Loop through items |
goto | Functions or while loops | Jump to a label (less common in shell) |
call | Functions or source | Execute another script/function |
\
) and drive letters (C:
), while Linux uses forward slashes (/
) and a unified file system hierarchy starting from the root (/
). Always convert paths accordingly.Translating Script Logic and Variables
Beyond direct command translation, the logic and variable handling in batch files need careful conversion. Windows batch uses %VAR%
for variables, while Bash uses $VAR
or ${VAR}
. Conditional statements (if
) and loops (for
) also have distinct syntaxes.
@echo off
SET MY_VAR=Hello World
IF EXIST "C:\Program Files\My App" (
ECHO App directory exists.
) ELSE (
ECHO App directory does not exist.
)
FOR %%f IN (*.txt) DO (
ECHO Processing %%f
)
#!/bin/bash
MY_VAR="Hello World"
APP_DIR="/opt/my_app"
if [ -d "$APP_DIR" ]; then
echo "App directory exists."
else
echo "App directory does not exist."
fi
for f in *.txt; do
echo "Processing $f"
done
"$MY_VAR"
) to prevent word splitting and globbing issues.Executing Your Linux Shell Script
Once you've translated your .cmd
file into a .sh
script, you need to make it executable and run it on your Linux system.
1. Save the script
Save your translated script with a .sh
extension (e.g., myscript.sh
). It's good practice to include the shebang line #!/bin/bash
at the very beginning to specify the interpreter.
2. Make it executable
Open a terminal and navigate to the directory where you saved the script. Use the chmod
command to grant execute permissions: chmod +x myscript.sh
.
3. Run the script
Execute the script using ./myscript.sh
. If it's in your PATH, you can just type myscript.sh
.