Commands.page Logo

How to Check File Size Before Downloading in Ubuntu Linux

When working with the Ubuntu operating system, managing disk space and bandwidth is crucial, especially when dealing with large files hosted on remote servers. Before committing to a full download, it is often necessary to verify the size of a file to ensure you have sufficient storage and time. This article provides a quick overview of the specific command-line tools available in Ubuntu that allow you to inspect file metadata or retrieve only the beginning of a file without transferring the entire content.

To check the size of a remote file without downloading it, the most efficient method is to send a HEAD request. This retrieves the HTTP headers, which include the Content-Length, without fetching the actual file data. The primary command for this operation is curl with the -I flag.

curl -I https://example.com/file.zip

Upon running this command, look for the line labeled Content-Length in the output. This number represents the file size in bytes. If you prefer using wget, you can use the spider mode which checks if the file exists and retrieves its properties without saving anything to disk.

wget --spider https://example.com/file.zip

The output will display the length of the file alongside other connection details. These methods are ideal for scripting or quick checks where downloading the full file is impractical.

If your intention is to actually download only the first few bytes of the file content to inspect its format rather than just checking the total size, you can use a range request with curl. This downloads only the head of the file content.

curl -r 0-1024 -o partial_file https://example.com/file.zip

This command downloads the first 1024 bytes of the file and saves them as partial_file. However, for simply verifying the total size of the remote resource, the curl -I or wget --spider commands are the standard and most resource-efficient solutions on Ubuntu.