Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If you're downloading a large file in linux via command line, is there a command to know if it's already completed? Currently I am only checking if the file size is the same as the expected file size but what if you don't know the expected file size? Currently ls shows the exact file name (i.e., file.zip) instead of something like file.zip.temp or something even if the file is currently downloading.

share|improve this question
    
Couldn't you just check whether wget is still running? (ps -ef | grep wget | grep -v grep or similar) –  slhck Oct 21 '12 at 13:06
    
@slhck looks like that ninja code will do the job –  IMB Oct 21 '12 at 13:21

5 Answers 5

up vote 4 down vote accepted

If you're downloading with wget, you can simply check if the instance is still running:

ps -ef | grep wget | grep -v grep

Also, you could check whether the file is still opened by wget. Obviously, replace the path here.

lsof /path/to/downloaded/file
share|improve this answer

If you do not need to execute wget in background, do not do it, it will only lead to complications.

I advocate against solutions like checking ps -C wget or pidof wget (shorter, perhaps non-POSIX, equivalents to ps -ef | grep wget | grep -v grep), since they will consider any wget and not only the one you are interested in, which might be an unassumable assumption if your machine does not only do one task.

If you are inside a script, you can wget & WGETPID=$! and then check that PID. Or you could simply wait, which would wait for your background processes to finish.

It might be important to do something in order to distinguish between a successful and unsuccessful download. if wget ... ; then touch wget_ok ; else touch wget_error ; fi

share|improve this answer

What if you are running more than one wget command?

If you need to accommodate for this scenario, then you could do a few different things:

  • You could pipe the progress to a file with a known name and check that.
  • You could force the idea you had in your question, download to a temporary file, then in a a second command rename it.
  • You could just write a token after the process is completed, i.e.

    wget 'file'; touch file.is.done
    
share|improve this answer

The lsofapproach suggested by slhck is probably the best way. If you want to use the other suggestions involving creating a file once the download is complete, use && instead of ; so that a report is generated only if the download exits correctly:

wget file && touch file.is.done
share|improve this answer

I'd like rather sound signal, so if i start a long operation (not only wget but any time resourse consuming) that i don't want watch instantaneously i use something like

`LONG_COMMAND && SOUND_OK || SOUND_ERROR`

so even if i need to know immediately when it finishes i'll also be able to do something else the same time without interrupting myself time to time for the checking, i just can hear result (of SOUND_OK if first command exit status is 0 and SOUND_ERROR in other case) and check it only once when it finishes. You can even continue current activity if only you need to know is that your task finished. LONG_COMMAND could be wget, and as SOUND_OK i prefer echo -e "\a" >/dev/console but you can use command to play any audio sample.

As long as wget can produce several different exit codes, one can use them to determine the alarm command in "bios-style" as follows

wget URL;\
CODE=$?;\
beep;\
[ $CODE -gt 0 ] && while true; do
 for peek in `seq 1 $CODE`; do
 beep; sleep 1;done
sleep 3; done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.