Commands.page Logo

Download Files and Strip Session IDs from URL in Ubuntu

This guide explains how to download files on Ubuntu while removing session IDs from the URL. You will learn to use command-line tools like wget and curl to fetch content without saving messy query parameters in your filenames. We will cover basic commands and options to ensure your downloaded files are organized and free of temporary tracking data.

Understanding the Problem

When downloading files from web servers, URLs often contain session identifiers or tracking parameters. These appear as query strings at the end of the address, such as ?sessionid=12345. If you download these files using default settings, your operating system may save the file with these parameters included in the filename. This creates clutter and makes file management difficult.

Using Wget to Save Clean Filenames

The wget utility is pre-installed on most Ubuntu systems. It allows you to specify the output filename manually, ignoring the query parameters in the source URL.

  1. Open your terminal.
  2. Use the -O flag to define the output name.
  3. Run the following command:
wget -O document.pdf "http://example.com/download.php?file=document.pdf&sessionid=xyz"

In this example, wget fetches the content from the long URL but saves it locally as document.pdf. The session ID remains in the request but is excluded from your local file name.

Using Curl for Controlled Downloads

curl is another powerful tool available on Ubuntu for transferring data. It functions similarly to wget regarding output naming.

  1. Open your terminal.
  2. Use the -o flag to set the output filename.
  3. Run the following command:
curl -o document.pdf "http://example.com/download.php?file=document.pdf&sessionid=xyz"

This command downloads the data from the URL containing the session ID but writes it to a file named document.pdf.

Automating Filename Cleaning

If you have a list of URLs with varying session IDs, you can script the cleaning process. A simple bash loop can extract the base filename and assign it to the output flag.

url="http://example.com/file.zip?session=123"
filename="file.zip"
wget -O "$filename" "$url"

By explicitly defining the filename variable, you ensure that no query strings append to your saved data. This method keeps your download directory organized and free of temporary session artifacts.