0

Suppose that there is a tree directory main, and there are some subdirectories in main, e.g., sub1, sub2, sub3, etc. And there is a specific subdirectory named PDFs in main. How can I efficiently (maybe by writing a batch file) collect all the PDF in the subdirectories sub<i> and copy them into PDFs.

Stephen
  • 103

1 Answers1

1

In Terminal, run

cd main
find sub1 sub2 sub3 -type f -name '*.pdf' -exec cp '{}' /path/to/PDF \;

If the target directory is not within main you can also skip cd main and run find main -type f ... instead.

PS: If you want to move the files, use mv instead of cp. If you want have them in both places but not use twice the disk space, use ln to create hard links (two directory entries pointing to the same file).

nohillside
  • 100,768
  • If there are too many sub<i> directories, is there a method that I do not need to list them all in the terminal command?@nohillside – Stephen Dec 13 '22 at 10:01
  • @stephen Make sure the target directory is outside of the parent folder and run the command directly on the parent. – nohillside Dec 13 '22 at 10:07