Rails one-liner to get URLs from rake routes
Earlier today I needed to get a list of URL paths that my Rails app provided, but I didn’t need all the extra cruft that you get with
rake routes
So I created a bash one-liner with sed, awk, uniq, and sort to get what I needed out of rake routes. The one liner looks like:
rake routes | sed -e "1d" -e "s,^[^/]*,,g" | awk '{print $1}' | sort | uniq
Let’s look at what this is doing:
- First we get the list of routes:
rake routes
- Next we pipe the result to sed, which does two things:
- Deletes the first line of the output:
sed -e "1d"
- Then it removes every character from the beginning of each line (^) that is not a forward-slash ([^/]*) and removes it:
-e "s,^[^/]*,,g"
- Deletes the first line of the output:
- We then pipe that result to awk which prints out only the first column of the output:
awk '{print $1}' - Next we:
sort
the result. - And finally we remove duplicates with:
uniq
Now we can copy this to an email to the client, and all we have to do is explain what the :id and (.:format) are, as opposed to sending them the raw output of rake routes with all that extra information.
Enjoy!

