126

I would like to get all files from the web page below 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/

It returned a bunch of lines in the terminal, but didn't get any file.

5 Answers 5

171

Use wget instead.

Install it with Homebrew: brew install wget or MacPorts: sudo port install wget

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 restrictions in robots.txt file and 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
6
  • 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, 2014 at 15:44
  • brew and port doesn't work for me to install wget. What should I do? Jan 27, 2016 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, 2016 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, 2017 at 19:45
  • Don't forget the / at the end. Nov 25, 2020 at 17:34
31

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.

3
  • Thank you. This is a nice solution and providing working example is great !
    – egelev
    Oct 21, 2015 at 10:21
  • 3
    xmllint --html --xpath '//a/@href' is probably a better parser than grep.
    – ceving
    Feb 5, 2019 at 13:17
  • Thank you for answering the question that was asked, as I can not install wget on the server I need to use Jan 29, 2021 at 15:57
18

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:

4

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

1
  • 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, 2018 at 7:40
1

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.

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