I have conf.yml files in different folders as below.


Now I want all the conf.yml to copy one folder like conf

Destination files should be like


Can you please suggest a script for it.

  • Can you please give me a basic idea of how to start with this problem – user118777 Jun 10 '15 at 9:57
  • Hi and welcome to the site. Please take a moment and look through our help center or take the tour. This is not a forum in the traditional sense and is not a free script-writing service either. We expect askers to expend some effort before asking here. So, please edit your question and show us what you've tried so far, what your google searches have shown you and which part, specifically, is giving you trouble. For example, I assume you know how to use the mv command, right? – terdon Jun 10 '15 at 10:23

You can use a simple wildcard pattern to enumerate the …/conf/conf.yml files. Use a for loop to iterate over the files.

for x in /var/*/conf/conf.yml; do …

You need to extract the part just after /var. You can do it with shell expansion features: prefix and suffix removal. Do it in two steps: remove the /var prefix, and remove the /conf/conf.yml suffix.

y=${x#/var}; y=${y%%/*}

Now glue it all together.

for x in /var/*/conf/conf.yml; do …
  y=${x#/var}; y=${y%%/*}
  cp -p "$x" "conf/${y}_conf.yml"

Note that the braces are necessary in ${y}_conf.yml, since $y_conf.yml would be parsed as “the value of y_conf followed by the text .yml”.

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.