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
  • 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 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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.