Virtual Machine Directory Export

The company I work at, like many, uses Windows for our workstation PCs. I much prefer developing in Linux, and so do most of my day-to-day work in a virtual machine. (I love VirtualBox’s Seamless Window feature for this). I do my work in a VirtualBox shared folder, which means that it is actually a folder on the host OS, but can be mounted in the guest OS as well. This makes the files I work on in Linux easily accessible from Windows too.

Sometimes I’m plugging away in a Linux shell and want to do something in the same directory on the Windows host. I quickly got tired of opening explorer (the Windows file browser) and navigating down to the directory I was working in under Linux, so I came up with a reasonable 2-step process to quickly open an explorer window in the current Linux directory. (We use Windows 7 at work, but I think a similar process should work for other Windows versions).

I wrote a script in Linux that creates a Windows batch file. When passed an argument, the script causes the batch file to open Windows explorer in the directory specified by the argument. Otherwise it uses the present working directory. The batch file is created in the root of the shared folder. Each time the script is run it overwrites the old batch file so that the same file now points to a new location.

Here are the steps to make this work in your environment:

  1. Download the script and put it in a directory that is in your Linux $PATH so it is easy to use from anywhere.
  2. Edit the script to match your VirtualBox setup:
    1. change the “HOSTSHARE” variable at the top to be the shared directory path on your Windows host machine.
    2. change the “SHAREMOUNT” variable at the top to be the mountpoint for the shared directory on your Linux guest machine
  3. Make sure that the script has executable permissions:
    • chmod u+x winthis.sh
  4. Run the script once to create the batch file (linlink.bat).
  5. In Windows, open explorer and navigate to the shared folder.
  6. Create a shortcut to the batch file (right-click, “Create Shortcut”).
  7. Right-click on the shortcut and select “Properties”.
  8. The “Target” field of the “Shortcut” tab shows the path to the shortcut target. In front of this path insert the word “explorer” (no quotes) and press “OK”. (Windows 7 refuses to let you pin a file shortcut to the taskbar, so this alters it to be an application shortcut. The application shortcut runs explorer, which then runs the batch file.)
  9. Now drag the shortcut to the task bar to pin it so it is always quickly available.

Now the two-step process to open the current directory in Windows is:

  1. Type “winthis.sh” in the Linux shell
  2. Click the shortcut on the Windows toolbar to open the directory

Now you can quickly drag the file to attach it to an Outlook e-mail or whatever else you need to do with it in Windows.

Notes:

  • If you want to change the icon for the toolbar link, you need to do it before you pin it to the toolbar. After you create the shortcut, bring up the properties and click the “Change Icon…” button.
  • You can delete the original shortcut after you have dragged it to the taskbar (but not the target batch file, of course).
  • I learned about the pinnable shortcut trick from a lifehacker tip.
  • At first I thought it would be simpler to avoid the batch script and just create the link directly. However, Windows “link” files are some complicated binary format, so I quickly gave up looking into that. After completing the process, I’m not sure it would have worked anyway, as from Linux I couldn’t directly modify the shortcut that lives on the Windows toolbar.
This entry was posted in Linux, Programming, Scripting. Bookmark the permalink.