85

I would like to download the files, all at once, from the following page using curl: http://www.ime.usp.br/~coelho/mac0122-2013/ep2/esqueleto/

I tried curl http://www.ime.usp.br/~coelho/mac0122-2013/ep2/esqueleto/ and it returned a bunch of lines in the terminal, but did'nt get any files.

124

Use wget instead. You can install it with brew install wget if you have installed Homebrew or sudo port install wget if you have installed MacPorts.

For downloading files from a directory listing, use -r (recursive), -np (don't follow links to parent directories), and -k to make links in downloaded HTML or CSS point to local files (credit @xaccrocheur).

wget -r -np -k http://www.ime.usp.br/~coelho/mac0122-2013/ep2/esqueleto/

Other useful options:

  • -nd (no directories): download all files to the current directory
  • -e robots.off: ignore robots.txt files, don't download robots.txt files
  • -A png,jpg: accept only files with the extensions png or jpg
  • -m (mirror): -r --timestamping --level inf --no-remove-listing
  • -nc, --no-clobber: Skip download if files exist
  • 3
    wget -r -np -k http://your.website.com/specific/directory. The trick is to use -k to convert the links (images, etc.) for local viewing. – yPhil Dec 11 '14 at 15:44
  • brew and port doesn't work for me to install wget. What should I do? – Hoseyn Heydari Jan 27 '16 at 15:58
  • @HoseynHeydari : you can use rudix.org for compiled binaries for osx. so you need to install rudix and then use : sudo rudix install wget – Mamonu Jul 3 '16 at 21:29
  • The option -k does not always work. E.g., if you have two links pointing to the same file on the webpage you are trying to capture recursively, wget only seems to convert link of the first instance but not the second one. – Kun Jan 28 '17 at 19:45
22

curl can only read single web pages files, the bunch of lines you got is actually the directory index (which you also see in your browser if you go to that URL). To use curl and some Unix tools magic to get the files you could use something like

for file in $(curl -s http://www.ime.usp.br/~coelho/mac0122-2013/ep2/esqueleto/ |
                  grep href |
                  sed 's/.*href="//' |
                  sed 's/".*//' |
                  grep '^[a-zA-Z].*'); do
    curl -s -O http://www.ime.usp.br/~coelho/mac0122-2013/ep2/esqueleto/$file
done

which will get all the files into the current directory.

For more elaborated needs (including getting a bunch of files from a site with folders/directories), wget (as proposed in another answer already) is the better option.

  • Thank you. This is a nice solution and providing working example is great ! – egelev Oct 21 '15 at 10:21
  • 1
    xmllint --html --xpath '//a/@href' is probably a better parser than grep. – ceving Feb 5 at 13:17
7

Ref: http://blog.incognitech.in/download-files-from-apache-server-listing-directory/

You can use following command:

wget --execute="robots = off" --mirror --convert-links --no-parent --wait=5 <website-url>

Explanation with each options

  • wget: Simple Command to make CURL request and download remote files to our local machine.
  • --execute="robots = off": This will ignore robots.txt file while crawling through pages. It is helpful if you're not getting all of the files.
  • --mirror: This option will basically mirror the directory structure for the given URL. It's a shortcut for -N -r -l inf --no-remove-listing which means:
    • -N: don't re-retrieve files unless newer than local
    • -r: specify recursive download
    • -l inf: maximum recursion depth (inf or 0 for infinite)
    • --no-remove-listing: don't remove '.listing' files
  • --convert-links: make links in downloaded HTML or CSS point to local files
  • --no-parent: don't ascend to the parent directory
  • --wait=5: wait 5 seconds between retrievals. So that we don't thrash the server.
  • <website-url>: This is the website url from where to download the files.

Happy Downloading :smiley:

2

You can use httrack available for Windows/MacOS and installable via Homebrew.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • something new for me, thanks. best is it is quick and fast: one -liner. just cut the fat and avoid all rubbish. – Peter Teoh May 9 '18 at 7:40
0

For those of us who would rather use an application with a GUI, there is the inexpensive shareware program DeepVacuum for Mac OS X, which implements wget in a user-friendly manner, with a list of presets that can handle commonly-needed tasks. You can also save your own custom configurations as presets.

enter image description here

You must log in to answer this question.

protected by Community Aug 17 '18 at 17:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .