The code below crops blank pdf image borders. The output file of the example below contains the tail "_cropped" before the extension ".pdf". Indexing a string might be a good solution to take the tail out. This URL provides an explanation for indexing. I do not know how to assign the respective filename to a variable and change its name by <old-name> <new-name>. Any help is appreciated. :)

for f in *; do
    pdf-crop-margins -v -p 0 -a -6 "$(basename $f .pdf).pdf";
  • 3
    To me it is a bit unclear what you want. Does the program make an example.pdf to an example_cropped.pdf? And what do you want to do next? Please try to be a bit more clear what you are given, what you desire as output and maybe add an example. – FelixJN Dec 23 '20 at 21:19
  • I edited the file for better comprehension. – Bruno Peixoto Dec 23 '20 at 21:49
  • So you want to manipulate the filename string? – FelixJN Dec 23 '20 at 22:40
  • yes, I want to edit it. – Bruno Peixoto Dec 23 '20 at 23:05
  • How do you want to edit it? From what to what? – roaima Dec 23 '20 at 23:33

Input for $f : example_cropped.pdf, output: example.pdf.

  • Option 1, your basename approach:

    echo $(basename "$f" _cropped.pdf).pdf
  • Option 2, sed

    sed 's/_cropped//' <<< "$f"
  • Option 3, rename - to be applied to files rather than strings. Will directly rename the file, use -n for dry run:

    rename 's/_cropped//' "$f"

rename may also be applied to multiple files in one go, maybe that is what you actually need.


In bash substitution is quite effective:

# file_cropped.pdf

echo ${v%_cropp*}.pdf
# file.pdf

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.