I have a lot of directories that contain some text files and I would copy all files (of all directories) in a new directory. I'm looking for a bash command that do this. How can I do it?

  • No need to take care of name clashes? Is the target dir flat? – Jacob Vlijm May 29 '16 at 20:25
  • No, every directory has different file names – linofex May 29 '16 at 20:27


find source_dir '*.txt' -exec cp -vuni '{}' dest_dir ";"
  • I don't understand the sintax, should "dest_dir" be without inverted commas? – linofex May 29 '16 at 20:31
  • Without, but I think it does not matter (have only tried it without, though) – rocket_doge_ May 29 '16 at 20:34
  • Please let me know if it worked ^^ – rocket_doge_ May 29 '16 at 20:43
  • The command worked in part. It copied all the files (.cc and executable included) – linofex May 29 '16 at 20:48

You can use the this command to copy all the files and subfolders in a directory to another directory:

cp -a /source/. /dest/

Taken from How can I copy the contents of a folder to another folder in a different directory using terminal?

Note: This does not solve OP's question as you cannot use it to only copy files of a certain type along with folders, but I'll leave it here for anyone that might need it.

  • what about copy only .txt files? Have I to change the command like this : cp -a /source/*.txt /dest/ ? – linofex May 29 '16 at 20:32
  • Yes, that should work. I'll run a quick test and get back to you in a couple mins. – palmerito0 May 29 '16 at 20:34
  • I'm sorry but that isn't working. I guess the command I gave you will only work to copy ALL files and folders. Adding *.txt makes it no longer copy the directories. I'll edit my answer accordingly. – palmerito0 May 29 '16 at 20:40
  • Yes, I tried the command and didn't work. thank you anyway! – linofex May 29 '16 at 20:47

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