Linux command output as a parameter of another com

2019-02-03 18:25发布

问题:

I would like to pass the output list of elements of a command as a parameter of another command. I have found some other pages:

  • How to display the output of a Linux command on stdout and also pipe it to another command?
  • Use output of bash command (with pipe) as a parameter for another command

but they seem to be more complex.

I just would like to copy a file to every result of a call to the Linux find command.

What is wrong here?:

find . -name myFile 2>&1 | cp /home/myuser/myFile $1

Thanks

回答1:

This is what you want:

find . -name myFile -exec cp /home/myuser/myFile {} ';'

A breakdown / explanation of this:

  • find: invoking the find command
  • .: start search from current working directory.
  • Since no depth flags are specified, this will search recursively for all subfolders
  • -name myFile: find files with the explicit name myFile
  • -exec: for the search results, perform additional commands with them
  • cp /home/myuser/myFile {}: copies /home/myuser/myFile to overwrite each result returned by find to ; think of {} as where each search result goes.
  • ';': used to separate different commands to be run after find


回答2:

There are a couple of ways to solve this, depending on whether you need to worry about files with spaces or other special characters in their names.

If none of the filenames have spaces or special characters (they consist only of letters, numbers, dashes, and underscores), then the following is a simple solution that will work. You can use $(command) to execute a command, and substitute the results into the arguments of another command. The shell will split the result on spaces, tabs, or newlines, and for assign each value to $f in turn, and run the command on each value.

for f in $(find . -name myFile)
do
    cp something $f
done

If you do have spaces or tabs, you could use find's -exec option. You pass -exec command args, putting {} where you want the filename to be substituted, and ending the arguments with a ;. You need to quote the {} and ; so that the shell doesn't interpret them.

find . -name myFile -exec cp something "{}" \;

Sometimes -exec is not sufficient. For example, in this question, they wanted to use Bash parameter expansion to compute the filename. In order to do that, you need to pass -exec bash -c 'your command', but then you will run into quoting problems with the {} substitution. To solve this, you can use -print0 from find to print the results delimited with null characters (which are invalid in filenames), and pipe it to a while read loop that splits parameters on nulls:

find . -name myFile -print0 | (while read -d $'\0' f; do
    cp something "$f"
  done)


回答3:

The pipe will send the output of one program to the input of another. cp does not read from its input stream at the terminal, it merely uses the arguments on the command line.

You want to either use xargs with the pipe or find's exec argument instead of pipes.