1

I'm developing maven snapshots. I pull down files using wget which is working fine, but it pulls down the files and creates a directory structure according to the URL.

EX:

http://example.net/nexus/content/repositories/repo/com/org/product/develop/component/buildNumber/file.txt

will create a directory structure beginning with example.net all the way to buildnumber which will contain file.txt and file2.txt

I need to reach the end of the directory structure in order to rename the files within it; the problem is that the workspace is at the beginning of the folder structure. I'm assuming that I could pull apart the URL through a for loop and cd through the results in between the slashes. I'm very new to shell scripting so I'm looking for some direction of how to structure my for loop and break apart this URL.

ghoti
  • 45,319
  • 8
  • 65
  • 104
Fpanico
  • 49
  • 1
  • 8
  • 1
    You need `basename` to get the name of the file without the path. Check this question: http://stackoverflow.com/q/965053/1983854 – fedorqui Jul 31 '14 at 15:46
  • I'm using multiple urls which could have changing content within their repos, so there could be many files with names I won't know. So I'm not sure how basename can help me – Fpanico Jul 31 '14 at 15:55
  • 2
    Do you actually want all those directories created locally? Because you can tell wget not to do that. – Etan Reisner Jul 31 '14 at 15:57
  • Well, I'm configured to pull down from many different URLs, so I'll need at least one directory for each artifact. But I see what you're saying. How can I structure wget to just pull files into the directory I want? – Fpanico Jul 31 '14 at 16:00
  • While you probably could extract the path all the way to the file, it will be easier to do what @EtanReisner is suggesting and just config `wget` (unfortunately, I barely use `wget` so I can't help) – skamazin Jul 31 '14 at 20:03
  • Can you perhaps include some code that you've tried, details of the results you're expecting, along with the results you've actually gotten? This is still a pretty theoretical question, which makes it much harder to answer. You'll get better results if there's actually some code to fix in your question. – ghoti Jul 31 '14 at 20:18
  • Look at the `-nH` `-nd` and `--cut-dirs` arguments to wget. – Etan Reisner Jul 31 '14 at 21:03

1 Answers1

0

this should get you in the folder where the files have been created:

#! /bin/bash

url='http://example.net/nexus/content/repositories/repo/com/org/product/develop/component/buildNumber/file.txt'

cd $(dirname $(sed -e 's#http://##' <<< "$url"))
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51