夏期劇場
夏期劇場

Reputation: 18337

wget -O for non-existing save path?

I can't wget while there is no path already to save. I mean, wget doens't work for the non-existing save paths. For e.g:

wget -O /path/to/image/new_image.jpg http://www.example.com/old_image.jpg

If /path/to/image/ is not previously existed, it always returns:

No such file or directory

How can i make it work to automatically create the path and save?

Upvotes: 44

Views: 44690

Answers (6)

SanjayK
SanjayK

Reputation: 21

I was able to create folder if it doesn't exists with this command:
wget -N http://www.example.com/old_image.jpg -P /path/to/image

Upvotes: 2

Noam Manos
Noam Manos

Reputation: 16990

To download a file with wget, into a new directory, use --directory-prefix without -O:

wget --directory-prefix=/new/directory/ http://www.example.com/old_image.jpg

Using -O new_file in conjunction with --directory-prefix, will not create the new directory structure, and will save the new file in the current directory. It may even fail with "No such file or directory" error, if you specify -O /new/directory/new_file

Upvotes: 8

Toan Nguyen
Toan Nguyen

Reputation: 936

After searching a lot, I finally found a way to use wget to download for non-existing path.

wget -q --show-progress -c -nc -r -nH -i "$1"

=====
Clarification

-q
--quiet --show-progress
  Kill annoying output but keep the progress-bar

-c
--continue
  Resume download if the connection lost

-nc
--no-clobber
  Overwriting file if exists

-r
--recursive
  Download in recursive mode (What topic creator asked for!)

-nH
--no-host-directories
  Tell wget do not use the domain as a directory (for e.g: https://example.com/what/you/need
   - without this option, it will download to "example.com/what/you/need")

-i
--input-file
  File with URLs need to be download (in case you want to download a lot of URLs,
   otherwise just remove this option)

Happy wget-ing!

Upvotes: 0

K1773R
K1773R

Reputation: 982

wget is only getting a file NOT creating the directory structure for you (mkdir -p /path/to/image/), you have to do this by urself:

mkdir -p /path/to/image/ && wget -O /path/to/image/new_image.jpg http://www.example.com/old_image.jpg

You can tell wget to create the directory (so you dont have to use mkdir) with the parameter --force-directories

alltogether this would be

wget --force-directories -O /path/to/image/new_image.jpg http://www.example.com/old_image.jpg

Upvotes: 0

Ash
Ash

Reputation: 134

mkdir -p /path/i/want && wget -O /path/i/want/image.jpg http://www.com/image.jpg

Upvotes: 7

kev
kev

Reputation: 161914

Try curl

curl http://www.site.org/image.jpg --create-dirs -o /path/to/save/images.jpg

Upvotes: 83

Related Questions