Truncate multiple file extensions

Hi,

I have files with names like file1.txt.txt.txt.txt and file2.txt.txt.txt.txt.txt............ (random infinite number of .txt exist).

how to truncate (mv) their names to ones with single .txt extension like file1.txt and file1.txt ? In other words, how to extract the filename upto first extension part?

Txs
Prvn

Do something like this---

mv file1.txt.txt* file1.txt
mv file2.txt.txt* file2.txt

or

sed s/file1.txt.txt*/file1.txt

Thanks
Namish

Hi try this one:

echo "number"
read num
who | nawk -v n=$num 'BEGIN{
str="file"
for (i=1;i<=n;i++)
str=sprintf("%s%s",str,".txt")
}
END{
print str
printf("%stxt\n",substr(str,1,index(str,".")))
}'

Txs namishtiwari,

But i have to do this for thousands of files in a directory so i cant use hardcoded filename. i will be using for loop for reading each file name. So, here how could i extract the "filename with first extension" out of "filename with infinite extensions"?

txs summer_cherry, here i dont know what is "number"..

Summer_cherry is just doing a echo of number,nothing to do with number that is not a variable.Whatever you do with echo that is excatly printed again.If you do not understand the code let me know i will tell you.

Thanks
Namish

Namish,

I know what " echo "number" " does and there's nothing hard to understand there. But his code expects again a "num".

I'm not sure if you read my post fully and again i repeat "how could i extract the "filename with first extension" out of "filename with infinite extensions"?" e.g., how to extract "file1.txt" from file1.txt.txt.txt..........? (no hardcoding of filenames with wild char pls..)

Regards,

Prvn

suppose you enterd num as 5,then it will print a file that is having a extension of
file1.txt.txt.txt.txt.txt
and reduce it to
file1.txt

if you have Python, here's an alternative

#!/usr/bin/python
import os,glob
for files in glob.glob("file*.txt"):
    s= files.split(".")
    newfile='.'.join(s[:2])
    os.rename(files,newfile) 

output:

# ls -1 file*.txt
file 2.txt.txt.txt.txt
file1.txt.txt.txt.txt
# python testing.py
# ls -1 file*.txt
file 2.txt
file1.txt

[/tmp]$ touch 1.txt 2.txt.txt 3.txt.txt.txt
[/tmp]$ ls *.txt
1.txt  2.txt.txt  3.txt.txt.txt
[/tmp]$ for file in *.txt; do mv ${file} ${file%%.txt*}.txt 2> /dev/null ; done
[/tmp]$ ls *.txt
1.txt  2.txt  3.txt
[/tmp]$ 

Great Vino..... it worked like a charm...Txs for your wonderful solution...

Txs ghostdog74, but i dont have python....

dear Vino,

I am really curious to know the expression ${file%%.txt} .

Please can you explain it to me.
I was looking for a solution to extract the extrension of a file name. I wanted to split a file by line numbers but the unix split command splits the file with suffix is aa, ab, ac etc which I want to convert into 00, 01 etc and I was struggling to get the extension part.

Thanks a lot in anticipation.

Type "man split" and you'll find your answer.

for i in file*.txt*
do
      newname=$(echo $i | cut -d. -f1-2)
      mv $i $newname
done