title: How to rename file based on parent and child folder name in bash script
body: <p>I would like to rename file based on parent/subparent directories name. </p>
  
  <p>For example: 
  test.xml file located at </p>
  
  <pre><code>/usr/local/data/A/20180101
  /usr/local/data/A/20180102
  /usr/local/data/B/20180101
  </code></pre>
  
  <p>how to save test.xml file in /usr/local/data/output as</p>
  
  <pre><code>A_20180101_test.xml
  A_20180102_test.xml
  b_20180101_test.xml
  </code></pre>
  
  <p>tried shall script as below but does not help.</p>
  
  <pre><code>#!/usr/bin/env bash
  
  target_dir_path="/usr/local/data/output"
  
  for file in /usr/local/data/*/*/test.xml; do
          l1="${file%%/*}"
          l2="${file#*/}"
          l2="${l2%%/*}"
          filename="${file##*/}"
          target_file_name="${l1}_${l2}_${filename}"
          echo cp "$file" "${target_dir_path}/${target_file_name}"
  done
  </code></pre>
  
  <p>Anything i am doing wrong in this shall script?</p>