- [Instructor] Welcome to the 11th video of section two,
slicing based on extension.
In the previous video
we've learned how to split files and data.
In this video we'll see how to perform actions
such as renaming the files by preserving the extension,
converting files from one format to another.
Several shell scripts perform manipulations
based on filenames.
We may need to perform actions such as renaming the files
by preserving the extension,
converting files from one format to another,
change the extension by preserving the name,
extracting a portion of the filename,
and so on.
The shell comes with inbuilt features
for slicing filenames based on different conditions.
Let's see how to do it.
The name from name.extension can easily be extracted
using the percent operator.
You can extract the name sample.jpg as follows.
The output file is sample.
The next task is to extract the extension of a file
from its filename.
The extension can be extracted
using the hash operator as follows.
Extract.jpg from the stored filename
and the variable file_jpg as follows.
The output is jpg.
Now we'll see how it works.
In the first task, in order to extract the name
from the filename in the format name.extension,
we've used the percent operator.
Dollar var percent dot star can be interpreted as,
remove the string match from the dollar var
for the wildcard path that appears
to the righthand side of percent,
dot star in the previous example.
Evaluating from right to left direction
should make the wildcard match.
Let's store the filename as var=sample.jpg.
Therefore the wildcard match for dot star
from right to left is .jpg.
Thus it is removed from the dollar var string
and the output will be sample.
Percent is a non-greedy operation.
It finds the minimal match
for the wildcard from right to left.
There is an operator, percent percent,
which is similar to percent but is greedy in nature.
This means it finds the maximal match
of the string for the wildcard.
For example we have,
using the percent operator we have,
the output will be hack.fun.book.
The percent operator performs a non-greedy match
for dot star from right to left .txt.
Using the percent percent operator, we have...
The output will be hack.
The percent percent operator performs a greedy match
for dot star going right to left, .fun.book.txt.
In the second task, we've used the hash operator
to extract the extension from the filename.
It's similar to percent, but evaluates from left to right.
Dollar var hash star dot can be interpreted as,
remove the string match from dollar variable
for the wildcard pattern match appears
to the righthand side of the hash,
star dot in the previous example.
Evaluating from the left to right direction
should make the wildcard match.
Similarly, as in the case of percent percent,
we have another greedy operator for hash,
which is hash hash.
It makes greedy matches by evaluating from left to right
and removes the match string from the specified variable.
Let's use this example.
By using the hash operator, we have...
The output will be fun.book.txt.
The hash operator performs a non-greedy match
for star dot from left to right, hack.
By using the hash hash operator, we have...
The output will be .txt.
The hash hash operator matches a greedy match
for star dot from left to right, txt.
The hash hash operator is preferred over the hash operator
to extract the extension from a filename
since the filename may contain multiple dot characters.
Since hash hash makes a greedy match,
it always extracts extensions only.
Here is a practical example that can be used
to extract different portions of a domain name,
given URL=www.google.com.
Awesome.
We have successfully learned about slicing filenames
based on extension.
In the next video
we'll learn how to rename and move files in bulk.