-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 
1

3 Answers 3

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.)

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

@tripleee Thank you for that reference.
-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.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.