Commands.page Logo

Handle Content-Disposition Headers When Downloading on Ubuntu

When downloading files from the web on Ubuntu, servers often use the content-disposition header to specify the actual filename. This article explains how to use command-line tools like curl and wget to respect these headers automatically. You will learn the specific flags required to save files with their intended names rather than generic defaults, ensuring your downloads are organized correctly without manual renaming.

Using Curl to Handle Headers

The curl utility is pre-installed on most Ubuntu systems and offers robust support for HTTP headers. To download a file while respecting the content-disposition header, use the -O and -J flags together. The -O flag saves the file using the remote name, while -J tells curl to use the header-suggested filename.

curl -OJ -L https://example.com/file

Include the -L flag if the URL might redirect to another location. This combination ensures the file is saved with the name provided by the server.

Using Wget for Downloads

Wget is another powerful tool available in the Ubuntu repositories. By default, wget does not always trust the content-disposition header for security reasons. You must explicitly enable this feature using the --content-disposition flag.

wget --content-disposition https://example.com/file

If wget is not installed, you can add it via sudo apt install wget. This command will parse the header and save the file accordingly.

Verifying the Download

After running either command, list the directory contents to confirm the filename matches the expected output. Use the ls -lh command to view file sizes and names. If the file retains a generic name, the server may not be sending the correct header, or the URL requires authentication.