1

My folder structure looks like this

Main
├── Sub1
|   ├── Subsub1
|   |   |   ├── file1.tex
|   |   |   ├── file2.tex
|   ├── Subsub2
|   |   |   ├── file1.tex
|   |   |   ├── file2.tex
├── Sub2
|   ├── Subsub1
|   |   |   ├── file1.tex
|   |   |   ├── file2.tex
|   ├── Subsub2
|   |   |   ├── file1.tex
|   |   |   ├── file2.tex

I want to loop to all subsubdirectories and execute the command pdflatex on each file.

My try

for d in */*/ ; do
    pdflatex "$d*.tex"
done

...but this doesn't work.

3
  • Welcome to the site. Can you tell us in what way it doesn't work? Is there any console output you can share with us that may help understand the problem? If so, please edit your question to include the relevant information. – AdminBee Feb 21 '20 at 8:43
  • Try removing " from pdflatex like that pdflatex $d*.tex. This will call pdflatex for each directory with all *.tex files. That means pdflatex will get called four times for the structure shown in your answer. Each time with two files as parameter. If you want to have pdflatex called eight times. It has to be done differently. – nobody Feb 21 '20 at 9:03
  • Also can you use a better variable name, it will help us see your intentions. Should d be directory_name or file_name? – ctrl-alt-delor Feb 21 '20 at 10:54
2

In bash you use globstar shell option.

#!/usr/bin/env bash

shopt -s globstar

for d in Main/**/*.tex; do
  echo pdflatex "$d"
done

Or use find.

find Main/ -type f -name '*.tex' -exec sh -c 'echo pdflatex  "$@"' _ {} +

Remove the echo once you're satisfied with the result.

10
  • How to move the current directory always to the directory where the current file is? – Sr. Schneider Feb 21 '20 at 9:27
  • 1
    move the current directory? – Jetchisel Feb 21 '20 at 9:30
  • The ´pdflatex´ command only works if the current working directory is where the file is (because I have some relative path in my .tex files). – Sr. Schneider Feb 21 '20 at 9:32
  • How to set the current working directory to the directory of my .tex file in the loop? Source – Sr. Schneider Feb 21 '20 at 9:35
  • 1
    This is the solution to my question. Thanks @Jetchisel. – Sr. Schneider Feb 21 '20 at 9:56

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.