title: Extract name of a subdirectory from a path
body: <p>I would like to extract the subdirectory name using a bash script from a path like <code>/home/user1/subdir/foo_1/foo_2/.../foo_n</code>.
  <code>subdir</code> will always reside under <code>/home/user1</code> hence the prefix is invariant but there can be varied number of subdirectories under <code>subdir</code></p>
  
  <p>Some examples:</p>
  
  <ul>
  <li><p>/home/user1/dir1/something/like/this: RESULT: dir1</p></li>
  <li><p>/home/user1/dir2/different/this/time/: RESULT: dir2</p></li>
  <li>/home/user1/dir3/this/is/the/last/example: RESULT: dir3</li>
  </ul>
  
  <p>I wrote this regex but it doesn't work correctly:</p>
  
  <pre><code> expr /home/user1/subdir/foo_1/foo_2 : '\/home\/user1\/\(.*\/\)\{1\}'
  </code></pre>
  
  <p>, but it seems to give <code>subdir/foo_1</code> instead of <code>subdir</code>.</p>
  
  <p>Thanks!</p>