Downloading Files to a Specific Directory
Often, you'll want to download files to a specific directory rather than your current working directory. Both curl
and wget
allow you to do this, but they use different methods.
- First, let's create a new directory to download our files into:
mkdir downloads
This creates a new directory named downloads
in your current location.
- Now, let's use
curl
to download a file to this directory:
curl -o downloads/curl_file.html http://example.com
The -o
option in curl
allows us to specify the output file, including its path. This command downloads the content from example.com and saves it as curl_file.html
in the downloads
directory.
- Next, let's use
wget
to download a file to the same directory:
wget -P downloads http://example.com/index.html
The -P
option in wget
stands for "prefix" and allows us to specify the directory where we want to save the file. This command downloads index.html
from example.com and saves it in the downloads
directory.
- We can verify that both files were downloaded to the
downloads
directory:
ls -l downloads
You should see both curl_file.html
and index.html
in the output.
- To see the contents of these files without changing our current directory, we can use
cat
with the full path:
cat downloads/curl_file.html
cat downloads/index.html
This allows us to verify the content of the downloaded files.