Is it possible to pipe the results of find
to a COPY command cp
?
Like this:
find . -iname "*.SomeExt" | cp Destination Directory
Seeking, I always find this kind of formula such as from this post:
find . -name "*.pdf" -type f -exec cp {} ./pdfsfolder \;
This raises some questions:
|
pipe? isn't that what its for?-exec
|
?You use the cp command for copying files from one location to another. This command can also copy directories (folders). [file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.
The difference is that one uses a lowercase "R" and the other uses a capital "R". Beyond that, no difference. Same thing if you use the --recursive long option.
The cp command also copies entire directories into other directories if you specify the -r or -R flags. This copies the files, but not the directories, from the orders directory into the shipments directory.
cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name.
The flow between command execution is all because of pipes. Pipes can be used for finding files with a specific extension in the extracted list of ls command. This command will select the “Alex” from “record.txt” file, and in the terminal, it will print out the total number of occurrences of the pattern “Alex”.
When you use a BAT file to pipe a command's output to a text file, the exact same commands described above are used, but instead of pressing Enter to run them, you just have to open the .BAT file. Redirection operators work in batch files by including the command just as you would from the Command Prompt:
Re: Is it possible to "pipe" the output of one query to another? @CliveWatson Yes! The IN operator has done the trick and have added to my vocabulary.
CP belongs to GNU Core Utilities (short: “coreutils”). This package with the most important tools is available for free use. The basic function of CP is simple: copy one or more files to a location specified by the user. For this, two pieces of information are important: the name of the object and the destination of the action.
Good question!
- why cant you just use | pipe? isn't that what its for?
You can pipe, of course, xargs
is done for these cases:
find . -iname "*.SomeExt" | xargs cp Destination_Directory/
- Why does everyone recommend the -exec
The -exec
is good because it provides more control of exactly what you are executing. Whenever you pipe there may be problems with corner cases: file names containing spaces or new lines, etc.
- how do I know when to use that (exec) over pipe | ?
It is really up to you and there can be many cases. I would use -exec
whenever the action to perform is simple. I am not a very good friend of xargs
, I tend to prefer an approach in which the find
output is provided to a while
loop, such as:
while IFS= read -r result do # do things with "$result" done < <(find ...)
There's a little-used option for cp
: -t destination
-- see the man page:
find . -iname "*.SomeExt" | xargs cp -t Directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With