title: Get the multilevel basename of a Path
body: <p>I am trying to write a program that is sort of <em>similar</em> to UNIX <code>basename</code>, except I can control the level of its base.</p>
  
  <p>For example, the program would perform tasks like the following:</p>
  
  <pre><code>$PROGRAM /PATH/TO/THE/FILE.txt 1
  FILE.txt # returns the first level basename
  
  $PROGRAM /PATH/TO/THE/FILE.txt 2
  THE/FILE.txt #returns the second level basename
  
  $ PROGRAM /PATH/TO/THE/FILE.txt 3
  TO/THE/FILE.txt #returns the third level base name
  </code></pre>
  
  <p>I was trying to write this in perl, and to quickly test my idea, I used the following command line script to obtain the second level basename, to no avail:</p>
  
  <pre><code>$echo "/PATH/TO/THE/FILE.txt" | perl -ne '$rev=reverse $_; $rev=~s:((.*?/){2}).*:$2:; print scalar reverse $rev'
  /THE
  </code></pre>
  
  <p>As you can see, it's only printing out the directory name and not the rest.
  I feel this has to do with nongreedy matching with quantifier or what not, but my knowledge lacks in that area. </p>
  
  <p>If there is more efficient way to do this in bash, please advise</p>