# I want to copy all pdf files into another directory [duplicate]

andrej benedičič 07/11/2018. 6 answers, 954 views

Linux users! I'm having a issue: I have all pdf files in directoryDownloads, but I want it to copy all the files in subfolder \Download\BOOKS\. How would I copy all files with for loop?

for files in ...

Kulfy 07/11/2018.

I don't think that you require for loop just for copying files. You can copy files using:

cp /home/username/Downloads/*.pdf /home/username/Downloads/BOOKS

But if you still want to use for loop, you can also write the below code:

cd /home/username/Downloads
for F in *.pdf
do
cp "$F" /home/username/Downloads/BOOKS done Kevin 07/11/2018. not with a for loop but this should work to move the files. mv /home/username/Downloads/*.pdf /home/username/Downloads/BOOKS Use this to make a copy of the files: cp /home/username/Downloads/*.pdf /home/username/Downloads/BOOKS studog 07/11/2018. The existing answers are all decent, but have varying minor issues, which may or may not be problematic. Let's try to handle them. 1. Is *.pdf correct for all the files? I find PDFs downloaded from the internet often are *.PDF instead. (I suspect certain case-insensitive filesystems cause this.) 2. Are all the PDFs in the BOOKS subdirectory directly, or are there subdirectories? BOOKS/Romance, BOOKS/Horror for example. 3. Do the PDFs have file extensions at all? Sometimes they don't. 4. Are any of the files symbolically linked? Unlikely, but not hard to handle. 5. Already pointed out but bears repeating: Do any of the filenames contain spaces or other odd characters? Spaces are fairly common for human-readable-ness. The strategy is to use find to locate potential PDFs, file to filter only actual PDFs, and then act on those filenames. find -L books -maxdepth 1 -print0 | xargs -0 -n1 -I{} sh -c '[ "$(file -b --mime-type "{}")" = "application/pdf" ] && cp "{}" Download/'

find -L Download/BOOKS -maxdepth 1 -print0 will locate all the filesystem entries in the subdirectory tree starting at Download/BOOKS, following symbolic links if needed, to a maximum depth. Increase the depth number to handle subdirectories, or remove -maxdepth 1 to handle the whole tree.

xargs -0 -n1 -I{} takes multiple lines of input and runs a command repeatedly with 1 input line.

sh -c '[ "$(file -b --mime-type "{}")" = "application/pdf" ] && cp "{}" Download/' makes a new shell to run a test on the filesystem entry, and if it is a PDF (as determined by file extension and file contents) make a copy of it to the Download/ directory. Using find takes care of 1., 2. and 4. Using file takes care of 3. Using the -print0 and -0 arguments and careful quoting takes care of 5. If you really needed a loop because you want to run other commands on each PDF, replace cp "{}" Download/ with whatever is approriate. The {} is a pattern that xargs will replace with each filename as it runs. Trasicio Maina 07/11/2018. I don't know about for loops (unless you write a C program to achieve this) but this line should do it:  cp -v ~/Downloads/*.pdf ~/Downloads/BOOKS/ The CP is copy and the * is for wildcard to select anything ending in .pdf in that directory. Bernard Wei 07/12/2018. A simple for loop should be able to do what you need, i.e. copy all PDF files under Downloads (including subdirectories) in to Downloads/BOOKS cd Downloads IFS=$(echo -en "\n\b") && for n in find . -path ./BOOKS -prune -o -name "*.pdf" -print; do cp -fv "$n" ./BOOKS; done Note that the single quote for the find statement is the backward single quote, not the normal one. Edited: Internal field separator needed for spaces in filenames. Ogre55 07/11/2018. A simple for loop will work, but is much more complicated than it needs to be. for FILE in ~/Downloads/BOOKS/*.pdf; do cp "$FILE" /new/directory/
done

This will copy all files in the BOOKS directory to /new/directory