- [Instructor] Welcome to the 12th video of section two,
renaming and moving files in bulk.
In the previous video we have learned
how to slice file names based on extension.
In this video, we'll see how to write scripts
to perform renaming and moving files in bulk.
Renaming a number of files is one of the tasks
we frequently come across.
A simple example is when you download photos
from your digital camera to your computer.
You may delete unnecessary files,
and it causes discontinuous numbering of image files.
Sometimes you may need to rename them with a custom prefix
and continue numbering for file names.
We sometimes use third party tools
for performing rename operations.
We can use Bash commands to perform a rename operation
in a couple of seconds.
Moving all the files having a particular sub-string
in their files names, for example,
the same prefix for file names,
or with a specific type to a given directory,
is another use case we frequently perform.
Let's see how to write scripts to perform
these kind of operations.
The rename command helps to change file names
using Perl regular expressions.
By combining the commands find, rename, and mv,
we can perform lots of things.
The easiest way of renaming files in the current directory
to your own file name with a specific format,
is by using the following script.
The output is as follows.
The script renames all the .jpeg and .png files
in the current directory, and its sub-directories
to new file names in the format: image-1.jpeg,
image-2.jpeg, image-3.png, image-4.png, and so on.
Let's see how it works.
In the previous script, we have used a for-loop
to iterate through the names of all files
ending with a .jpeg or .png extension.
We use the find option to perform this search.
Where the -o option is used to specify
multiple iname options, which perform
a case insensitive match.
By using -maxdepth1, we make sure that dollar img
will contain a filename only from a current directory,
not it's subdirectories.
We have initialized a variable count equals one,
in order to keep track of the image number.
The next step is to rename the file using the mv command.
The name of the file should be formulated for renaming.
Dollar, curly bracket, img, hash, hash, star, dot
in the script, passes the extension
of the filename currently in the loop.
Let's count plus plus is used to increment
the file number for each execution of the loop.
There are a variety of other ways to perform
rename operations.
Let's walk through a few of them.
Renaming star dot jpeg in uppercase
to star dot jpeg in lowercase.
To replace space in the filenames
with the underscore character.
Hash, apostrophe s, slash, space, slash, underscore,
slash g, is the replacement part in the file name.
And star is the wildcard for the target files.
It can be any star dot text or any other wildcard pattern.
To convert a file name of files
from uppercase to lowercase and vice versa.
To recursively move all the .mp3 files to a given directory.
To recursively rename all the files by replacing space
with the underscore character.
Awesome.
We have successfully learned how to rename
and move files in bulk.
In the next video, we'll learn how to spellcheck
and dictionary manipulation.