2

I have a lot of papers in pdf files (and also other type of files in the same folder). I'd like to rename them using the pattern

<current file name>[<number of pages>].pdf

But after some tries I got a lot of wrong file names with duplicated number of pages at the end. For example,

Madsen - Categorical[48][48].pdf

So, I'd like to:

  1. Clean all file names removing (if) any occurrence of numbers in brackets at the end. For example, the file above should be renamed to

Madsen - Categorical.pdf

  1. Rename all files just inserting [<number of pages>] at the end.

The [<number of pages>] should be read from the pdf file since I don't have this information.

1

So, here are two scripts, which you can put in a folder with the pdf-files needing to be renamed.

The first one is for adding page numbers and the second is for deleting them. Both scripts are interactive

  • Type abort Enter to exit the script,
  • type n Enter to enter interactive file-by-file mode
  • and if you want a script to proceed with all pdfs without confirmation type y Enter y Enter.

The script to add number of pages in file.pdf (file.pdf -> file[N].pdf)

#!/bin/bash

IFS='
'
ret="not_ok";

renameinteractively () {
for file in *.pdf ; do 
  # derive number of pages in a pdf document
    npages=`pdfinfo $file | grep Pages | awk -e '{print $2}'`;
  # make up a new filename
    file2=${file%%.pdf}[$npages].pdf;
  # an auxiliary variable
    ret="not_ok";

  # interactive part. Ask to rename or not.
    printf "Rename\n $file to \n $file2 ? (y/n/abort) \n";
    until [ $ret == 'ok' ]; do
  # read your answer and y-for rename, n-skip and abort-to exit
      read ans;
      if [ $ans == "y" ]
      then 
          mv $file $file2;
          ret="ok";
          continue;
      elif [ $ans == "n" ]
      then 
          ret="ok";
          break;
      elif [ $ans == "abort" ]
      then 
          exit;
      else
          printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi
      done;
  done ;
}

renameallofthem () {
until [ $ret == 'ok' ]; do
      if [ $a == "y" ]
      then 
    for file in *.pdf ; do 
      npages=`pdfinfo $file | grep Pages | awk -e '{print $2}'`;
      mv $file ${file%%.pdf}[$npages].pdf; 
      printf "\nMoved \n"
      echo $file
      printf "to\n"
      echo ${file%%.pdf}[$npages].pdf
    done ;
    ret="ok";
    exit;
      elif [ $a == "n" ]
      then 
    printf "\n\n OK, let's do it interactively!\n\n"
    renameinteractively;
      elif [ $a == "abort" ]
      then 
    exit;
      else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi;
done;
}

printf "Rename ALL of the .pdf files in current folder? (y/n/abort) \n"
read a
if [ $a == "y" ]
then
      printf "Really??\n\n Do we rename ALL of them? (y/n/abort) \n"
      read a
      renameallofthem;
elif [ $a == "n" ]
then 
  printf "\n\n OK, let's do it interactively!!\n\n"
  renameinteractively;
elif [ $a == "abort" ]
then 
    exit;
else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
fi

The script to remove number of pages from file[N][N]....[N].pdf (file[N][N]....[N].pdf -> file.pdf)

#!/bin/bash

IFS='
'
deleteNumbersInteractively () {
for file in `ls *].pdf | grep '\[[0-9]*\]'` ; do 
# an auxiliary variable
  ret="not_ok";
# make up a new filename
  file2=`echo $file | sed -e 's/\[[0-9]\+\]//g' `;
# interactive part. Ask to rename or not.    
  printf "Rename\n $file to \n $file2 ? (y/n/abort) \n";
  until [ $ret == 'ok' ]; do
      read ans;
      if [ $ans == "y" ]
      then 
      mv $file $file2;
      ret="ok";
      continue;
      elif [ $ans == "n" ]
      then 
      ret="ok";
      continue;
      elif [ $ans == "abort" ]
      then 
      exit;
      else
      printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi
  done;
done;
}
deleteAllTheNumbers () {
until [ $ret == 'ok' ]; do
      if [ $a == "y" ]
      then 
    for file in *.pdf ; do 
      file2=`echo $file | sed -e 's/\[[0-9]\+\]//g' `;
      mv $file $file2; 
      printf "\nMoved \n"
      echo $file
      printf "to\n"
      echo $file2
    done ;
    ret="ok";
    exit;
      elif [ $a == "n" ]
      then 
    printf "\n\n OK, let's do it interactively!\n\n"
    deleteNumbersInteractively;
      elif [ $a == "abort" ]
      then 
    exit;
      else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi;
done;
}


printf "Delete ALL of the filename[NUMBERS].pdf from files in current folder? (y/n/abort) \n"
read a
if [ $a == "y" ]
then
      printf "Really??\n\n Do we rename ALL of them? (y/n/abort) \n"
      read a
      deleteAllTheNumbers;
elif [ $a == "n" ]
then 
  printf "\n\n OK, let's do it interactively!!\n\n"
  deleteNumbersInteractively;
elif [ $a == "abort" ]
then 
    exit;
else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
fi

Is everything ok now?

  • Does pdfinfo work also for pdf which contains only scanned papers? – Sigur Aug 17 '15 at 8:53
  • yes it seems to work with any pdfs. – Evgenii Frolov Aug 17 '15 at 9:10
  • edited according to your raname rule: name.pdf -> name[N].pdf – Evgenii Frolov Aug 17 '15 at 9:24
  • Since I have files with blank spaces in their names your code does not work. With echo mv it seems to be OK but when I use only mv I get mv: the target “Ordnung[27].pdf” is not a directory. – Sigur Aug 17 '15 at 9:30
  • Edited the code keeping 'space separated pdf names.pdf' files in mind. – Evgenii Frolov Aug 17 '15 at 9:42
3

Here's one simple way, that addresses your original question completely. Could be done as a one-liner, but this syntax keeps it readable.

#!/bin/bash
for F in "$@"
do
echo mv "$F" "${F%.pdf}[$(pdfinfo "$F" | awk '/^Pages/{print $NF}')].pdf"
done
$ ls *pdf
aosa-bash.pdf  article.pdf  bash.pdf  bashref.pdf  rose94.pdf
$ find . -name \*.pdf -exec ./pdf.sh {} +
mv ./article.pdf ./article[11].pdf
mv ./bashref.pdf ./bashref[172].pdf
mv ./bash.pdf ./bash[75].pdf
mv ./aosa-bash.pdf ./aosa-bash[14].pdf
mv ./rose94.pdf ./rose94[13].pdf
$

If happy with the proposed renames, either paste all the mv commands into a shell window or revise the script, replacing the echo mv with simply mv.

  • 1
    I'd like to obtain as output article[11].pdf. The number of pages in brackets. – Sigur Aug 17 '15 at 8:53
  • Sure, now revised. – steve Aug 17 '15 at 9:06
  • Almost. I have pdf files with blank spaces. I'm working on it. – Sigur Aug 17 '15 at 9:12
  • What is the meaning of {F%.pdf}? Why not mv "$F" "$F"[$( )]? – Sigur Aug 17 '15 at 9:16
  • 1
    @seteve, please, see my edited post. – Sigur Aug 17 '15 at 12:29

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.