How to move to the last subdirectory one by one?

Hi,

How can i traverse to the last subfolder in all the directories.
eg:
i have the below folders structure

f1/sf1/r1
f2/sf2/r2
f3/sf3/r3/r4

i need to move to the last directory in each directory.
Can anyone tell me a solution for this?

I saw an example that does that.

find . -type d | sort | awk '$0 !~ last "/" {print last} {last=$0} END{print last}'

I am not understanding what does last denote in the above example. Can anyone explain it

Whenever the branch changes, last (holding the line before) will hold the longest previous branch; so that and only that should be printed.

last is a variable, and holds the previous line.
If the current line does not match the previous line plus a / then print the previous line.
--
I think your script is not correct. The regular-expression match does not like special characters.
At the end of this web page there are some links.
From the "Drilling down to the last subdirecty"

find . -depth -type d | awk -F/ 'NF>=pnf {print} {pnf=NF}' 

It only saves the previous number of fields, and find -depth enforces processing of directories first, so you don't need to sort.