-1

I tried the ('mv', 'sed') command's in Linux . I have more than one file I want to delete the last character of the file like this

Before

natural1_dd-aa.txt
natural2_dd-aa.txt
natural3_dd-aa.txt

I want to delete ('_dd-aa')

After

natural1.txt
natural2.txt
natural3.txt
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • 1
    Possible duplicate of [Renaming files in a folder to sequential numbers](https://stackoverflow.com/q/3211595/608639), [Renaming a set of files to 001, 002, … on Linux](https://stackoverflow.com/q/880467/608639), [Extract substring in Bash](https://stackoverflow.com/q/428109/608639), [How to remove last n characters from a string in Bash?](https://stackoverflow.com/q/27658675/608639), etc. – jww Apr 25 '19 at 11:48

3 Answers3

0

These days, I'm using pathlib for this kind of problem.

from pathlib import Path

for path in Path(".").glob("*.txt"):
    path.rename(path.with_name(path.stem.partition("_dd-aa")[0] + path.suffix))

(Update: This answer was appropriate prior to the removal of the tag when the question was edited. It's no longer relevant.)

brunns
  • 2,689
  • 1
  • 13
  • 24
0

Tentative to answer in case there are fundamental issues with this, but:

find . -name \*_dd-aa\* | while read filename
do
    mv "$filename" "$(echo $filename | sed 's/_dd-aa//g')"
done

...seems to work as requested.

Method

  1. Find all files that contain _dd-aa
  2. Obtain the new filename by removing the _dd-aa part
  3. Use mv to rename the file
Adam
  • 709
  • 4
  • 16
-1

One way:

ls *dd-aa* | sed 's/\(.*\)_dd-aa\(.txt\)/mv & \1\2/' | sh

List only those files with the pattern(*dd-aa*). Using sed, prepare the mv command by taking 1st part before the pattern (_dd-aa) and the 2nd part after the pattern. And pass it on to a shell.

Guru
  • 16,456
  • 2
  • 33
  • 46