remove double extensions in bash
bash, rename, shell
Solution
Assuming:
- You only want to perform this in the current working directory (non-recursively)
- The double extensions have format precisely as `.jpg.jpg`:
Then the following script will work:
#!/bin/bash
for file in *.jpg.jpg
do
mv "${file}" "${file%.jpg}"
done
Explanation:
- `${file%.jpg}`: This part is called Parameter Subsitution.
- From the same source: "${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end of $var."
- Note that the "pattern" mentioned here is called globbing, which is different from regular expression in important ways.
To use this script:
- Create a new file called `clean_de.sh` in that directory
- Set it to executable by `chmod +x clean_de.sh`
- Then run it by `./clean_de.sh`
A Note of Warning:
As @gniourf_gniourf have pointed out, use the `-n` option if your `mv` supports it.
Otherwise - if you have `a.jpg` and `a.jpg.jpg` in the same directory, it will rename `a.jpg.jpg` to `a.jpg` and in the process override the already existing `a.jpg` without warning.
Problem
I am familiar with rename but I was curious does rename still apply for removing duplicate extensions?? Say I have a few files named: - picture2.jpg.jpg - picture9.jpg.jpg - picture3.jpg.jpg - picture6.jpg.jpg How would you remove the the duplicate extension?? End result: - picture2.jpg - picture9.jpg - picture3.jpg - picture6.jpg