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?
Asked
Active
Viewed 309 times
0
-
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
2 Answers
0
Try
find source_dir '*.txt' -exec cp -vuni '{}' dest_dir ";"

rocket_doge_
- 736
-
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
-
-
The command worked in part. It copied all the files (.cc and executable included) – linofex May 29 '16 at 20:48
0
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. – carreter 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. – carreter May 29 '16 at 20:40
-