4

Often I have to annotate multiple pdf documents using xournal. If I start with a directory of "fresh" pdf files, I just open them all with xournal:

for f in *pdf; do xournal $f&; done

When I begin to annotate a file, say a .pdf I save it as a .xoj, i.e. I just switch the file extension. Now suppose I have an interrupted session and want to open all .pdf files in the directory provided that no corresponding .xoj file exists and open the .xoj file otherwise (both with xournal).

How can I do this in command line?

6

You can just remove .pdf to get the file's name without the extension and check for a file of that name with the .xoj extension:

for f in *.pdf
do
    if [ -f "${f%.pdf}".xoj ]
    then
        xournal "${f%.pdf}".xoj &
    else
        xournal "${f}" &
    fi
done
| improve this answer | |
5
for f in *.pdf; do
  fname=$(basename "$f" .pdf);
  if [[ -e "${fname}".xoj ]]; then
    # .xoj file does exist, do things here
  else
    # .xoj file doesn't exist, do other things here
  fi
done
| improve this answer | |
  • 1
    Strictly speaking, you don't need the braces: "$fname".xoj (or "$fname.xoj" ) would be OK. – G-Man Says 'Reinstate Monica' Nov 7 '15 at 3:36
3

With zsh you could use the estring glob qualifier:

for f (./*.pdf(.e_'[[ ! -f $REPLY:r.xoj ]] || REPLY=$REPLY:r.xoj'_))
xournal $f &

The string [[ ! -f $REPLY:r.xoj ]] || REPLY=$REPLY:r.xoj is executed as shell code. REPLY holds the value of the current argument. If there's no corresponding .xoj file for the current .pdf file then [[ ! -f $REPLY:r.xoj ]] returns true and nothing happens (REPLY remains unchanged).
If there is a corresponding .xoj file for the current .pdf file then [[ ! -f $REPLY:r.xoj ]] returns false and REPLY=$REPLY:r.xoj is executed which replaces (via the :r modifier) the .pdf extension of the current argument with .xoj.

| improve this answer | |
-2

Just trying to make it clear and concise. Re-assigning f and invoking xournal in only one place, accessing the extension mangling ${f%.pdf} only once.

for f in *.pdf
do
    xoj = "${f%.pdf}.xoj"
    [ -f "$xoj" ] && f="$xoj"
    xournal "$f" &
done
| improve this answer | |

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.