user3667111 user3667111 - 4 months ago 7
Linux Question

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

Say I have a bunch of files:

blar23123.log
blar23131.log
blar11111.txt
blar55532.log


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

Expected output (text file):

blar23123
blar23131
blar55532


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

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.

Comments