Ubuntu – Remove part of file name using for loop in terminal

command lineextensionrename

In terminal using for loop we can add a part before or after the file name. But if we want to remove it how do we do this?
For example we can add .old after all .txt files name using the command bellow

$for i in *.txt
>do
>    mv $i $i.old
>    done

But my question is how do we remove this .old just using the same technique?

Best Answer

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

It's a simple for cycle, just like the one you wrote. Note I've used ; instead of newlines so that I can type it into a single line. The only construct that is new to you is the ${i%%.txt}, which simply means $i, with whatever following the %% signs cut off from the end of $i.

A good tip: if unsure what would happen, try adding echo in front of the actual command, so that you would see the exact commands that are to be executed. E.g.:

for i in *.txt; do echo mv $i ${i%%.txt}; done 
Related Question