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

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

This question already has an answer here:

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 ...

6 Answers

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
   cp "$F" /home/username/Downloads/BOOKS

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/

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

HighResolutionMusic.com - Download Hi-Res Songs

1 Katy Perry

365 flac

Katy Perry. 2019. Writer: Zedd;Katy Perry;Caroline Ailin;Corey Sanders;Daniel Davidsen;Cutfather;Peter Wallevik.
2 5 Seconds Of Summer

Who Do You Love flac

5 Seconds Of Summer. 2019. Writer: Andrew Taggart;Talay Riley;Oak;Sean Douglas;Luke Hemmings;Calum Hood;Ashton Irwin;Michael Clifford;Trevorious;Zaire Koalo.

Happy flac

DEAMN. 2019.
4 Alec Benjamin

Let Me Down Slowly flac

Alec Benjamin. 2019. Writer: Alec Benjamin;Sir Nolan;Michael Pollack.
5 Ariana Grande

7 Rings flac

Ariana Grande. 2019. Writer: Ariana Grande;Richard Rodgers;TBHits;Njomza;Michael "Mikey" Foster;Kaydence;Tayla Parx;Scootie;Oscar Hammerstein II;Victoria Monét.
6 Brooks

Better When You're Gone flac

Brooks. 2019. Writer: David Guetta;Emma Lov Block;Ido Zmishlany;Jackson Foote;Jeremy Dussolliet;Brooks.
7 Bruno Mars

Please Me flac

Bruno Mars. 2019. Writer: J. White Did It;DJ White Shadow;Philip Lawrence;Bruno Mars;Cardi B.
8 Dimitri Vegas & Like Mike

Selfish flac

Dimitri Vegas & Like Mike. 2019. Writer: Victor Thell;Michael Thivaios;Maria Smith;Jeff Porcaro;Dimitri Thivaios;David Paich;Dan Book;Will Grands;ANGEMI.
9 Avril Lavigne

Dumb Blonde flac

Avril Lavigne. 2019. Writer: Mitch Allan;Bonnie McKee;Nicki Minaj;Avril Lavigne.
10 Halsey

11 Minutes flac

Halsey. 2019. Writer: Matt Schwartz;Halsey;Dominic Harrison;Brynley Plumb.
11 Mike Perry

Way Too High flac

Mike Perry. 2019. Writer: Dimitri Vangelis;Andreas Wiman;Sabrina Bernstein;Pär Westerlund;Mike Perry.

Pretend flac

CNCO. 2019. Writer: Luis Daniel Nieves;Diane Warren;Britten Newbill;Rykeyz;Lucky Daye.
13 Ariana Grande

Needy flac

Ariana Grande. 2019. Writer: Tayla Parx;TBHits;Victoria Monét;Ariana Grande.
14 Ariana Grande

Break Up With Your Girlfriend, I'm Bored flac

Ariana Grande. 2019. Writer: Kandi;Kevin "She'kspere" Briggs;Max Martin;ILYA;Savan Kotecha;Ariana Grande.
15 Armin Van Buuren

Lonely For You flac

Armin Van Buuren. 2019. Writer: Armin Van Buuren;Bonnie McKee;Eden XO;Benno De Goeij.
16 Icona Pop

This Is How We Party flac

Icona Pop. 2019. Writer: R3hab;Icona Pop.
17 Lauv

I'm So Tired flac

Lauv. 2019. Writer: Lauv;Michael Pollack;Troye Sivan;Leland;OZGO.
18 Normani

Dancing With A Stranger flac

Normani. 2019. Writer: Mikkel S. Eriksen;Tor Hermansen;Jimmy Napes;Normani;Sam Smith.
19 Hozier

Dinner & Diatribes flac

Hozier. 2019. Writer: Hozier.
20 Afrojack

Ferrari flac

Afrojack. 2019.

Related questions

Hot questions


Popular Tags