Categories
Command Line (CLI) Using Linux

Cygwin rename command help

Rename files in cygwin, overcoming a limitation

Cygwin is a Linux-like environment for Windows.  It comes in handy when you’re at work, or at home, and your only operating system happens to be Microsoft Windows.  Cygwin allows you to issue Linux commands in a console in Microsoft Windows.  I find it easier to use than the DOS CMD prompt when I’m using a Windows OS.  It’s a great way to get things done quicker, and can be scripted—in a similar environment to a Linux Terminal.

So, let’s say you’re seasoned in the Linux shell.  You’re now at work, using Cygwin, and you want to rename a lot of files based on their name matching a pattern of letters or numbers or both.  This is where cygwin has some trouble.

By default, cygwin does not come with rename ability.  The package util-linux is required.

Cygwin’s rename command renames files if you tell it the exact name of your target file.  This is the same as the mv command. I’m no Cygwin expert, but this is the experience I’ve had.

In Linux, you can rename files using wildcards or pattern matches.  Cygwin’s rename command cannot seem to handle regexes in the same way.  I’ve tried a fair amount of googling to see why this command has limited behavior in cygwin.  I haven’t found a usable solution or add-on to cygwin to overcome this limitation.

So, here’s a shell script that uses sed, mv, and find to overcome the limitation.  I was unable to attach it, so I copied and pasted it below.

Please understand and accept the following disclaimer:  YOU MAY USE & MODIFY THE SCRIPT.  AUTHOR ASSUMES ZERO LIABILITY.  SCRIPT INTENDED FOR USE IN CYGWIN WITHIN A WINDOWS OS. SCRIPT COMES WITH NO WARRANTY, USE AT YOUR OWN RISK. Thanks.

#!/bin/sh
# cygrename : A Program to overcome limitation of cygwin's "rename" function.
# Author: Adam Teller
#
# Cygwin's "rename" doesn't work same way as *Nix "rename"
# because it cannot use wildcards. So, what can we do?
## Get the basename of the file if it matches search pattern.
## If 4th argument is set, change file to that suffix.
## else, match pattern within file name and keep same suffix
#
## Arguments: [search pattern] [replace pattern] [in suffix] [to new suffix]

LIST1=$(find . -type f -name "*${1}*\.${3}")
if [ -z "${LIST1}" ];
 then
   printf "Did not find any file matches, program will exit\n"
 exit 1
fi

for found in $LIST1;
 do
   MATCHEDFILE=$(basename $found);
   NEWFILENAME=$(echo $MATCHEDFILE | sed -e "s/${1}/${2}/");
   NEWFILENAMENOEXTENSION=$(echo $NEWFILENAME | sed -r "s/\.${3}//");

   ## TEST if $4 has been set, it means want a new file suffix
   if [ -z "${4}" ];
     then
       printf "$MATCHEDFILE :to be renamed as: $NEWFILENAME for files of type ${3}\n";
       mv $MATCHEDFILE $NEWFILENAME;
     else
       printf "${NEWFILENAMENOEXTENSION}.${3} will get $4 as its suffix, ";
       printf "and be renamed to ${NEWFILENAMENOEXTENSION}.${4} \n";
       mv $MATCHEDFILE "${NEWFILENAMENOEXTENSION}.${4}"
  fi
 done

 exit 0

Ok, I’ve copied the script and saved it, how do I use it?

  • Run your script from the same directory that has the files that you want to rename. I’ve named my copy “cygrename” (short for “cygwin rename”) but you may save your file with another name if you wish.  Make sure your script is executable. Use the cd command to get to the correct directory.
  • It accepts 3 or 4 command line arguments.
  • Invoke it in the shell using sh cygrename “what-to-find” “replace-it-with” “file extension” “new file extension” (optional).
  • The “file extension” argument does not include the dot (.), just use letters such as txt or doc or gif.
  • 3 arguments is the minimum and I’ve omitted the argument counting, you may however wish to add it into your program so that the 3 argument minimum is enforced.
  • In the even that the program does not find a match, the program exits.
  • The script uses the find command to look for files matching your pattern.  The result of a “find” usually includes the path information.  This is why we use the basename directive.  We just want the actual file name.  For example, a call to find might result in “./file1.pdf” and what we really want to use is “file1.pdf”
  • If the optional 4th argument is used, it indicates a desire to change the file suffix.  Otherwise, you can rename files and keep the file suffix as is.

Here’s a way to store & use the script.  This script is yours to use & modify as you wish.

  1. Store the script (assuming you’ve named it cygrename) in the “bin” folder in your user’s home folder.  In a windows environment, this is usually C:\cygwin\home\userName\bin.  When you first see this folder, it should be empty.  There is a cygwin “system” bin that will be full of executable files. THIS IS NOT what you’re looking for. You are looking for your user’s bin folder, which will be empty until you start putting files into it.
  2. Once you’ve used the cd command to get to the folder where the target files reside, you can invoke the script.  The script can be more easily invoked if you change the PATH variable in Cygwin.  Find instructions here.  Adding your user’s bin directory to your search PATH is recommended because it will allow you to call the script just by its filename, and won’t require the entire path name of the script.
  3. In this example, for simplicity, I’m assuming you have several files in the same “bin” directory as the script you have saved.  You have 4 image files named “picture1.jpg” “picture2.jpg” “picture3.jpg” and “picture4.jpg” .  You want to change “picture” to “img” and keep the files as *jpg  -From within the “bin”directory, Call the script as sh cygrename “picture” “img” “jpg”
  4. Your 4 jpg files should now be named “img1.jpg”, “img2.jpg” and etc.
  5. If (based on previous example) you wanted to change some web files (html) AND their file suffix, you could call the script as sh cygrename “website” “site” “htm” “html” and it would rename files such as “websiteLocation.htm” to “siteLocation.html” because you used the optional 4th argument.
  6. Please be aware that simply changing a file extension DOES NOT CONVERT a file.  What I mean is that if you change a file suffix of “img2.jpg” to “img2.gif” you are not converting to the gif file type.  The file becomes a jpg file named with an incorrect suffix.  This is not recommended.  However, changing an extension from .htm to .html is ok because it is a text-based file.  Image files, office suite files, and multimedia files should only have their suffix changed by using another application to convert the files.
