0

How can I remove a specific filename extension in the filename in a directory using sed. Like for example I have a files in a directory,

file1.txt
file2.txt
file3.cpp

Then I want to remove the filename extension of a file with .txt extension, so the result is,

file1
file2
file3.cpp

Thanks!

domlao
  • 15,663
  • 34
  • 95
  • 134

2 Answers2

2

You can use rename:

rename 's/\.txt$//' *

Saying so would remove the .txt extension from matching files.

devnull
  • 118,548
  • 33
  • 236
  • 227
1

And if you really want to use sed, this should work:

    for file in *.txt ; do mv $file `echo $file | sed 's/\(.*\)\.txt/\1/'` ; done
Labynocle
  • 4,062
  • 7
  • 22
  • 24