Menu

13 August 2007

How to copy all pdf from a folder and subfolder

Sometimes We get a big directory which has a lot of many subdirectories and so on, generally I use to copy a courses form websites. i use to copy full or part of a site (that include every files and folder) using wget command. but only pdf files are need mainly. So here is command which copy all the pdf files to a single directory.
here is the situation

|--->BIGFOLDER
|
--->BIGFOLDER/f1.pdf
|
--->BIGFOLDER/f2.pdf
|
--->BIGFOLDER/classes
|--->BIGFOLDER/classes/f3.jpg
|--->BIGFOLDER/classes/f4.pdf
|--->BIGFOLDER/classes/pdf.doc
|--->BIGFOLDER/classes/f5.ppt
|--->BIGFOLDER/Fun/
|--->BIGFOLDER/Fun/f6.jpg
|--->BIGFOLDER/Fun/f7.pdf
|--->BIGFOLDER/Fun/Movies/
|--->BIGFOLDER/Fun/Movies/f8.pdf
|--->BIGFOLDER/Fun/Movies/f9.avi
|--->BIGFOLDER/Fun/f10.pdf
now using command line go to the BIGFOLDER directory using cd command
then copy past this line

mkdir ../allpdf;find -name '*.pdf'|while read Filename ; do cp "$Filename" ../allpdf/ ; done
after applying we will get a new folder named allpdf parallel to the search folder. like this

|--->BIGFOLDER
|
--->BIGFOLDER/f1.pdf
|
--->BIGFOLDER/f2.pdf
|
--->BIGFOLDER/classes
|--->BIGFOLDER/classes/f3.jpg
|--->BIGFOLDER/classes/f4.pdf
|--->BIGFOLDER/classes/pdf.doc
|--->BIGFOLDER/classes/f5.ppt
|--->BIGFOLDER/Fun/
|--->BIGFOLDER/Fun/f6.jpg
|--->BIGFOLDER/Fun/f7.pdf
|--->BIGFOLDER/Fun/Movies/
|--->BIGFOLDER/Fun/Movies/f8.pdf
|--->BIGFOLDER/Fun/Movies/f9.avi
|--->BIGFOLDER/Fun/f10.pdf
|--->allpdf
|--->allpdf/f1.pdf
|--->allpdf/f2.pdf
|--->allpdf/f4.pdf
|--->allpdf/f7.pdf
|--->allpdf/f8.pdf
|--->allpdf/f10.pdf
If you want to copy all .ppt files you apply this command
mkdir ../allppt;find -name '*.ppt'|while read Filename ; do cp "$Filename" ../allppt/ ; done
I hope you got the logic if you want to search copy all .doc files.
now suppose you want to delete all .pdf files instead of copying then your command will be

find -name '*.pdf'|while read Filename ; do rm "$Filename" ; done

You may not want to delete .pdf files but you may want to delete all .ini files form some virus infection, as i have removed from my pen drive once and latter from a 80 GB harddisk of my friend when it was infected by virus.

If you know shell programming then you can add much more functionality to this little command.


Post a Comment