5

I have multiple files whose names look like this

2313_ABCDEAUG2014_PQRST_0_.pdf
3244_ABCDEAUG2014_PQRST_0_.pdf
4234_ABCDEAUG2014_PQRST_0_.pdf

t want to rename the file in the following format.

AZ-2014-08-2313.pdf
AZ-2014-08-3244.pdf

Please suggest.

5

There is a – I consider cool – piece of software called perl-rename in which you can do your task as simple as:

perl-rename 's/^(.*)_.*AUG2014.*/AZ-2014-08-$1.pdf/' *.pdf

HTH, Cheers,

  • (note that it's sometimes available under the name prename or rename). – Stéphane Chazelas Sep 3 '14 at 20:48
  • Questioner? what OS are you using? On Debian this tool is called rename. And yes it is cool. – ctrl-alt-delor Sep 3 '14 at 21:15
  • Just for the record, under Arch Linux it is community/perl-rename. – Scyld de Fraud Sep 4 '14 at 5:07
2

If file names don't contain single quote or newline characters:

ls | sed -n "s/$/:JAN1FEB2MAR3APR4MAY5JUN6JUL7AUG8SEP9OCT10NOV11DEC12/
  s/\(\(.*\)_.*\(...\)\([0-9]\{4\}\)_.*\):.*\3\([0-9]*\).*/mv -i -- '\1' 'AZ-\4-\5-\2.pdf'/p"

Add | sh to actually do the renaming.

That's using a common sed trick to do some translation (here AUG -> 8).

We append the mapping table (here JAN1FEB2...) to the end of the pattern space and rely on back-references (\(...\).*\1\([0-9]*\)) to do the look up. We're capturing the AUG in \3 and getting (in \5 the sequence of digits that follows that \3 in the mapping table.

1

The following will go through all pdf files, and copy them to the new name format, leaving the original files in place so you can check it is as you want it.

for file in $(ls *AUG2014*pdf) ; do newName=$(echo $file | awk -F_ '{ print $1 }') ; cp $file AZ-2014-08-$newName.pdf ; done

ls *AUG2014*pdf - lists all files containing Aug2014, ending in pdf

for aString in <list> ; do someCommand $aString ; done - loops through all elements in a list, with each one referenced as aString. Referencing aString using the dollar symbol $ before hand.

awk -F_ - breaks the string into tokens as per the "_" divider. The command above prints the first token.

Be careful that there are not multiple files starting with the same number. If there are, two files may be copied to the same filename, one overwriting the other. Check the number of old files against the number of new files.

0

Well, here's what I did with GNU tar:

mkdir test; cd test
eval "set -- $(i=
for m in JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
do  touch 2313_ABCDE${m}2014_PQRST_0_.pdf \
        3244_ABCDE${m}2014_PQRST_0_.pdf \
        4234_ABCDE${m}2014_PQRST_0_.pdf
    printf " %s's/\([0-9]*\)_.*$m\(20..\)_.*\./AZ-\\\\2-%02d-\\\\1./'" \
    --xform= $((i=$i+1))
done)"
mkdir -p ../new
tar -c . "$@" | tar -C../new -x

You obviously wouldn't need the mkdir test or the touch ..\..\.. parts, but I did those so that I might verify this answer. You can see the results of those two here:

ls -m 

2313_ABCDEAPR2014_PQRST_0_.pdf, 2313_ABCDEAUG2014_PQRST_0_.pdf,
2313_ABCDEDEC2014_PQRST_0_.pdf, 2313_ABCDEFEB2014_PQRST_0_.pdf,
2313_ABCDEJAN2014_PQRST_0_.pdf, 2313_ABCDEJUL2014_PQRST_0_.pdf,
2313_ABCDEJUN2014_PQRST_0_.pdf, 2313_ABCDEMAR2014_PQRST_0_.pdf,
2313_ABCDEMAY2014_PQRST_0_.pdf, 2313_ABCDENOV2014_PQRST_0_.pdf,
2313_ABCDEOCT2014_PQRST_0_.pdf, 2313_ABCDESEP2014_PQRST_0_.pdf,
3244_ABCDEAPR2014_PQRST_0_.pdf, 3244_ABCDEAUG2014_PQRST_0_.pdf,
#...and so on

But there are also...

ls -m ../new

AZ-2014-01-2313.pdf, AZ-2014-01-3244.pdf, AZ-2014-01-4234.pdf,
AZ-2014-02-2313.pdf, AZ-2014-02-3244.pdf, AZ-2014-02-4234.pdf,
AZ-2014-03-2313.pdf, AZ-2014-03-3244.pdf, AZ-2014-03-4234.pdf,
AZ-2014-04-2313.pdf, AZ-2014-04-3244.pdf, AZ-2014-04-4234.pdf,
AZ-2014-05-2313.pdf, AZ-2014-05-3244.pdf, AZ-2014-05-4234.pdf,
AZ-2014-06-2313.pdf, AZ-2014-06-3244.pdf, AZ-2014-06-4234.pdf,
AZ-2014-07-2313.pdf, AZ-2014-07-3244.pdf, AZ-2014-07-4234.pdf,
AZ-2014-08-2313.pdf, AZ-2014-08-3244.pdf, AZ-2014-08-4234.pdf,
AZ-2014-09-2313.pdf, AZ-2014-09-3244.pdf, AZ-2014-09-4234.pdf,
AZ-2014-10-2313.pdf, AZ-2014-10-3244.pdf, AZ-2014-10-4234.pdf,
AZ-2014-11-2313.pdf, AZ-2014-11-3244.pdf, AZ-2014-11-4234.pdf,
AZ-2014-12-2313.pdf, AZ-2014-12-3244.pdf, AZ-2014-12-4234.pdf

As you can see it changed all of the files I created to your specifications. Now, having done this, I'm satisfied, and so I will:

rm *; mv ../new/* ./; rmdir ../new

And that's that.

0
filelist='2313_ABCDEAUG2014_PQRST_0_.pdf 3244_ABCDEAUG2014_PQRST_0_.pdf 4234_ABCDEAUG2014_PQRST_0_.pdf'
for i in $filelist; do 
  id=$(echo $i|awk -F_ '{print $1}');
  mv $i AZ-2014-08-$newname.pdf;
done

You may have different year or month. If that's the case, you have to extract the other parts of the name you want to keep in order to build the new name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.