[wget] download a file while preserving the remote directory structure

 

from gemini

 

To download a file while preserving the remote directory structure on your local machine, you should use the -x (or --force-directories) flag.

If you want to go a step further and recreate the structure starting from a specific point (rather than including every single folder from the root of the server), you can combine it with the -nH flag.

The Standard Command

This command will create a folder named after the hostname and then replicate the full path of the file inside it.

Bash
 
wget -x https://example.com/assets/images/logo.png

Advanced Options for Path Control

Often, you don't want the hostname folder (like example.com/) at the start of your local path. Here is how you can refine the output:

Flag Function Resulting Path Example
-x Force directories. Creates the full path. example.com/assets/images/logo.png
-nH No Host. Disables the hostname prefix. assets/images/logo.png
--cut-dirs=N Ignore N levels. Skips the first $N$ directories. If $N=1$, it saves to images/logo.png

Pro-Tip: Downloading a Directory Tree

If your goal is to download an entire folder and its subdirectories while keeping everything organized, use the recursive flag:

Bash
 
wget -r -nH --cut-dirs=1 --no-parent https://example.com/assets/

Note: The --no-parent flag is a lifesaver; it prevents wget from accidentally wandering "upward" into other folders on the server that you didn't intend to download.

posted @ 2026-04-29 04:04  profesor  阅读(22)  评论(0)    收藏  举报