Chris
Chris

Reputation: 891

Remove multiple extensions from all files in a directory (Bash)

Thanks to messing up my first attempts to do this, I now have a directory full of files that are now named like this:

valery-special-music-poetry.txt.mtxt.md.md.txt.mtxt.md.md.md

which I need to be named:

valery-special-music-poetry.md

how can I do this from the command line?

I am running: GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)

Upvotes: 1

Views: 2832

Answers (4)

MeaCulpa
MeaCulpa

Reputation: 911

By Shell itself:

for i in *; do mv "$i" "${i%%.*}.${i##*.}"; done

Unless your dir are single level, I suggest using find, if you have in Apple.

Upvotes: 3

user123444555621
user123444555621

Reputation: 152956

for file in *; do mv "$file" "${file/.*./.}"; done

Upvotes: 3

Konza
Konza

Reputation: 2163

This may help you

#!/bin/bash
for filename in *
do
    x=`echo $filename | sed 's/\..*\./\./g'`
    mv $filename $x
done

Save this to a file called rename.sh

chmod +x rename.sh
./rename.sh

Upvotes: 2

Vijay
Vijay

Reputation: 67211

ls -1 *.txt.mtxt.md.md.txt.mtxt.md.md.md | nawk -F"." '{cmd="mv "$0" "$1".md";system(cmd)}'

Upvotes: 0

Related Questions