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,145
  • 32
  • 90
  • 128

2 Answers2

2

You can use rename:

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

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

devnull
  • 111,086
  • 29
  • 224
  • 214
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
  • 3,454
  • 7
  • 21
  • 24