Use Linux commands on Windows

Unleash the Power of Linux on Your Windows Laptop: A Guide for Non-Admin User

Have you ever found yourself on a locked-down work laptop, frustrated by the limitations of the Windows Command Prompt or PowerShell? Perhaps you’re a developer or an engineer who is used to the power and flexibility of Bash scripting and Linux commands for automating tasks, but you don't have the administrative rights to install the necessary software.

This is a common predicament, but it has a surprisingly simple solution. You can bring a fully-functional Linux environment to your Windows machine without a single installation, thanks to portable applications like MobaXterm and Git Bash.

Why Bother with Bash and Linux Commands?

For a Windows user, the idea of using a different command line environment might seem daunting. However, once you experience it, you'll understand why it's so popular:

Automation: Bash scripts are incredibly powerful for automating repetitive tasks. From file management and data processing to running complex workflows, a few lines of code can save you hours of manual work.

Powerful Tools: Linux provides a suite of robust command-line tools for text and file manipulation, such as grep (for searching), sed (for editing streams), awk (for pattern scanning), and find (for powerful file searches).

Consistency: If you work with Linux servers, cloud environments, or other developers, using a Bash shell on your local machine creates a consistent workflow, eliminating the need to constantly switch your mindset between different command syntaxes.

Your Portable Allies: MobaXterm and Git Bash

Git Bash

Most developers need Git for version control. Git for Windows includes a program called Git Bash, which provides a full-featured Bash environment.

How to get it:

  1. Go to the official Git for Windows website.

  2. Instead of the standard installer, look for the "Portable" version on the downloads page.

  3. Download and extract the ZIP file to any folder you like.
  4. Run the git-bash.exe file, and you'll have a new terminal window ready to go.

MobaXterm

Think of MobaXterm as a complete "Swiss Army knife" for remote computing. While it's most famous for its advanced SSH client, its core feature is a powerful, built-in Bash shell that gives you access to a huge set of Linux commands.

How to get it:

  1. Navigate to the MobaXterm website.
  2. Click on "Download" and select the "Home Edition" and then choose the "Portable Edition" ZIP file.
  3. Extract the ZIP file and run MobaXterm.exe. That's it!

Practical Example: Automatically Deleting Old Log Files

Let's put this into practice with a common task: cleaning up a folder cluttered with old log files. Manually doing this is a pain, but a simple script makes it a breeze.

The Problem

Imagine a folder that's filled with hundreds of .log files, and you need to delete any file older than 30 days to free up disk space.

The Bash Script

Save the following code in a file named cleanup.sh and place it in the same directory as your log files.

#!/bin/bash

# Define the directory to clean up
LOG_DIR="."

# Define the number of days after which files should be deleted
DAYS_OLD=30

echo "Starting cleanup of old log files..."
echo "Looking for files older than $DAYS_OLD days in $LOG_DIR"

# The find command is the core of this script.
# -type f: searches for files (not directories)
# -name "*.log": only matches files ending with .log
# -mtime +$DAYS_OLD: finds files modified more than 30 days ago
# -delete: deletes the found files
find "$LOG_DIR" -type f -name "*.log" -mtime +$DAYS_OLD -delete

echo "Cleanup complete!"

How to Run the Script

Using Git Bash or MobaXterm:

  1. Open your Git Bash or MobaXterm portable executable.
  2. Navigate to the folder where you saved cleanup.sh using the cd command. For example: cd /c/Users/YourName/Documents/MyLogs.
  3. Make the script executable: chmod +x cleanup.sh. This is a one-time step.
  4. Run the script by typing its name: ./cleanup.sh.

In an instant, your script will find and remove all the old log files, and you'll get a confirmation message.

By using these portable applications, you can take control of your command-line environment and automate tasks on any Windows machine, completely bypassing the need for administrative privileges. It’s a powerful skill that can significantly boost your productivity.

How is it useful to CAE Engineer

As a CAE (Computer-Aided Engineering) engineer, a typical day involves running complex simulations using software like LS-DYNA, ANSA, or HyperMesh. These simulations generate numerous output files such as: .op2 (Nastran results), .f06 (Nastran output), .pch (punch file), .scr (scratch files), .scratch (scratch data), .obd (Abaqus results), .dat (Abaqus files), .hm (HyperMesh model), .ansa (Ansa model).

Over time, these files accumulate and consume significant storage space. Efficient file management is crucial to maintaining smooth workflows and optimizing computational resources.

Routine Task: Cleaning Up Old Simulation Files

  • Regularly identify and delete old or redundant files (e.g., from completed or obsolete simulation runs). -Prioritize removal of large temporary files like .scratch and old result files (.op2, .f06).
  • Archive important models and results before deletion.
  • Use automated scripts or CAE management tools to batch delete old files safely.
  • Ensure backup policies are followed before discarding any files.

This routine helps keep the workstation or server clean, improves data accessibility, and prevents storage bottlenecks, which is vital for efficient CAE operations.

So, to summarize, a CAE engineer frequently deletes old simulation and intermediate files such as op2, f06, pch, scr, scratch, obd, dat, hm, and ansa files to optimize storage, maintain system efficiency, and ensure seamless simulation workflows.

Feel free to modify the script to match your specific workflow! If you need support for making customized script as per your requirement, please connect me on email.