Reputation: 13
I'm trying to create a bash script that takes a directory full of files (about 500 files) that have all different types of extensions (no seriously, like 30 different types of extensions) and I want get rid of all of the extensions, and replace them with .txt
I've been searching around for a while now, and can only find examples of taking a specified extension, and changing it to another specified extension. Like png --> jpg, or .doc --> .txt
Here's an example I've found:
# Rename all *.txt to *.text
for f in *.txt; do
mv -- "$f" "${f%.txt}.text"
done
This works, but only if you go from .txt to .text, I have multiple different extensions I'm working with.
My current code is:
directory=$1
for item in $directory/*
do
echo mv -- "$item" "$item.txt";
done
This will append the .txt onto them, but unfortunately I am left with the previous ones still attached. E.G. filename.etc.txt, filename.bla.txt
Am I going about this wrong? Any help is greatly appreciated.
Upvotes: 1
Views: 661
Reputation: 16036
It's a trivial change to the first example:
cd "$directory"
# Rename all files to *.txt
for f in *
do
mv -- "$f" "${f%.*}.txt"
done
If a file contains multiple extensions, this will replace only the last one. To remove all extensions, use %%
in place of %
.
Upvotes: 1