If you are wondering how to copy files of a specific extension (e.g., .txt, .php, .java) recursively, the following guide can help you. By copying files recursively, I mean that you copy files located in the current directory as well as its sub-directories, to another location which has the same directory structure.
Your first (unsuccessful) attempt may be using cp command with "-r" option.
However, the above command will copy only *.txt files in the top-level current directory, but not in any of its sub-directories. So this is not the right way to do it.
A correct way to copy files recursively is to use cpio command, which is a Linux tool that copies files to and from archives. The command below demonstrates how to copy .txt files from one directory tree to another.
- "-p" option enables a recursive copy operation.
- "-d" option creates leading directories where needed.
- "-m" option retains previous file modification times while files are copied to the destination location.
Subscribe to Xmodulo
You can have daily Linux tutorials and FAQs delivered to your email inbox. Simply enter your email address below to subscribe to our mailing list. You will receive hands-on guides and carefully written tutorials related to Linux, everything for free.