Combining FIND and -exec CP {} comands to change files w/in subdirectories


#1

Hello all,

First time posting here…

I am attempting to use the following code syntax to accomplish a simple task of locating a file ‘index123.html’ in multiple subdirectories and replacing it with a copy of a file in the current directory with the same name and documenting the completed task using an output.txt file to be placed in my /home/user/Documents/txt/ as a file named ‘outputofcp.txt’:

CODE:

find -name index123.html -exec cp {} index123.html + > /home/administrator/Documents/txt/outputofcp.txt

ERROR:

find: missing argument to ‘-exec’

QUESTION:

What am I missing? Is this the best syntax to accomplish the task as stated above? Are there alternatives that would produce more effective results?

BONUS QUESTION:

Is there a way to accomplish this task and integrate the actions and results directly into a GIT repository command of git add, git submit or the like?

What might that command syntax look like?

Thanks in advance to all who help in response!!!


#2

Hi,

Welcome to our forum.

In order to implement all your requirements, the easiest way would be to implement a simple bash for loop. Something like:

for i in $(find . -name index123.html); do cp -v /tmp/index123.html $i >> /home/administrator/Documents/txt/outputofcp.txt; git add $i; done

The above command will find all index123.html files in a current working directory recursively and replace them with a file located in /tmp/index123.html. Write a simple documentation file /home/administrator/Documents/txt/outputofcp.txt and git add all files in question. Depending whether you want to git push all files separately or one by one you can either add your git command inside a for loop by including another “;” or you can add another “;” after a done statement. However, the command is quite long so you better write some bash script such as:

#!/bin/bash

for i in $(find . -name index123.html); do
        cp -v /tmp/index123.html $i >>  /home/administrator/Documents/txt/outputofcp.txt
        git add $i
        # alternatively add your git push command here to submit files one by one
done

# push to git repo
git commit -m "message"
git push -u origin master

PS:
here is a corrected version of your find command:

find -name index123.html -exec cp -v index123.html {} \; >> /home/administrator/Documents/txt/outputofcp.txt

I hope you find my answer useful!

Lubos


#3

Thank you Lubos!!!

I used the corrected code syntax of:

find -name index123.html -exec cp -v index123.html {} ; >> /home/administrator/Documents/txt/outputofcp.txt

It worked just as I desired. The more complex syntax was definitely worth seeing and I will keep it as reference for future attempts. I did not attempt the git commit at this time but did find that the changed files were staged with the syntax above and I simply added the comment as I reviewed the list prior to commit.

Again, Thank you for your help in correcting my code syntax as well as offering the additional options.


#4

#5