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.
2 comments:
oh WOW!!! NAren this script which I found on website was quite helpful. I always used 2 use the old custom ways of copyin but this makes life so simpler
I am quite AMAZED!!!!
Vitesh
You need not fiddle with the shell for that. Its a simple one liner if you use xargs:
find [directory] -name 'pattern' | xargs -i cp {} [target directory]
ex:
find docs/ -name '*.pdf' | xargs -i cp {} ../pdfs
This line copies all pdf files from directory "docs" to directory "pdfs" in the parent directory.
You could also use -I option to make it more clear what is happening.
Oh btw to know what is happening go for the man pages
But yes the shell is infinitely more powerful than any one liner can ever be.
Post a Comment