Download files

From Juneday education
Jump to: navigation, search

The source code that accompanies the material on this wiiki can be found at github.com. We also provide scripts to ease some tasks. This page will explain how to download using several different methods.

Download files

You can download files in several different ways. We give some examples on how to download using command line tools and also how to use a browser. We will assume you have a URL to a file to download.

Let's assume you want to download the file: https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml

Download using wget

With wget you can download the file like this:

 
$ wget --no-check-certificate --content-disposition https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml

wget downloads the file to the current directory so make sure to either

  • change directory (cd) to the directory where you want the file to be stored in
  • move (mv) the files to the directory where you want the file to be stored in

An example run of the above:

 
$ wget --no-check-certificate --content-disposition https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml
--2017-09-15 11:44:07--  https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml
Resolving github.com (github.com)... 192.30.253.112, 192.30.253.113
Connecting to github.com (github.com)|192.30.253.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/progund/tig015-weekly/master/beverages-framework/resources/sortiment.xml [following]
--2017-09-15 11:44:07--  https://raw.githubusercontent.com/progund/tig015-weekly/master/beverages-framework/resources/sortiment.xml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17214862 (16M) [text/plain]
Saving to: ‘sortiment.xml’

sortiment.xml                     100%[============================================================>]  16,42M  9,77MB/s    in 1,7s    

2017-09-15 11:44:09 (9,77 MB/s) - ‘sortiment.xml’ saved [17214862/17214862]

$ ls -al sortiment.xml
-rw-rw-r--. 1 hesa hesa 17214862 15 sep 11.44 sortiment.xml

Download using curl

With curl you can download the file like this:

 
$ curl -LJO -k https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml

curl downloads the file to the current directory so make sure to either

  • change directory (cd) to the directory where you want the file to be stored in
  • move (mv) the files to the directory where you want the file to be stored in

An example run of the above:

 
$ curl -LJO -k https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   172  100   172    0     0    180      0 --:--:-- --:--:-- --:--:--   179
100 16.4M  100 16.4M    0     0  3498k      0  0:00:04  0:00:04 --:--:-- 6416k
$ ls -al sortiment.xml 
-rw-rw-r--. 1 hesa hesa 17214862 15 sep 11.42 sortiment.xml

Download using a browser

Download and execute scripts

We will assume you have downloaded the file, if not see above. Let's say we want to download this script: https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/get_latest_jar.sh. So we can now assume that you have the file in the current directory.

Let's show how can check this in bash:

 
$ ls -al get_latest_jar.sh 
-rw-rw-r--. 1 hesa hesa 184 15 sep 11.41 get_latest_jar.sh

Now let's try to execute the script:

 
$ ./get_latest_jar.sh 
bash: ./get_latest_jar.sh: Permission denied

But why? This is because the file is not executable, meaning we're not allowed to execute it. We could start the script using bash ./get_latest_jar.sh but this is inconvenient and also will not work if other scripts invoke this script. Let's look at the permissions:

 
$ ls -al get_latest_jar.sh 
-rw-rw-r--. 1 hesa hesa 184 15 sep 11.41 get_latest_jar.sh

Before we continue we need to know what the letters (r, w, and x) mean in this context:

  • r means permission to read the file
  • w means permission to write (to) the file
  • x means permission to execute the file

Let's focus on the first part -rw-rw-r-- and analyse that in smaller chunks and

  • -rw-rw-r-- You have permissions r and w but not x.
  • -rw-rw-r-- The file's group have permissions r and w but not x.
  • -rw-rw-r-- All users have permissions r but not w and x.

Let's use the command chmod to make this script executable and then list the file again:

 
$ chmod a+x ./get_latest_jar.sh 
$ ls -al ./get_latest_jar.sh 
-rwxrwxr-x. 1 hesa hesa 184 15 sep 11.41 ./get_latest_jar.sh

We can now see that I, the group and everyone have permission to executre the file. How can we see this? Let's again focus on the first part of the listing and analyse that in smaller chunks and

  • -rwxrwxr-x You have permissions r, w and x.
  • -rwxrwxr-x The file's group have permissions r, w and x.
  • -rwxrwxr-x All users have permissions r and x but not w

We should now be able to execute the script with the following command:

 
$ ./get_latest_jar.sh

Note: If you want to change the permission on all sh files in the current directory you can use the following command:

 
$ chmod a+x *.sh

Getting the entire repository

We use git to version control To download the entire repository, which

Troubleshooting

If your, for some reason, fail to download the requested file or managed to download but the file seems to be wrong: check out our Bash FAQ