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 ...
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 ...
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
The existing answers are all decent, but have varying minor issues, which may or may not be problematic. Let's try to handle them.
*.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.)BOOKS/Romance
, BOOKS/Horror
for example.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.
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.
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
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.
cd /foo/bar; find ./ -name '*.pdf' -exec cp -fv '{}' ./BOOKS/ \;
– immortal squish
Jul 11 '18 at 22:59
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
cp $FILE
to cp "$FILE"
to prevent word-splitting, as @Doug mentions.
– wchargin
Jul 11 '18 at 19:21
cd /home/andrej/Prejemi for F in *.pdf do cp "$F" /home/andrej/Prejemi/KNJIGE done `
– andrej benedičič Jul 12 '18 at 18:10cd ~/Prejemi; cp *.pdf KNJIGE/
seems clearly much better. It is simpler, using strictly fewer constructs. It is easier to read and more idiomatic. And it only invokes cp
once, so it will be faster with lots of files.
– wchargin
Jul 13 '18 at 02:47
find -exec
or find -print0 | xargs -0
is a better solution than a for
loop.)
– wchargin
Jul 13 '18 at 02:48
\Download\BOOKS\
is... wrong. Is Download/BOOKS a subdirectory under your home directory? – RonJohn Jul 12 '18 at 01:20