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?