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
|
|
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
|
|
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
|
|
Better, again can we improve it further? Of course, we can try this
|
|
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?
|
|
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?
|
|
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 :)
Related Articles:
- 2024/06/17 Bash script performance issue and remediation
- 2024/03/29 Run X11 applications on docker or podman
- 2023/12/23 Show Local IP
- 2022/11/12 Openvpn files to nmcli connection
- 2017/03/06 Linux Best Practices and Tips
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.