Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pipe the results of FIND to a COPY command CP?

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:

  1. Why cant you just use | pipe? isn't that what its for?
  2. Why does everyone recommend the -exec
  3. How do I know when to use that (exec) over pipe |?
like image 982
Paul Avatar asked Oct 07 '14 10:10

Paul


People also ask

When using the cp command you can copy files?

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.

What is difference between cp and copy command?

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.

Can you copy a directory using cp command?

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.

Is cp copy in Linux?

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.

How to use pipes in Linux to find a specific file?

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”.

How do I pipe a command output to a text file?

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:

Is it possible to pipe the output of one query to another?

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.

What is CPCP and how to use it?

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.


2 Answers

Good question!

  1. 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/ 
  1. 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.

  1. 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 ...) 
like image 79
fedorqui 'SO stop harming' Avatar answered Sep 22 '22 14:09

fedorqui 'SO stop harming'


There's a little-used option for cp: -t destination -- see the man page:

find . -iname "*.SomeExt" | xargs cp -t Directory 
like image 40
glenn jackman Avatar answered Sep 20 '22 14:09

glenn jackman