LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command tofind files by extension through a directory tree, and copy them all to the (https://www.linuxquestions.org/questions/linux-newbie-8/command-tofind-files-by-extension-through-a-directory-tree-and-copy-them-all-to-the-736020/)

budword 06-27-2009 11:37 AM

command tofind files by extension through a directory tree, and copy them all to the
 
Ok, I should know how to do this already, but I don't, and google didn't help me find exactly what I want, so I'm here to pick the brains of people smarter than I.

I want to search down through a directory tree and find files with a certain extension, then cp or mv them all to the same directory.

For example. I want to find all the .txt documents in my home dir, and copy or move them all to my /home/me/doc dir. They could be in any of /home/me/text/words /home/me/text/sentences or /home/me/text/pdf.

I'm sure there is a simple command for this, but "ls | grep .txt | cp" type stuff I've tried doesn't work.

Thanks much for setting me straight.

David

GrapefruiTgirl 06-27-2009 11:54 AM

mv -f /home/me/text/*/*.txt /home/me/doc


Something pretty much like the above ought to work!

Sasha

Telengard 06-27-2009 01:48 PM

find can do what you want when used with the exec action.

Code:

man find
If the GNU documentation is on your system you can get even more details and examples with:

Code:

info find
As an example, I chose not to move any files because I don't know anything about your system. Instead, I'll show one way to find all C source files from the current directory down which use the SDL libraries.

Code:

find -iname '*.c' -exec grep -q SDL '{}' ';' -print
Note that even though the above command should be quite harmless, there may be security concerns connected with the exec action as explained in the GNU find documentation.


All times are GMT -5. The time now is 12:20 PM.