How to move or copy files listed by 'find' command in unix?
I have a list of certain files that I see using the command below, but how can I copy those files listed into another folder, say ~/test?
find . -mtime 1 -exec du -hc {} +
Solution 1:
Adding to Eric Jablow's answer, here is a possible solution (it worked for me - linux mint 14 /nadia)
find /path/to/search/ -type f -name "glob-to-find-files" | xargs cp -t /target/path/
You can refer to "How can I use xargs to copy files that have spaces and quotes in their names?" as well.
Solution 2:
Actually, you can process the find
command output in a copy command in two ways:
-
If the
find
command's output doesn't contain any space, i.e if the filename doesn't contain a space in it, then you can use:Syntax: find <Path> <Conditions> | xargs cp -t <copy file path> Example: find -mtime -1 -type f | xargs cp -t inner/
-
But our production data files might contain spaces, so most of time this command is effective:
Syntax: find <path> <condition> -exec cp '{}' <copy path> \; Example find -mtime -1 -type f -exec cp '{}' inner/ \;
In the second example, the last part, the semi-colon is also considered as part of the find
command, and should be escaped before pressing Enter. Otherwise you will get an error something like:
find: missing argument to `-exec'