Thomas
Thomas

Reputation: 3

Renaming filename before first occurrence of character

I'm trying to rename a batch of files using a bash script or just in the command line but can't seem to find anything on how to remove characters before the first occurrence of a character.

Right now my files are named:

author1_-_year_-_title_name.txt
author2_-_year_-_title_name.txt

And I want them to look like

_-_year_-_title_name.text

or even

year_-_title_name.text

I've tried sed in the command line:

sed 's/^[^_-_]* _-_ //' *

but this only tried to edit the text files, not the file name

Upvotes: 0

Views: 1622

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133538

Could you please try rename command as follows.

rename -n s/[^-]*-_//  *.txt

Output will be as follows.

rename(author1_-_year_-_title_name.txt, year_-_title_name.txt)
rename(author2_-_year_-_title_name.txt, year_-_title_name.txt)

Once you are Happy with above results(which will print only on terminal) remove -n option in above command and it should rename the files.

Upvotes: 0

oguz ismail
oguz ismail

Reputation: 50775

You can't change filenames using sed. Try this simple loop instead:

for fp in ./*_-_*; do
  echo mv "$fp" "${fp#*_-_}"
done

If the output looks good, remove echo.

Upvotes: 1

Related Questions