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.
- 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.
- 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.
- 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”
- Your 4 jpg files should now be named “img1.jpg”, “img2.jpg” and etc.
- 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.
- 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.