Short url to full url

2024-06-19 601 words 3 mins read

Most of the Social sites will shorten the links. There are a lot of ways to check the url before you go to the url and get phished :)

For example, for bitly you can just add a ‘+’ at the end of the url. For example, you can enter the following in url bar https://bit.ly/2KEOXNx+ and see the details of the where the url takes you. This is just a example url that I got from google but you get the idea.

Then there are some sites which allow you to expand the url like expandurl where you can enter the url and it will show you the expanded url.

But if you are in a for some fun, do you need any of these :). Terminals are a lot of fun when you want to do interesting things.

First lets understand how these short links work. Here is a makeuseof article on this. But in short, you send a request to the link, the server responds with just headers and the headers contains a HTTP parameter called Location. Browsers know that it needs to follow the link in Location header because the response code is of 3xx like 301 (mostly) which means Moved Permanently.

With that out of the way, we can use this information to our advantage. A simple command like this should show you the full url

1
curl -Isq bitly.com/enterprise|grep -i Location

Here we are instructing curl to get only the headers and be silent and quite. The respons contains the header and we grep for the Location header. Thats simple but can we make it better. Of course, everything can be improved. Lets see next one

1
curl -Isq bitly.com/enterprise|awk '/Location/{print $2}'

Now, we have replace grep with awk (you can use your own tool like sed, grep or whatever else you like to find a patter), and just print the url, insteam of the complet output. How does it look

1
2
$ curl -Isq bitly.com/enterprise|awk '/Location/{print $2}'
https://bitly.com/pages/enterprise

Better, again can we improve it further? Of course, we can try this

1
curl -Isq bitly.com/enterprise|awk '/Location/{print $2}'|xclip -i -selection clipboard

Whoa, what happened here. If you run this, it does not show the url any more. It does not becuase it has copied it to your clipboard that you can paste with Shift+Ctrl+v on most linux systems or with right click and paste or Ctrl+v. Now, you can put this in your bashrc as an alias and boom, next time you have a short link, you can expand it.

Can we improve this further, you may ask. Why not?

1
curl -Iqs "$(xclip -o -selection clipboard)" |awk '/Location/{print $2}'|xclip -i -selection clipboard

What does this do, well now you dont need to worry about changing the url in the command as you can see in the command above. Now, you can just copy the url with Shift+Ctrl+c or Ctrl+c or mouse into your clipboard and then run this command. It will expand the url and show you the full url.

One last time, can we improve this further, why not?

1
a="$(curl -Iqs "$(xclip -o -selection clipboard)" |awk 'toupper($1) ~ /LOCATION/{print $2}')"; echo $a; echo $a | xclip -i -selection clipboard

This time we have made 2 changes.

  • Use toupper in awk. This is to ensure that we can do case insensitive search for Location in headers.
  • We are saving the full url to a variable. Then print the variable and copy it to clipboard. This enables us to see the url as well as copying to the clipboard.

Hope this helps :)


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it