Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

i need to copy all txt files included in a lot of subdirectories into a single directory. Example:

 MyDir
   Dir1
     1.txt
     2.txt
   Dir2
     a.txt
     b.txt
     e.jpg
   Dir3 
     f.txt
     k.txt
  MyDestination

How to copy all txt to MyDestination ?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Try using find ...

find /MyDir -name '*.txt' -exec cp -t /MyDestination "{}" +

which will find the relevant txt files and pass them in batches to cp to have it copy them to MyDestination.

share|improve this answer

What about

cp -R *.txt <destination>

share|improve this answer
3  
This won't work because the *.txt is expanded by the shell, not by cp. This will copy any top level .txt files as well as recursively copy any top level directories which match *.txt. –  mgorven May 12 '12 at 18:43
    
Ah ok, thanks for clearing that up! :) –  Dean Perry May 12 '12 at 18:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.