Mint Mint - 3 months ago 9
Bash Question

Match folder name from url using regex

I want to match just the folder name that a file is in,

eg:

pic/2009/cat01.jpg

pic/2009/01/cat02.jpg

I want to just match what I put in bold.

So far I have this:

[^/]*/


Which will match,

pic/2009/cat01.jpg

Any idea?

Answer

Without using a regular expression:

FILE_NAME="pic/2009/cat01.jpg"
basename $(dirname $FILE_NAME)

dirname gets the directory part of the path, basename prints the last part.