How to get the filename from a http link in linux?

by viola_gleichner , in category: General Help , 6 months ago

How to get the filename from a http link in linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by adelia , 6 months ago

@viola_gleichner 

In Linux, you can use the basename command along with the curl command to extract the filename from an HTTP link. Here's how you can do it:

  1. Install the curl package (if not already installed) by running the following command: sudo apt-get install curl
  2. Once installed, you can use the curl command with the -I option to retrieve the HTTP headers from the link and search for the Content-Disposition header, which often includes the filename. curl -I http://example.com/file.pdf The output will contain the HTTP headers. Look for the Content-Disposition header. If present, it may look like this: Content-Disposition: attachment; filename="file.pdf"
  3. Finally, use the basename command to extract the filename from the header. basename $(curl -s -I http://example.com/file.pdf | grep -i content-disposition | awk -F" '{print $2}') This command uses command substitution ($(command)) to capture the output of curl, grep, and awk commands and then extracts the filename using basename. You need to replace http://example.com/file.pdf with your desired HTTP link.


Note: This method assumes that the HTTP server provides the filename in the Content-Disposition header. However, not all servers return this header, so it might not always work.

Member

by mohammad , 16 days ago

@viola_gleichner 

Another way to get the filename from an HTTP link in Linux is by using the following command:

1
2
curl -sI http://example.com/file.pdf | grep -i location | awk -F"/" '{print $NF}' | tr -d '
'


This command uses curl to retrieve the headers, grep to find the Location header, awk to extract the filename from the last part of the URL, and finally tr to remove any carriage return characters.


Please replace http://example.com/file.pdf with the actual HTTP link you want to extract the filename from.


This command does not rely on the Content-Disposition header, but it assumes that the filename is present in the URL itself.