I have a file called TempsModel.txt which is created as follows:

ls /media/Elise/2811226E69F71131/ModelOutput/* > TempsModel.txt

so it lists all the files in that directory. These files are all compressed netcdf files so it looks like this:


I need this list to not contain the .gz. How do I remove these three characters? I tried question Delete the last character of a string using string manipulation in shell script and Remove last character from line

But how do I create a second file TempsModel2.txt where the list does not contain these last three characters?

  • You could do it in one operation, e.g. ls /media/Elise/2811226E69F71131/ModelOutput/* | sed 's/.gz$//' > TempsModel.txt
    – fpmurphy
    Jun 27, 2019 at 14:30

2 Answers 2


It's probably not best practice to create that file using ls but given the sample input you have provided you can use


awk '{sub(/.gz$/, ""); print}' TempsModel.txt > TempsModel2.txt


sed 's/.gz$//' TempsModel.txt > TempsModel2.txt

In order to create the initial file without ever having the .gz extension you could do:

for file in /media/Elise/2811226E69F71131/ModelOutput/*; do echo "${file%.gz}"; done > TempsModel.txt

You could also do:

set -- /media/Elise/2811226E69F71131/ModelOutput/*
printf '%s\n' "${@%.gz}" > TempsModel.txt

If zsh is available, then similar to this Using parameter expansion to generate arguments list for mkdir -p, you could use a glob qualifier to do the suffix removal on the fly:

setopt histsubstpattern extendedglob

print -rl -- /media/Elise/2811226E69F71131/ModelOutput/*.gz(#q:s/%.gz/) > TempsModel2.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.