How to Make wget Follow Relative Links Only?

When mirroring a website or downloading specific directories using wget, you often want to restrict the download to files linked within the same structure. By default, wget may follow absolute links that lead to external domains or parent directories, cluttering your local download. To force wget to strictly follow relative links and stay within a specific directory hierarchy, you must combine recursive downloading with strict directory and domain locking options.

The Command Solution

To achieve this, use the following command structure in your terminal:

wget --recursive --no-parent --level=inf --page-requisites --adjust-extension --convert-links --span-hosts=off http://example.com/target-directory/

Key Flags Explained

Common Pitfalls to Avoid

When attempting to isolate downloads to relative paths, watch out for the following behaviors: