Search code examples
linuxcurlawkgrepcut

How to get the upload speed from cURL?


When I run below curl upload command it gives me big output. How can I cut short this to just show the final upload speed?

curl --upload-file /tmp/testlocal -v -u tu**r:******@*23 http://nexus3-core:8081/nexus3/repository/tes******/tes*****
* Expire in 0 ms for 6 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Expire in 0 ms for 1 (transfer 0x558e6c881f50)
* Expire in 2 ms for 1 (transfer 0x558e6c881f50)
* Expire in 0 ms for 1 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
*   Trying 172.30.51.207...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x558e6c881f50)
* Connected to nexus3-core (172.30.51.207) port 8081 (#0)
* Server auth using Basic with user 'tu****'
> PUT /nexus3/repository/testu****/tes**** HTTP/1.1
> Host: nexus3-core:8081
> Authorization: Basic dHVzZXI6VHVzZXJAMTIz
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Length: 1048576000
> Expect: 100-continue
> 
* Expire in 1000 ms for 0 (transfer 0x558e6c881f50)
< HTTP/1.1 100 Continue
} [41940 bytes data]

  4 1000M    0     0    4 45.5M      0  50.7M  0:00:19 --:--:--  0:00:19 50.6M
  8 1000M    0     0    8 89.7M      0  47.3M  0:00:21  0:00:01  0:00:20 47.2M
 13 1000M    0     0   13  139M      0  48.2M  0:00:20  0:00:02  0:00:18 48.2M
 19 1000M    0     0   19  193M      0  49.6M  0:00:20  0:00:03  0:00:17 49.6M
 23 1000M    0     0   23  234M      0  47.9M  0:00:20  0:00:04  0:00:16 47.9M
 29 1000M    0     0   29  291M      0  49.4M  0:00:20  0:00:05  0:00:15 49.2M
 34 1000M    0     0   34  346M      0  50.1M  0:00:19  0:00:06  0:00:13 51.2M
 40 1000M    0     0   40  408M      0  51.7M  0:00:19  0:00:07  0:00:12 53.8M
 46 1000M    0     0   46  465M      0  52.3M  0:00:19  0:00:08  0:00:11 54.3M
 52 1000M    0     0   52  520M      0  52.5M  0:00:19  0:00:09  0:00:10 57.1M
 58 1000M    0     0   58  587M      0  53.9M  0:00:18  0:00:10  0:00:08 59.1M
 64 1000M    0     0   64  648M      0  54.4M  0:00:18  0:00:11  0:00:07 60.3M
 70 1000M    0     0   70  706M      0  54.7M  0:00:18  0:00:12  0:00:06 59.5M
 76 1000M    0     0   76  763M      0  54.9M  0:00:18  0:00:13  0:00:05 59.5M
 78 1000M    0     0   78  781M      0  51.2M  0:00:19  0:00:15  0:00:04 48.7M
 79 1000M    0     0   79  791M      0  49.7M  0:00:20  0:00:15  0:00:05 40.7M
 83 1000M    0     0   83  839M      0  49.6M  0:00:20  0:00:16  0:00:04 38.2M
 89 1000M    0     0   89  895M      0  50.0M  0:00:19  0:00:17  0:00:02 37.8M
 95 1000M    0     0   95  957M      0  50.6M  0:00:19  0:00:18  0:00:01 38.9M* We are completely uploaded and fine

100 1000M    0     0  100 1000M      0  48.6M  0:00:20  0:00:20 --:--:-- 41.0M< HTTP/1.1 201 Created
< Date: Fri, 08 Jan 2021 06:52:32 GMT
< Server: Nexus/3.23.0-03 (OSS)
< X-Content-Type-Options: nosniff
< Content-Security-Policy: sandbox allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-top-navigation
< X-XSS-Protection: 1; mode=block
< Content-Length: 0
< 

100 1000M    0     0  100 1000M      0  47.9M  0:00:20  0:00:20 --:--:-- 41.9M

My desired out put is as below

Curl upload speed : 41.9M

I know curl prints stderr and I am struggling to get that output with grep


Solution

  • Not exactly answering your question but perhaps a better way to get the average upload speed is to use the dedicated option for it? Try this:

    curl -w 'Speed: %{speed_upload}\n' -T local-file http://...target...
    

    That -w option string will then output the average upload speed (in bytes/sec) after a successful transfer.