user3667111 user3667111 - 1 year ago 37
Linux Question

Appending files that end with .log to a txt file without the .log

Say I have a bunch of files:


I want to output all the ones that end in the .log into a text file without the .log.

Expected output (text file):


I have a pretty botch way of doing this, I create the files a new seperate files without the .log. But As I'm working on data sets of 100000 files it can be an exhaustive job and take a while.

What I have been doing:

for file in *.log; do cp -- "$file" "${file%%.log}"; done

And then I have been been listing them and then appending them to a txt file

Answer Source

I guess you want:

for file in *.log ; do
    echo "${file%.*}"
done > output.txt

${file%.*} is a so called parameter expansion. In this case it removes the shortest match of a dot followed by arbitrary characters from the end for $file. This will effectively remove the file extension.