Both Windows and Linux offer some inbuilt utilities for easily renaming files, which are outlined in this article.
It is strongly advised to test commands on dummy files with your naming scheme first. Please note that this article is intended for guidance only: it is ultimately the student's responsibility to ensure any commands being ran are correct (see the 'avoiding mishaps' section at the bottom of the article for some suggestions to avoid mistakes during this process).
For ease of operation, it is recommended to avoid using any spaces in your filenames.
Windows (Powershell)
1) Press Windows+x and select 'Windows PowerShell' from the utility menu to launch a console session.
2) Important: You will first need to navigate to a directory (folder) you wish to operate in using the 'set-location' command. For example to move to the X:\Downloads folder, enter:
Set-Location X:\Downloads
3) Then use 'get-childitem' cmdlet to list the contents of the folder, and 'rename-item' to change the names of listed files:
Get-Childitem | Rename-Item -NewName {$_.name -replace "Orangtun","Orangutan"}
# The two values that are given to '-replace', are 1) string to be replaced, and 2) string to
# replace with.
If you don't want to affect all files in the working folder, but rather only those that fit a certain naming scheme, this can be done by adding a path to get-childitem.
The '*' symbol (also known as a 'wildcard') represents any number of any character. For example, oliv* will match text such as 'Olivia', 'Oliver' and 'olivine'.
# Files with names that include certain text
Get-Childitem -path *Orangtun* | Rename-Item -NewName {$_.name -replace "Orangtun","Orangutan"}
# Files ending in .exr
Get-Childitem -path *.exr | Rename-Item -NewName {$_.name -replace "Orangtun","Orangutan"}
If you see the error 'rename-item : Source and destination path must be different.', it is likely because the command attempted to run on a directory (rather than a regular file), but made no change to it. To supress this, you can add the -file switch to Get-Childitem so that it skips any directories:
Get-Childitem -file | Rename-Item -NewName {$_.name -replace "Orangtun","Orangutan"}
Fig. 1. An example of the process. There are only files in this directory and therefore no error.
Linux (Terminal)
The 'rename' utility can be used to rename files.
1) Right-click on the Linux desktop and select the option to open a terminal.
2) Important: change location to the folder you want to work in using the 'cd' (change directory) command:
# E.g. to change to the Downloads folder
cd /escape/profiles/students/UG/PCL00001/Downloads
# You can also use the ~ symbol as a shorthand for your home directory
cd ~/Downloads
3) Run the command. Note the syntax is as follows (you can also enter man rename in the terminal to view the full help page):
rename [options] expression replacement file
Where:
- Expression is the string of text you want to replace
- Replacement is the new string of text that will replace the old text
- File is the file you want to operate on.
- For basic use [options] can be ignored.
In order to rename multiple files at once, the '*' and '?' (wildcard) characters should be used (note that '*' means any number of any character, while '?' means one of any character).
For example:
# This will rename files starting with 'orangtun'
rename orangtun orangutan orangtun*
# This will rename files such as orangtun_001, orangtun_999 and orangtun_abc
rename orangtun orangutan orangtun_???
Note: unlike Windows, Linux is case sensitive, so correct capitalization must be used when running commands.
Avoiding mishaps: things to consider
Running the above commands may cause unexpected results when multiple file sequences exist in the same directory.
This is especially the case if the name of one contains the other: for example, my_render.exr and my_render_dark.exr.
In this scenario commands that operate on a string of text 'my_render' will also apply the files named my_render_dark.exr as the latter includes the previous string.
It is often possible to avoid these issues with specific combinations of wildcards, but to avoid unexpected outcomes, we strongly recommend storing each sequence of files in its own dedicated directory during the renaming process.
Comments