Modifying wget permissions and download directories on Cygwin

cygwin;permissionswget

I have Cygwin installed on my Windows 7 64 bit PC and I often download large files using wget. wget, however, saves all files with permissions that forbid me from executing the files when they're Windows executable files (.exe or .msi). Is there any way for me to set the permissions such that I can execute these files automatically? Can I also change the default directory to which they're saved? Presently they are saved to the cygwin folder C:\cygwin64\home\Brenton and I'd like to save them to C:\Users\Brenton\Downloads.

For clarity's sake I know how to move these files to my desired directory manually after they're saved (e.g., using:

mv filename C:\Users\Brenton\Downloads

where filename is the file name that needs to be transferred) and I know how to change permissions manually via:

chmod 755 filename

but what I want is an automatic way so that all files downloaded via wget from henceforth would be downloaded to my preferred directory with my preferred permissions.

Best Answer

Destination directory

You can use the -P prefix / --directory-prefix=prefix option to direct wget to save to a certain directory.

Set directory prefix to prefix. The directory prefix is the directory where all other files and subdirectories will be saved to, i.e. the top of the retrieval tree. The default is . (the current directory).

Example to save a single file to your download directory:

wget -p $(cygpath -u "C:\Users\Brenton\Downloads") http://host.name/setup.exe

File permissions

On my system, umask is set to 0022 so when I use wget to download files to my Cygwin home directory, the files have -rw-r--r-- permissions – as expected.

However, if I download to my Windows Downloads directory, the executable permissions are set. I’m not sure why but I suspect that somehow the permissions are being by influenced by NTFS ACLs. This behaviour may also work to your favour on your system. If it does, you could use a simple shell alias such as:

alias wget-exe='wget -P $(cygpath -u "C:\Users\Brenton\Downloads") '

Suggested shell script / function

If you still have to change permissions, you could use the following code as the basis for a shell script or function (called something like wget-exe):

#!/bin/sh
downloads=$(cygpath -u "C:\Users\Brenton\Downloads")
wget -P "$downloads" "$@"

dir="$PWD"
cd "$downloads"
chmod 755 *.exe *.msi
cd "$dir"
# Alternative version using find to change permissions of all .msi and .exe files
# downloaded in the last day.
# find "$downloads" -mtime -1 \( -name '*.exe' -o -name '*.msi' \) -print0 | xargs -0 chmod +w
Related Question