Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

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
share|improve this answer
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
share|improve this answer
    
Strictly speaking, you don't need the braces: "$fname".xoj (or "$fname.xoj" ) would be OK. – G-Man 20 hours ago

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.

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.