Suppose, I have a directory called Titlepage that have many files named titlepage_1.pdf, titlepage_2.pdf ... titlepage_n.pdf and their tex files also. I have a bash function that alter two filenames.(e.g. $alterpdf 2 3 this command swap filename titlepage_2.pdf to titlepage_3.pdf. And does same thing for the corresponding tex files also.)

function swap(){ mv $1 $1._tmp && mv $2 $1 && mv $1._tmp $2; }
function alterpdf(){ swap titlepage_$1.pdf titlepage_$2.pdf; swap titlepage_$1.tex titlepage_$2.tex; }  

Now, I want a function(e.g, filepush new.pdf 2 3) that can insert new.pdf inside titlepage_2.pdf and titlepage_3.pdf with the name titlepage_3.pdf. And next files's number is increased. titlepage_3.pdf to titlepage_4.pdf titlepage_4.pdf to titlepage_5.pdf and so on.

There also a .tex file for all pdf in that directory.

I want to implement follows-

Titlepage$ ls
titlepage_1.tex titlepage_1.pdf titlepage_2.tex titlepage_2.pdf titlepage_3.tex titlepage_3.pdf
Titlepage$ vim new.tex
Titlepage$ pdflatex new.tex
Titlepage$ ls
new.pdf new.tex titlepage_1.tex titlepage_1.pdf titlepage_2.tex titlepage_2.pdf titlepage_3.tex 
Titlepage$ push new.pdf 2 3
Titlepage$ ls
titlepage_1.tex titlepage_1.pdf titlepage_2.tex titlepage_2.pdf titlepage_3.tex titlepage_3.pdf titlepage_4.tex titlepage_4.pdf

proposed solution

$ls *.pdf|grep -Eo [0-9]+|sort -n  

This results the index of the files. Starting from tail increment each index adding 1 for each file up to the insertion point(2nd argument of push). Then rename the target file(new.pdf) to titlepage_3rd_argement_of_push.pdf

  • 1
    What is the relevance of the .tex files? They're not mentioned in the renames to that point. Do they need to be renamed? – Jeff Schaller Mar 21 '18 at 16:45
  • @JeffSchaller yes. I edited my question again. – alhelal Mar 21 '18 at 16:58

OK, here's a script that does something like what you want.



# just in case
case $prefixnum in
  (*[![:blank:]]*) echo "invalid prefix, contains numbers"; exit 1;;

# check input arguments
if [ ! $# -eq 2 ];
  echo "USAGE: insert.sh <newfiles> <insertion_index>"
  echo 'example: insert.sh "new.pdf new.tex" 2'
  exit 1

## process infiles
for infile in ${NEWFILES}
  for file in $(ls -1 ${PREFIX}[0-9]*.${ext} | sort -rV)


      if [ "${numberless}${number}.${ext}" != "${file}" ];
        echo "invalid file ${file}"
        echo "${numberless}${number}.${ext}" "${file}"
        exit 1

      if [ ${number} -ge ${INSERT_IDX} ]
          echo "$file" "${numberless}$((${number} + 1))".$ext
          mv "$file" "${numberless}$((${number} + 1))".$ext
        echo ${file}
  echo "${infile}"  "${PREFIX}${INSERT_IDX}.${ext}"
  mv "${infile}"  "${PREFIX}${INSERT_IDX}.${ext}"
  • 1
    Check your script urgently. There is a great mistake, but easy to solve. $./insert.sh "delete.pdf delete.tex" 2 \\ \titlepage_1.pdf\\ titlepage_2.pdf titlepage_3.pdf titlepage_3.pdf titlepage_4.pdf\\ titlepage_4.pdf titlepage_5.pdf\\ delete.pdf titlepage_2.pdf \\titlepage_1.tex titlepage_2.tex titlepage_3.tex titlepage_3.tex titlepage_4.tex titlepage_4.tex titlepage_5.tex – alhelal Mar 23 '18 at 16:15
  • That means finally only titlepage_5.tex and titlepage_5.pdf and befor index 3. – alhelal Mar 23 '18 at 16:18
  • That was a bad bug, thanks for catching it. I reversed the sort order, which should get rid of that problem. – hhoke1 Mar 23 '18 at 20:51
  • Yes, now it works. The script is large enough. If you can minimize the script size it is helpful to other to understand what you want to do easily. – alhelal Mar 24 '18 at 0:13
  • I don't fell the necessity of first if(...........invalid file.......) block inside the for loop. I guess if [ "${numberless}${number}.${ext}" != "${file}" ]; is false all time. – alhelal Mar 24 '18 at 0:23

that can insert new.pdf inside titlepage_2.pdf and titlepage_3.pdf

So to clarify, you want to be able to sandwich new.pdf between to the two titlepage files, and produce a new file, titlepage_3.pdf, and move the original titlepage_3.pdf to a titlepage_4.pdf, and increment the number of the remaining pdf files with similar names?

In other words, we want to be left with a new list, where the new titlepage_3.pdf now contains the pages in titlepage_2.pdf, then the pages in new.pdf, then the pages in titlepage_3.pdf? There are several ways to concatenate pdfs you could use for this. For incrementing the numbers, I would recommend doing that before the concatenation step, using perl-style regex and the rename command.

For the .tex files, I'm not sure what exactly you're trying to accomplish, but this link might have useful information on how to concatenate tex files.

  • You don't understand my question. I don't say inserting new.pdf in a pdf file. I want to insert this inside the filesystem. – alhelal Mar 22 '18 at 0:09
  • I edited my question again. See that. – alhelal Mar 22 '18 at 0:18
  • You can delete this, as you posted a solution. – alhelal Mar 24 '18 at 0:14

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.