Skip to content

Instantly share code, notes, and snippets.

@santisbon
Last active March 16, 2023 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santisbon/332956c4ab21d51990ba5c7e970721ab to your computer and use it in GitHub Desktop.
Save santisbon/332956c4ab21d51990ba5c7e970721ab to your computer and use it in GitHub Desktop.
#Linux utilities on #Windows.

Using Linux utilities on Windows

If you use a Windows computer at work you may feel that you're missing out on some really useful functionality that you're used to having on a UNIX system. Or perhaps you've always used Windows and have struggled with messy workarounds for something that would be easier with Linux commands.

If you're on an older version of Windows that doesn't have the Windows Subsystem for Linux (WSL) or you don't want to install a Linux distribution on your machine, you can still use Linux utilities to quickly get things done. At the end we'll see some notes about doing the same thing on WSL.

This brief guide will help you with an example of a common task: finding out which files have been modified on your computer (or a remote server) within a specified time frame.

First you'll need to install Cygwin. This program will let you use tools similar to a Linux distribution on Windows. At some point during the installation you'll be asked to select the tools you need. The default choice is a decent collection but I like to select a few additional ones that I use often: nano, wget, curl, and dos2unix.

After the installation is done and you open Cygwin you'll have a bash interpreter ready for use. The root of the directory structure points to the installation folder e.g. C:\cygwin64 so /home points to C:\cygwin64\home. Your Windows drives are accessible at /cygdrive e.g. your D: drive is at /cygdrive/d.

I like to personalize a few aspects of this interface. For example, open your configuration file located in your home directory with the nano text editor:

nano ~/.bashrc

Add anything you'd like to customize your shell. For example, have it always start at the home folder of your Windows user (C:\Users\your_user_name)

cd /cygdrive/c/Users/your_user_name

Save the file and exit.

Now the fun stuff. Let's find all the files that were modified within the last day on the C: drive of a server on the network. For each file we want to list its properties (like last modified time, permissions, etc), and finally redirect the output to a file on our computer.

You can use a UNC path with // just like you would use \\ on Windows. To find files by modification time use the -mtime option followed by the number of days to look for. The number can be a positive or negative value. A negative value equates to less than so -1 will find files modified within the last day. Similarly, +1 will find files modified more than one day ago.

find //server_name/c$ -type f -mtime -1 -exec ls -la {} \; > results.txt 

After that you might want to change new lines to Windows format in the results file.

unix2dos results.txt

Feel free to experiment with other Linux tools. Maybe you want to change all spaces and tabs to commas and save the result as a csv file:

cat results.txt | sed 's/[ \t]/,/g' > results.csv 

Maybe you'll then want to copy these results to a shared location on a server:

cp results.csv //server_name/folder_name/results.csv 

There are countless tasks that are made easier with a powerfull set of Linux command line utilities and you can save a lot of time with the right tools.

Windows Subsystem for Linux

A quick note on WSL. To access the remote location as a UNC path you'll need to mount it using the DrvFs filesystem on a path of your choice like /mnt.

sudo mkdir /mnt/share
sudo mount -t drvfs '\\server\share' /mnt/share

When you're done you can unmount the filesystem if you no longer need it.

sudo umount /mnt/share

If it's not taking the credentials of your user or you need to specify other credentials you can navigate to the share location using Windows File Explorer and WSL will inherit the credentials you supply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment