Skip to content

Instantly share code, notes, and snippets.

@parkerl
Created February 8, 2012 18:31
Show Gist options
  • Save parkerl/1771976 to your computer and use it in GitHub Desktop.
Save parkerl/1771976 to your computer and use it in GitHub Desktop.
Extract image URLs from a CSS file and download them onto a new server (Linux)

#Extract Image URLs and WGet Them to a New Server#

This was a fun problem. I needed to move all the images referenced in a CSS file to another server. I didn't want to just grab all the image files as there were a bunch I didn't need. Here is how I went about it. I am sure you could do it in one step but doing it this way gives you a chance to check for errors.

First you may want to use wget http://otherserver/the_css.css to pull the CSS file on to the target server if it is still on the old server as it was in my case.

  1. User grep to extract the URLs from the css file into another file. (You may need to adjust the regular expression if you have funny characters in your file names) Note the use of the -o flag that tells grep to only print out that part of the line that matches the expression rather than the entire line.

     grep -o '\/path\/to\/files\/[a-zA-Z0-9/.:_-]*' the_css.css > images.txt
    
  2. Feed the images.txt file to wget with the -i flag and it will pull from the URLs and place the images in you current directory.

     wget -i images.txt 
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment