WildCard Find Replace CP












2















I'm trying to replace files that match a particular pattern* on CentOS, but it is not working.



I initially wrote:



cp example.com/(*)/orm-mysql/build/conf/(*)-conf.live.php example_dev.com/$1/orm-mysql/build/conf/$1-conf.live.php


I want to replace the files in the example_dev.com directory with the files of the same name in example.com.



*The pattern is example.com/ then anything until /orm-mysql/build/conf/ then a file prefixed with the first wildcarded value and ending in -conf.live.php.



I tried using find with exec:



find /var/www/html/example.com -name '*/orm-mysql/build/conf/*-conf.live.php' -exec echo {} ;


but it threw an error:




find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ‘*/orm-mysql/build/conf/*-conf.live.php’' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ‘*/orm-mysql/build/conf/*-conf.live.php’'




Which I think is because -name is for a file, not a path.



How can I accomplish this, is there an easier way than writing a shell script?



Some examples:



/var/www/html/example.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example.com/audio/orm-mysql/build/conf/audio-conf.live.php


and they would replace:



/var/www/html/example_dev.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example_dev.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example_dev.com/audio/orm-mysql/build/conf/audio-conf.live.php









share|improve this question

























  • Did you try the suggestions from the error message? Also, why are you using parentheses around the *? That wouldn't really work anyway. Could you give us some example file names? I assume that the $1 you are using, although not recognized by cp/shell, means that the file name also has the directory name in it, is that correct?

    – terdon
    Jan 7 at 14:49













  • I tried the wholename and samefile but got the same error. I'm not sure what I'm suppose to put in place of the ... in their example

    – user3783243
    Jan 7 at 14:54
















2















I'm trying to replace files that match a particular pattern* on CentOS, but it is not working.



I initially wrote:



cp example.com/(*)/orm-mysql/build/conf/(*)-conf.live.php example_dev.com/$1/orm-mysql/build/conf/$1-conf.live.php


I want to replace the files in the example_dev.com directory with the files of the same name in example.com.



*The pattern is example.com/ then anything until /orm-mysql/build/conf/ then a file prefixed with the first wildcarded value and ending in -conf.live.php.



I tried using find with exec:



find /var/www/html/example.com -name '*/orm-mysql/build/conf/*-conf.live.php' -exec echo {} ;


but it threw an error:




find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ‘*/orm-mysql/build/conf/*-conf.live.php’' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ‘*/orm-mysql/build/conf/*-conf.live.php’'




Which I think is because -name is for a file, not a path.



How can I accomplish this, is there an easier way than writing a shell script?



Some examples:



/var/www/html/example.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example.com/audio/orm-mysql/build/conf/audio-conf.live.php


and they would replace:



/var/www/html/example_dev.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example_dev.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example_dev.com/audio/orm-mysql/build/conf/audio-conf.live.php









share|improve this question

























  • Did you try the suggestions from the error message? Also, why are you using parentheses around the *? That wouldn't really work anyway. Could you give us some example file names? I assume that the $1 you are using, although not recognized by cp/shell, means that the file name also has the directory name in it, is that correct?

    – terdon
    Jan 7 at 14:49













  • I tried the wholename and samefile but got the same error. I'm not sure what I'm suppose to put in place of the ... in their example

    – user3783243
    Jan 7 at 14:54














2












2








2








I'm trying to replace files that match a particular pattern* on CentOS, but it is not working.



I initially wrote:



cp example.com/(*)/orm-mysql/build/conf/(*)-conf.live.php example_dev.com/$1/orm-mysql/build/conf/$1-conf.live.php


I want to replace the files in the example_dev.com directory with the files of the same name in example.com.



*The pattern is example.com/ then anything until /orm-mysql/build/conf/ then a file prefixed with the first wildcarded value and ending in -conf.live.php.



I tried using find with exec:



find /var/www/html/example.com -name '*/orm-mysql/build/conf/*-conf.live.php' -exec echo {} ;


but it threw an error:




find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ‘*/orm-mysql/build/conf/*-conf.live.php’' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ‘*/orm-mysql/build/conf/*-conf.live.php’'




Which I think is because -name is for a file, not a path.



How can I accomplish this, is there an easier way than writing a shell script?



Some examples:



/var/www/html/example.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example.com/audio/orm-mysql/build/conf/audio-conf.live.php


and they would replace:



/var/www/html/example_dev.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example_dev.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example_dev.com/audio/orm-mysql/build/conf/audio-conf.live.php









share|improve this question
















I'm trying to replace files that match a particular pattern* on CentOS, but it is not working.



I initially wrote:



cp example.com/(*)/orm-mysql/build/conf/(*)-conf.live.php example_dev.com/$1/orm-mysql/build/conf/$1-conf.live.php


I want to replace the files in the example_dev.com directory with the files of the same name in example.com.



*The pattern is example.com/ then anything until /orm-mysql/build/conf/ then a file prefixed with the first wildcarded value and ending in -conf.live.php.



I tried using find with exec:



find /var/www/html/example.com -name '*/orm-mysql/build/conf/*-conf.live.php' -exec echo {} ;


but it threw an error:




find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name ‘*/orm-mysql/build/conf/*-conf.live.php’' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ‘*/orm-mysql/build/conf/*-conf.live.php’'




Which I think is because -name is for a file, not a path.



How can I accomplish this, is there an easier way than writing a shell script?



Some examples:



/var/www/html/example.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example.com/audio/orm-mysql/build/conf/audio-conf.live.php


and they would replace:



/var/www/html/example_dev.com/video/orm-mysql/build/conf/video-conf.live.php
/var/www/html/example_dev.com/images/orm-mysql/build/conf/images-conf.live.php
/var/www/html/example_dev.com/audio/orm-mysql/build/conf/audio-conf.live.php






find wildcards pattern-matching






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 7 at 14:55







user3783243

















asked Jan 7 at 14:46









user3783243user3783243

1275




1275













  • Did you try the suggestions from the error message? Also, why are you using parentheses around the *? That wouldn't really work anyway. Could you give us some example file names? I assume that the $1 you are using, although not recognized by cp/shell, means that the file name also has the directory name in it, is that correct?

    – terdon
    Jan 7 at 14:49













  • I tried the wholename and samefile but got the same error. I'm not sure what I'm suppose to put in place of the ... in their example

    – user3783243
    Jan 7 at 14:54



















  • Did you try the suggestions from the error message? Also, why are you using parentheses around the *? That wouldn't really work anyway. Could you give us some example file names? I assume that the $1 you are using, although not recognized by cp/shell, means that the file name also has the directory name in it, is that correct?

    – terdon
    Jan 7 at 14:49













  • I tried the wholename and samefile but got the same error. I'm not sure what I'm suppose to put in place of the ... in their example

    – user3783243
    Jan 7 at 14:54

















Did you try the suggestions from the error message? Also, why are you using parentheses around the *? That wouldn't really work anyway. Could you give us some example file names? I assume that the $1 you are using, although not recognized by cp/shell, means that the file name also has the directory name in it, is that correct?

– terdon
Jan 7 at 14:49







Did you try the suggestions from the error message? Also, why are you using parentheses around the *? That wouldn't really work anyway. Could you give us some example file names? I assume that the $1 you are using, although not recognized by cp/shell, means that the file name also has the directory name in it, is that correct?

– terdon
Jan 7 at 14:49















I tried the wholename and samefile but got the same error. I'm not sure what I'm suppose to put in place of the ... in their example

– user3783243
Jan 7 at 14:54





I tried the wholename and samefile but got the same error. I'm not sure what I'm suppose to put in place of the ... in their example

– user3783243
Jan 7 at 14:54










1 Answer
1






active

oldest

votes


















2














I would actually do this with a shell loop:



for dir in example.com/*; do
prefix=${dir##*/}
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php
example_dev.com/"$prefix"/orm-mysql/build/conf/$1-conf.live.php
done


That will complain if the target directory doesn't exist, but will work as expected if it does. If you want to avoid the complaints, use this:



for dir in example.com/*; do
prefix=${dir##*/}
target=example_dev.com/"$prefix"/orm-mysql/build/conf/
if [ -d "$target" ]; then
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php "$target"
fi
done





share|improve this answer


























  • When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

    – user3783243
    Jan 7 at 15:06













  • @user3783243 ah sorry, my bad. See updated answer. The echo should work now.

    – terdon
    Jan 7 at 15:11











  • Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

    – user3783243
    Jan 7 at 15:16






  • 1





    @user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

    – terdon
    Jan 7 at 15:20











  • Perfect, thanks.

    – user3783243
    Jan 7 at 15:26











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493009%2fwildcard-find-replace-cp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I would actually do this with a shell loop:



for dir in example.com/*; do
prefix=${dir##*/}
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php
example_dev.com/"$prefix"/orm-mysql/build/conf/$1-conf.live.php
done


That will complain if the target directory doesn't exist, but will work as expected if it does. If you want to avoid the complaints, use this:



for dir in example.com/*; do
prefix=${dir##*/}
target=example_dev.com/"$prefix"/orm-mysql/build/conf/
if [ -d "$target" ]; then
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php "$target"
fi
done





share|improve this answer


























  • When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

    – user3783243
    Jan 7 at 15:06













  • @user3783243 ah sorry, my bad. See updated answer. The echo should work now.

    – terdon
    Jan 7 at 15:11











  • Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

    – user3783243
    Jan 7 at 15:16






  • 1





    @user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

    – terdon
    Jan 7 at 15:20











  • Perfect, thanks.

    – user3783243
    Jan 7 at 15:26
















2














I would actually do this with a shell loop:



for dir in example.com/*; do
prefix=${dir##*/}
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php
example_dev.com/"$prefix"/orm-mysql/build/conf/$1-conf.live.php
done


That will complain if the target directory doesn't exist, but will work as expected if it does. If you want to avoid the complaints, use this:



for dir in example.com/*; do
prefix=${dir##*/}
target=example_dev.com/"$prefix"/orm-mysql/build/conf/
if [ -d "$target" ]; then
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php "$target"
fi
done





share|improve this answer


























  • When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

    – user3783243
    Jan 7 at 15:06













  • @user3783243 ah sorry, my bad. See updated answer. The echo should work now.

    – terdon
    Jan 7 at 15:11











  • Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

    – user3783243
    Jan 7 at 15:16






  • 1





    @user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

    – terdon
    Jan 7 at 15:20











  • Perfect, thanks.

    – user3783243
    Jan 7 at 15:26














2












2








2







I would actually do this with a shell loop:



for dir in example.com/*; do
prefix=${dir##*/}
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php
example_dev.com/"$prefix"/orm-mysql/build/conf/$1-conf.live.php
done


That will complain if the target directory doesn't exist, but will work as expected if it does. If you want to avoid the complaints, use this:



for dir in example.com/*; do
prefix=${dir##*/}
target=example_dev.com/"$prefix"/orm-mysql/build/conf/
if [ -d "$target" ]; then
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php "$target"
fi
done





share|improve this answer















I would actually do this with a shell loop:



for dir in example.com/*; do
prefix=${dir##*/}
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php
example_dev.com/"$prefix"/orm-mysql/build/conf/$1-conf.live.php
done


That will complain if the target directory doesn't exist, but will work as expected if it does. If you want to avoid the complaints, use this:



for dir in example.com/*; do
prefix=${dir##*/}
target=example_dev.com/"$prefix"/orm-mysql/build/conf/
if [ -d "$target" ]; then
cp "$dir"/orm-mysql/build/conf/"$prefix"-conf.live.php "$target"
fi
done






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 7 at 15:11

























answered Jan 7 at 14:57









terdonterdon

129k32253428




129k32253428













  • When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

    – user3783243
    Jan 7 at 15:06













  • @user3783243 ah sorry, my bad. See updated answer. The echo should work now.

    – terdon
    Jan 7 at 15:11











  • Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

    – user3783243
    Jan 7 at 15:16






  • 1





    @user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

    – terdon
    Jan 7 at 15:20











  • Perfect, thanks.

    – user3783243
    Jan 7 at 15:26



















  • When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

    – user3783243
    Jan 7 at 15:06













  • @user3783243 ah sorry, my bad. See updated answer. The echo should work now.

    – terdon
    Jan 7 at 15:11











  • Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

    – user3783243
    Jan 7 at 15:16






  • 1





    @user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

    – terdon
    Jan 7 at 15:20











  • Perfect, thanks.

    – user3783243
    Jan 7 at 15:26

















When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

– user3783243
Jan 7 at 15:06







When I run echo $prefix I don't get anything back but echo $dir gives me back the full path. I didn't try it with the cp yet, is it not accessible via echo (and if not is there some way to see the command prior to running it)?

– user3783243
Jan 7 at 15:06















@user3783243 ah sorry, my bad. See updated answer. The echo should work now.

– terdon
Jan 7 at 15:11





@user3783243 ah sorry, my bad. See updated answer. The echo should work now.

– terdon
Jan 7 at 15:11













Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

– user3783243
Jan 7 at 15:16





Thanks, that works. What does the ${dir##*/} do, or what is the phrasing for that so I can read more about it?

– user3783243
Jan 7 at 15:16




1




1





@user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

– terdon
Jan 7 at 15:20





@user3783243 see "Substring Removal" here: tldp.org/LDP/abs/html/string-manipulation.html

– terdon
Jan 7 at 15:20













Perfect, thanks.

– user3783243
Jan 7 at 15:26





Perfect, thanks.

– user3783243
Jan 7 at 15:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493009%2fwildcard-find-replace-cp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List directoties down one level, excluding some named directories and files

list processes belonging to a network namespace

list systemd RuntimeDirectory mounts