Rename many files

Hi all

I have files in the following format:

01_anote1.pdf
01_bnote1.pdf
01_control1.pdf
01_ethics1.pdf
01_invoice1.pdf
01_invoice_21.pdf
20_quote_l1.pdf

I need to rename them to

01_anote.pdf
01_bnote.pdf
01_control.pdf
01_ethics.pdf
01_invoice.pdf
01_invoice_2.pdf
20_quote_l.pdf

That is, remove the 1 before the extension. Your help ppreciated.

Thanks

Lucas

Try this first without the coloured part, to be shure the mv commands are correct:

ls *.pdf | sed 's/\(.*\)1\(\..*\)/mv & \1\2/' | sh

Regards

Try..

@lmatlebyane,

Try this,

 The following command rename the files  in the directory.

rename 1.pdf .pdf *1.pdf

This one replace all the contents in the files whichever filename ends with 1.pdf

sed 's/1.pdf/.pdf/g' *1.pdf

Thanks, tried all solutions and they all work!

Can someone please explain this command

well, I can try explaining..

ls *.pdf - its clear i guess.

now..

this matches anything1.anything i.e the filename like 01_anote1.pdf

and replacing this by mv anything1.anything anything.anything

here - "anything" before dot means filename and "anything" after dot means extntion.

so the output is mv 01_anote1.pdf 01_anote.pdf which is redirecting to the shell to execute the mv command.

now in sed... & is used to replace what is matched previously.
\1 \2 are used to recall the saved regex by escaped paranthesis.

as far as i know m you can have max 9 save regex/strings.

@Franklin52 or other : if there is something else in your command please share.

if you have Python, you can use the script in my sig called file renamer. eg usage

# ls -1
01_anote1.pdf
01_bnote1.pdf
01_control1.pdf
01_invoice_21.pdf
20_quote_l1.pdf

# filerenamer.py -p "(\d)\.pdf" -e ".pdf" -l "*.pdf"
==>>>>  [ /home/images/20_quote_l1.pdf ]==>[ /home/images/20_quote_l.pdf ]
==>>>>  [ /home/images/01_invoice_21.pdf ]==>[ /home/images/01_invoice_2.pdf ]
==>>>>  [ /home/images/01_anote1.pdf ]==>[ /home/images/01_anote.pdf ]
==>>>>  [ /home/images/01_control1.pdf ]==>[ /home/images/01_control.pdf ]
==>>>>  [ /home/images/01_bnote1.pdf ]==>[ /home/images/01_bnote.pdf ]

# filerenamer.py -p "(\d)\.pdf" -e ".pdf"  "*.pdf"
/home/images/20_quote_l1.pdf  is renamed to  /home/images/20_quote_l.pdf
/home/images/01_invoice_21.pdf  is renamed to  /home/images/01_invoice_2.pdf
/home/images/01_anote1.pdf  is renamed to  /home/images/01_anote.pdf
/home/images/01_control1.pdf  is renamed to  /home/images/01_control.pdf
/home/images/01_bnote1.pdf  is renamed to  /home/images/01_bnote.pdf
# ls -1
01_anote.pdf
01_bnote.pdf
01_control.pdf
01_invoice_2.pdf
20_quote_l.pdf

remove -l to commit

sed 's/\(.*\)1\(\..*\)/mv & \1\2/' | sh

With sed you can save substrings with \(.*\) and recall them back with \1, \2, \3 etc.

\(.*\)1\(\..*\)

Here we saved the 1st portion \(.*\) before the 1 and the 2nd portion after the 1.

mv & \1\2

The string is substituted with mv & and the 2 portions. The & is a character with a special meaning and is replaced with the matched string.

Regards

I wish I were better at sed and awk. Sometimes, doing this is easier and is more understandable if you have to look at the code again one day and your sed is shaky like mine:

ls *.pdf 2>/dev/null|while read file_nm ; do
   mv $file_nm ${file_nm%%1.*}.pdf
done