Pouya
Pouya

Reputation: 109

What does this command Actually do? Is it correct?

I found this command line when I was checking a Bash Script! My question is what does this command do and is this command correct?

    find / -name "*.src" | xargs cp ~/Desktop/Log.txt

Upvotes: 0

Views: 97

Answers (3)

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11593

What the command is

find / -name "*.src"

Explanation: Recursively find all regular files, directories, and symlinks in /, for which the filename ends in .src

|

Explanation: Redirect stdout from the command on the left side of the pipe to stdin of the command on the right side

xargs cp ~/Desktop/Log.txt

Explanation: Build a cp command, taking arguments from stdin and appending them into a space-delimited list at the end of the command. If the pre-defined buffer space of xargs (generally bounded by ARG_MAX) is exhausted multiple cp commands will be executed,

e.g. xargs cp arg100 .... arg900 could be processed as cp a100 ... a500; cp a501 ... a900

Note The behavior of your cp varies a lot depending on its arguments

  • If ~/Desktop/Log.txt is the only argument, cp will throw stderr
  • If the last argument to cp is a directory
    • All preceding arguments that are regular files will be copied into it
    • Nothing will happen for all preceding arguments that are directories, except stderr will be thrown
  • If the last argument is a regular file
    • If there are 2 total arguments to cp and the first one is a regular file. Then the contents of the second argument file will be overwritten by the contents of the first argument
    • If there are more than 2 total arguments, cp will throw a stderr

So all in all, there are too many variables here for the behavior of your command to really ever be precisely defined. As such, I suspect, you wanted to do something else.

What it probably should have been

My guess is the author of the command probably wanted to redirect stdout to a log file (note the log file will be overwritten each time you run the command)

find / -name "*.src" > ~/Desktop/Log.txt

Additionally, if you are just looking for regular files with the .src extension, you should also add the -type f option to find

find / -type f -name "*.src" > ~/Desktop/Log.txt

Upvotes: 2

RBH
RBH

Reputation: 592

find the files or directory with .src extension in / and copy file ~/Desktop/Log.txt as the find result's filename

for example if the output of the find command is

file.src
directory1.src
file2.src 

xargs command will execute cp ~/Desktop/Log.txt file.src directory1.src file2.src which does not make any sense.

Upvotes: 3

jherran
jherran

Reputation: 3367

It finds every file or directory that match the pattern *.src from /.

Then copy the file ~/Desktop/Log.txt to every result of previous command.

Upvotes: 0

Related Questions