Technical Implementation of Downloading Files to Specific Directories Using curl Command

Nov 19, 2025 · Programming · 14 views · 7.8

Keywords: curl command | file download | directory operation | shell scripting | batch processing

Abstract: This article provides an in-depth exploration of various technical solutions for downloading files to specific directories using the curl command in shell scripts. It begins by introducing traditional methods involving directory switching through cd commands, including two implementation approaches using logical AND operators and subshells. The article then details the differences and application scenarios between curl's -O and -o options for file naming. Following this, it examines the --output-dir option introduced in curl version 7.73.0 and its combination with --create-dirs. Finally, through practical case studies, the article presents complete solutions for batch file downloading in complex directory structures, covering key technical aspects such as file searching, variable handling, loop control, and error management.

Basic Methods for Downloading Files to Specific Directories with curl

In shell script development, there is often a need to download files from the internet and save them to specific directories. curl, as a powerful command-line tool, offers multiple approaches to achieve this requirement.

Traditional Directory Switching Methods

In earlier versions of curl, there was no direct option to specify the download directory. Developers needed to switch working directories to achieve file downloads to specific locations.

Using Logical AND Operator

cd target/path && { curl -O URL ; cd -; }

This method first uses the cd command to switch to the target directory, then executes the curl download operation, and finally uses cd - to return to the original directory. The logical AND operator && ensures that the download operation only executes if the directory switch is successful, enhancing script robustness.

Using Subshell Environment

(cd target/path && curl -O URL)

By executing directory switching and download operations within a subshell, the parent shell's working directory remains unaffected. After the subshell completes execution, it automatically returns to the original directory state.

Detailed Explanation of curl Options

-O Option: Preserving Remote Filename

The -O option instructs curl to save the file using the filename from the remote server. When combined with directory switching methods, the file is saved in the current working directory.

-o Option: Specifying Local Filename

curl -o target/path/filename URL

The -o option allows explicit specification of the complete path and name for the local file. This method does not require switching working directories; the target path is specified directly in the command.

Modern curl Directory Support

--output-dir Option

Starting from curl version 7.73.0, the --output-dir option was introduced, allowing direct specification of the download directory:

curl --create-dirs -O --output-dir /tmp/receipes https://example.com/pancakes.jpg

--create-dirs Option

When the target directory does not exist, the --create-dirs option automatically creates the required directory structure, eliminating the need for manual directory creation.

Batch Downloading in Complex Scenarios

In practical applications, there is often a need to handle batch downloading of multiple files across different directories.

File Searching and Variable Handling

var1=( $( find -maxdepth 2 -type f -name '*.txt' -print0 | xargs -0 grep -ho -E 'HW[0-9]{10}' ) )

Using the find command combined with regular expressions to locate specific files and store results in array variables.

Loop Processing for Multiple Files

for var1 in $(grep -R -h --include="*.txt" 'foo\|bar')
do
    curl -O "https://api.site.com/3/$var1"
done

Processing multiple matching results through loop structures, executing independent download operations for each match.

JSON Data Processing

get_HW_info(){
    curl --request GET \
    --url "https://site.com/find/$var1?external_source=HW_id" \
    --header 'content-type: application/json' \
    --data '{}'
}

In API calling scenarios, combining JSON data processing to extract filename and path information from server responses.

Best Practice Recommendations

Error Handling Mechanisms

In actual deployments, appropriate error handling mechanisms should be added, including network connection checks, directory permission verification, and download result confirmation.

Performance Optimization

For batch downloading scenarios, consider using parallel downloads or connection reuse techniques to improve download efficiency. Additionally, avoid repeated downloads of the same content and implement reasonable caching strategies.

Security Considerations

When handling user input or external data, thorough validation and escaping are necessary to prevent path traversal attacks and command injection vulnerabilities.

Conclusion

The curl command provides multiple solutions for downloading files to specific directories, ranging from basic to advanced levels. Developers can choose appropriate methods based on specific requirements, finding corresponding implementations from simple directory switching to complex batch processing. With updates to curl versions, officially provided options have become increasingly rich, making file download operations more convenient and efficient.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.