1

I need a way that helps me to run a command automatically after a download completes.

I want to create a directory beside the downloaded file automatically after I clicked on the integrated download button from Firefox. I want all of those to be automatic.

4
  • I think this hasn't got much to do with Ubutnu. Just search for a addon which executes a command after completing a download.
    – Marton
    Jun 27, 2015 at 21:32
  • What would be the purpose of the newly created directory and what would the command exactly need to do? Jun 28, 2015 at 17:17
  • that was just an example to explain my problem. actually I want to learn "the way" to run any command after download completed, not only such a thing like creating a directory!
    – Snowleaf
    Jun 28, 2015 at 23:26
  • wget has that option... Aug 2, 2016 at 16:00

3 Answers 3

1

If you can download the file outside of Firefox, you can use

filetodownload=file-to-download.ext
wget http://www.example.org/"$filetodownload" -O "$filetodownload"
#"I want to create a directory beside the downloaded file automatically"
#You didn't specify a dir name
mkdir "$filetodownload"

Otherwise, you'll have to monitor the file's download progress and when it's finished, make the directory "beside the downloaded file". The following worked for me (but I couldn't make a dir with the same name as the file). It looks at all files though, including hidden ones. Modify as needed.

#! /bin/bash
while [ 1 ]
 do
  for files in $(find . -maxdepth 1 -mmin -1 -type f)
   do
    testbefore=$(ls -l $files | awk '{print $5$6$7$8$9}')
    sleep 2
    testafter=$(ls -l $files | awk '{print $5$6$7$8$9}')
    if [ $testbefore = $testafter ]
     then
#     echo $testbefore
     mkdir $(basename "$files")_
    fi
   sleep 5
  done
 done
2
  • I don't understand the first line of the first method! what is it?
    – Snowleaf
    Jun 27, 2015 at 23:43
  • 1
    That's a variable. When you assign a value to a variable, you can just do var=value. When you want to use it later in the script or command-line session, you prefix it with a '$'. If you paste that into Notepad++ and set the "Language" to "Shell", it will colorize the keywords. It makes it easier to see what's what.
    – user38537
    Jun 28, 2015 at 0:10
0
  1. Add mime type to neverAsk.openFile in about:config (you can add */* for all)
  2. Create an executable script that:
    • takes the file path from the arguments list (the first argument)
    • moves it to target location (as it has been downloaded to tmp)
    • runs whatever commands you want
  3. Tell Firefox to open this file type with your script

This last one is easy to do in about:preferences but little harder if you want to manually add records to mimeTypes.rdf file in Firefox's profile.

1
  • It doesn't work any more. I checked on Firefox 41.0.2, 44.0 and 47.0. It seems like a bug in Firefox. The checkbox "do this automatically for files like that from now on" works for Save File but does not work for Open with download dialog option any more.
    – cprn
    Aug 4, 2016 at 11:48
0

Firefox downloads a file under a tempory name $f.part and if download is completed renames it to $f.

So you can watch for directory changes with tools like inotifywait or watchmedo.

inotifywait -m .
...
./ MOVED_FROM file.zip.part
./ MOVED_TO file.zip

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .