user3667111 user3667111 - 5 months ago 26
Linux Question

Removing the file extension in a text file bash

So I have put a load of names of files in a text file, these are specifically

.log
files:

ls *.log > finished_data.txt


Now that I Have the list of
.log
files, how do I keep the names but remove the
.log
extension?
My thought process is renaming them all?

Answer

Just loop through the .log files and move them:

for file in *.log
do
    mv "$file" "${file%.log}"
done

This uses shell parameter expansion:

$ d="a.log.log"
$ echo "${d%.log}"
a.log