Score:1

How to use pdfcrop to crop multiple files in directory?

ke flag

I want to use pdfcrop to crop all pdf-files in a folder, but I am completely unfamiliar with bash. What I have so far is:

for file in *.pdf; do
   pdfcrop "$file" "${file%.*}_Cropped.pdf";
done

The name of the output file should be fine, as this gives the wanted results:

for file in *.pdf; do
   echo "${file%.*}_Cropped.pdf";
done

But for each file pdfcrop returns "!!! Error: Link from 'my_correct_input_filename' to 'tmp-pdfcrop-some_more_numbers-img.pdf' failed (Operation not permitted)!" My filenames include empty spaces and '. Is that the reason? But it seems to recognize the input filename correctly.

Edit: For using pdfcrop with a single file only, the empty spaces cause trouble as expected, because the file name is interpreted as multiple names.

hr flag
Your shell quoting looks correct to me, so I don't think spaces or other special characters in filenames are the issue. Where are the files located? Are they on the same filesystem as your OS, or a different one? What are your Ubuntu and pdfcrop versions?
David avatar
cn flag
Is it possible it is creating tmp file and there is the conflict?
qcabepsilon avatar
ke flag
@steeldriver The files are located in the shared folder of the host and the virtual machine that I use. It is mounted in /media. The ubuntu version is 20.04. The pdfcrop is from texlive-extra-utils 2019.202000218-1.
qcabepsilon avatar
ke flag
@David In an earlier try pdfcrop could not finish and I ended up with files called tmp.. which were pdfs with the cropped content. Maybe pdfcrop first creates a tmp file and then renames it.
David avatar
cn flag
Which was my thought.
qcabepsilon avatar
ke flag
@David What can I do about it? When renaming a file to not include any 'special' characters it works. But I would prefer to not rename everything. Do you have an idea how to circumvent this?
Score:2
jp flag

For using pdfcrop with a single file only, the empty spaces cause trouble as expected, because the file name is interpreted as multiple names.

That is unlikely as you are using a properly quoted parameters in your loop, so no shell word splitting can happen in that case:

for file in *.pdf; do
   pdfcrop "$file" "${file%.*}_Cropped.pdf";
done

What could it be then?

Upon inspecting the source code for pdfcrop, the error you get is defined in:

if ($symlink_exists) {
    symlink($inputfile, $inputfilesafe)
        or die sprintf "$Error Link from `%s' to `%s' failed (%s)!\n",
                       $inputfile, $inputfilesafe, exterr;

Which means that a symbolic-link creation was not successful ... pdfcrop creates symbolic-links to original files with unsafe names prior to processing as stated in the comments part of the linked source code:

# Input files with unsafe file names are linked/copied
# to temporary file with safe file name.

The definition of unsafe filenames can be found in this source code line as a regular expression:

elsif (not $inputfile =~ /^[\w\d\.\-\:\/@]+$/) { # /[\s\$~'"#{}%]/

i.e. (remember these are regular expressions e.g. \w is a word character and \d is a digit and \s is a space and \$ is a literal $ ... etc) any of \w\d\.\-\:\/@ are considered safe ... While any of \s\$~'"#{}% are not considered safe in an input filename.

The actual exterr(external error) will indicate the reason as for example if a file with the same name of the random link name it's trying to create that error will read something like file exists ... However, in your case the external error reads Operation not permitted which most likely is because the filesystem on which your working directory resides doesn't support symbolic-links ... For example *FAT* filesystems don't support symbolic-links ... Therefore, changing your working directory to a filesystem that supports symbolic-links like Ubuntu's EXT4 should solve this issue.

qcabepsilon avatar
ke flag
I checked the file system with 'mount' which gives me 'type vboxsf' for my folder. Symbolic links in the virtual box shared folder seems to be a problem, indeed. Since I cannot change the type here, I will now copy my files to another folder and crop them there.
qcabepsilon avatar
ke flag
Is there a chance that you found out what is considered an unsafe name in passing, so that I can avoid that in the future?
Raffa avatar
jp flag
@qcabepsilon I updated the answer for that ... Plus, [this](https://lwn.net/Articles/686789/) is a good read on the matter of safe and unsafe filenames in general.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.