Categories
Command Line (CLI) Learning Linux Using Linux

Using Linux at Work

Use Linux at work—even if your PC runs Windows.

When I started using Linux, I wondered when I would get to actually use it “on the job”.  It wasn’t easy sometimes to work with a Windows-based computer—when there’s a skill set available that can help you do things faster on the computer.  This skill set is the Linux command shell, but as mentioned your computer runs Microsoft Windows.  What can you do??

At one job, I was able to install Perl.  At another job one had to submit a request (with business justification) to add software to a workstation.  Instead of Perl I thought “why not Python?”  Since both times it was a Windows XP Environment, I used the DOS command line (or “CMD” app) to execute the Perl or Python programs.

I used the DOS CMD because (at that time) I did not know about Cygwin.  Using the DOS CMD to execute programs is easy.  The difficult part is dealing with case-insensitivity (which hurts portability) and using a shell to navigate folders with spaces in the names is a real pain.

Using the DOS shell to execute programs is not the same as using Cygwin.  Cygwin is command shell that emulates a working Linux environment.  Installing Cygwin with the base packages is simple and will provide a good “starting point” for learning some shell commands.

Want to use Linux but your work computer is Mac OS-X?

OS-X (like Linux) is Unix-based.  It has a shell that’s known as “Terminal” and it is in the Applications/Utilities folder.  You can also (if the OS is 10.5 or later) use keystrokes (apple + Space bar) to show the “spotlight search” box at the top right and start typing Terminal.  When search finds “Terminal” then press ENTER and voila!  Say hello to the Darwin Terminal.

Darwin handles the basic stuff really well.  It’s capable of helping you learn Shell commands.  But it doesn’t do everything that a regular Linux distro does.  Darwin’s limits can be overcome by installing apps and libraries.  Most likely you will need your System Administrator’s help to get and install X11, MacPorts, or Fink to augment the BSD-derived Darwin environment.

Categories
Applications Command Line (CLI) Introduction Your Choice

Favorite CLI Linux Apps: Intro

We’re talking about the Linux command line…again.  Don’t worry, I won’t bore you (hopefully). If you’re still on this page, I commend you.

Remember, a Linux user should never feel forced to use the terminal or “console”, but knowing how to use it will help you get the most out of Linux.  The speed, simplicity, and the consistency are what drive me to use it nearly every time I’m at a Linux Desktop.

Ok, here’s a list of programs that are typically not in a distro by default.  These programs are intentionally run from a terminal prompt.

Categories
Applications Command Line (CLI) Your Choice

Favorite Linux CLI Apps: Emacs

Emacs text editor (also known as emacs21-nox*)

[*The GNU Emacs editor without X support] is not usually part of a distro install—but it should be.  When you need to quickly edit a text or config file, a shell command can open the file, let you save changes, and then return you to the shell prompt.  Default console-based text editors (pre installed) are vi or nano.  I’ve tried them both.  I like emacs better.  At the time of writing, Synaptic in Crunchbang Linux 9.04 shows the console emacs as “emacs21-nox”

There are split camps and heavy debates as to which is better.  Just google  “vi vs emacs”.  It is useless for me to jump on the debate bandwagon.  Just know that they are similar, but operate differently.  Both edit and save text in a console/terminal.  Both offer no formatting or styling like a GUI word processor.  However, vi is a “modal editor” meaning that you switch back and forth between “insert mode” (for typing) and “command mode” (to issue file, search, or text-related commands.  Emacs is all one mode, where keystroke combos invoke commands.

Forget the evangelism or the “he said/she said bull$h1t”.  Try them both and see what you like.  You can always uninstall an unwanted application program with Synaptic.

Categories
Applications Command Line (CLI) Using Linux Your Choice

Favorite CLI Linux Apps: Guake

Guake and Yakuake are drop-down terminal shells

Guake, or (if it’s a K Desktop Environment) Yakuake are “drop-down terminal shells” that appear when you press a button (usually F12).

If you usually open a terminal with every log in to your Linux desktop, there’s a more convenient (not to mention laaazy) way.  Simply tell the OS to run Guake after your user’s successful login. In Openbox, you’d add it to your autostart.sh file

The Guake terminal will notify you that it has started, and then auto-hides itself until you “un-hide it” by pressing the F12 key.  When you’re done with your current command-line stuff, simply press F12 again and Guake gets out of your way.

Categories
Applications Command Line (CLI) Using Linux Your Choice

Favorite CLI Linux Apps: Lynx

Lynx is a text-only web browser that runs in the shell.

Lynx is useful tool for those times when you want to extract only the web links from a web page.  Install lynx using the Synaptic (or other) package manager.

To view the hyperlinks of a given web page (google.com in this example), issue the command

lynx -dump http://www.google.com

It can also behave in a similar way to wget when you want to view the HTML source code of a web page.  The command to view the HTML source code is

lynx -source http://www.example.com

Click the following link to view a post where we collected links to mp3 files to build an unattended download list for the wget command.  Another feature of Lynx is that it allows you to view your pages as a web crawler/robot such as googlebot might see them.