1

I have a large folder with subfolders of pdf files. I want to get all the pdf files (290 of them) and get them into one directory. (I thought that would be simple.) so that I can concatenate them into one document with my pdf program pdfshuffle, or something like that. I got the files path and names into a file: out.txt, using: find $(pwd) -iname '*.PDF' > out.txt out.txt contains paths and files like this:

...
/home/user/Downloads/pedals/PEDALS/Electro-Harmonix/Small Stone Phaser.PDF
/home/user/Downloads/pedals/PEDALS/Electro-Harmonix/Dr. Quack (Doctor Q Melhorado).pdf
/home/user/Downloads/pedals/PEDALS/Marshall/Guv'Nor 1.PDF
/home/user/Downloads/pedals/PEDALS/Marshall/Guv'Nor 2.pdf
...

cool. so then I thought I could do this:

#!/bin/bash
input="out.txt"
while IFS= read -r line
do
  #echo "$line"
cp $line .
done < "$input"

But it only coppies about 36 of the pdfs, and the shell says it cannot stat files:

...
cp: cannot stat 'Boogie/Vtwin.pdf': No such file or directory
cp: cannot stat '/home/user/Downloads/pedals/PEDALS/Mesa': No such file or directory
...

The file is clearly there, and I can view it etc. whats the problem with my method or script?

3
  • 1
    is it becuase the filenames have spaces in them? Im looking at the files that were copied, and it seems none of them have spaces in the filenames.
    – j0h
    Jan 2, 2021 at 22:17
  • 3
    Yes, the unquoted $line will be subject to word splitting on the default (whitespace) IFS - the IFS= assignment only unsets IFS for the read command itself Jan 2, 2021 at 22:17
  • cool, that was it. i did cp "$line" . and it worked.
    – j0h
    Jan 2, 2021 at 22:21

1 Answer 1

1

Handling filenames containing spaces or other funny characters is easily done, using find and xargs. Read man find xargs and do something like

find . -mindepth 1 -type f -iname '*.pdf' -print0 | \
    xargs -0 -r echo mv -t $PWD

to verify commands, and when they correspond to expectations...
remove the "echo" to actually execute the commands.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .