Get network usage from specific date on terminal

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite












On vnstat I can get the month, day, hour or top 10 days network usage. Is there anyway that I can get the network usage from specific date on the terminal? by vnstat or another tool?



Edit:



I want to have the usage from specific date to the current date or between two dates not only a specific date.







share|improve this question


























    up vote
    2
    down vote

    favorite












    On vnstat I can get the month, day, hour or top 10 days network usage. Is there anyway that I can get the network usage from specific date on the terminal? by vnstat or another tool?



    Edit:



    I want to have the usage from specific date to the current date or between two dates not only a specific date.







    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      On vnstat I can get the month, day, hour or top 10 days network usage. Is there anyway that I can get the network usage from specific date on the terminal? by vnstat or another tool?



      Edit:



      I want to have the usage from specific date to the current date or between two dates not only a specific date.







      share|improve this question














      On vnstat I can get the month, day, hour or top 10 days network usage. Is there anyway that I can get the network usage from specific date on the terminal? by vnstat or another tool?



      Edit:



      I want to have the usage from specific date to the current date or between two dates not only a specific date.









      share|improve this question













      share|improve this question




      share|improve this question








      edited May 1 at 12:02

























      asked Apr 30 at 23:11









      ICE

      6852723




      6852723




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          vnStat supports date and time range specific queries for all list outputs starting from version 2.0 (currently available as beta). That version also allows free configuration of data retention durations so there's no more hardcoded 30 day limit for daily data. See the change notes and the GitHub repository for more details.



          $ vnstat --days --begin 2018-04-02 --end 2018-04-06

          em1 / daily

          day rx | tx | total
          -------------------------+-------------+---------------------------------------
          2018-04-02 4.88 GB | 1.95 GB | 6.83 GB %%%%%%%%%%%%%%%%%:::::::
          2018-04-03 3.56 GB | 1.09 GB | 4.66 GB %%%%%%%%%%%%::::
          2018-04-04 3.91 GB | 2.07 GB | 5.99 GB %%%%%%%%%%%%%%:::::::
          2018-04-05 2.61 GB | 1.63 GB | 4.24 GB %%%%%%%%%:::::
          2018-04-06 3.29 GB | 1.43 GB | 4.72 GB %%%%%%%%%%%:::::
          -------------------------+-------------+---------------------------------------
          sum of 5 18.25 GB | 8.17 GB | 26.43 GB





          share|improve this answer




















          • Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
            – WinEunuuchs2Unix
            May 5 at 23:21











          • I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
            – Teemu Toivola
            May 5 at 23:37










          • Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
            – WinEunuuchs2Unix
            May 5 at 23:58











          • Thanks for adding this feature. This solution seems much cleaner.
            – ICE
            May 6 at 1:17










          • I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
            – WinEunuuchs2Unix
            May 12 at 2:34

















          up vote
          1
          down vote













          Copy the code below into a file. I'm using ~/bin/vnstat-hist.sh. After saving the file mark it as executable using:





          chmod a+x ~/bin/vnstat.sh


          To run the script call it with the parameter for number of days. For example for today use vnstat-hist.sh 1. For last five days (including today) use:



          $ vnstat-hist.sh 5
          vnstat -d 5 day summary
          2018-04-27 6.21 GiB | 1.83 GiB | 8.04 GiB | 780.45 kbit/s
          2018-04-28 5.97 GiB | 1.05 GiB | 7.02 GiB | 681.20 kbit/s
          2018-04-29 8.27 GiB | 1.47 GiB | 9.74 GiB | 945.40 kbit/s
          2018-04-30 4.09 GiB | 1.35 GiB | 5.44 GiB | 527.97 kbit/s
          2018-05-01 1.36 GiB | 1.13 GiB | 2.49 GiB | 315.40 kbit/s
          Total:32.73



          vnstat-hist.sh Bash script



          Note this program can be shorter but hopefully the design is easier to follow for novices.



          #!/bin/bash

          # NAME: vnstat-hist.sh
          # PATH: $HOME/bin
          # DESC: Written for AU Q&A: https://askubuntu.com/questions/1030345/get-network-usage-from-specific-date-on-terminal/1030399?noredirect=1#comment1675801_1030399
          # Get total vnStat bytes from x days ago to today.
          # Parameter 1 = number of days: 1= today, 2= yesterday + today, etc.

          # DATE: May 1, 2018.

          re='^[0-9]+$'
          if ! [[ $1 =~ $re ]] ; then
          echo "Error: Parameter 1 must be number of days" >&2; exit 1
          fi

          # Get body of vnstat -d into file, ie strip headings and total lines
          # First get count of all lines, then delete 2 total lines & 5 heading lines

          vnstat -d > /tmp/vnstat-hist.txt
          NumLines=$(cat /tmp/vnstat-hist.txt | wc -l)
          NumLines=$(( NumLines - 2))
          cat /tmp/vnstat-hist.txt | head -n $NumLines > /tmp/vnstat-hist2.txt
          NumLines=$(( NumLines - 5))
          cat /tmp/vnstat-hist2.txt | tail -n $NumLines > /tmp/vnstat-hist.txt

          MaxDays=$(cat /tmp/vnstat-hist.txt | wc -l)

          DayCount="$1"
          (( $DayCount > $MaxDays )) && DayCount=$MaxDays
          cat /tmp/vnstat-hist.txt | tail -n $DayCount > /tmp/vnstat-hist2.txt
          echo "vnstat -d $DayCount day summary"
          awk 'sum+=$8; END print "Total:" sum 1' /tmp/vnstat-hist2.txt

          # Clean up temp files
          rm -f /tmp/vnstat-hist.txt
          rm -f /tmp/vnstat-hist2.txt

          exit 0





          share|improve this answer






















          • Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
            – ICE
            May 1 at 11:52










          • Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
            – WinEunuuchs2Unix
            May 1 at 15:09










          • Yes. It's from the past 30 days.
            – ICE
            May 1 at 15:15






          • 1




            @ICE You're welcome. I enjoyed learning some new things putting it together.
            – WinEunuuchs2Unix
            May 2 at 0:47






          • 1




            @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
            – WinEunuuchs2Unix
            May 6 at 1:49










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1030345%2fget-network-usage-from-specific-date-on-terminal%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          vnStat supports date and time range specific queries for all list outputs starting from version 2.0 (currently available as beta). That version also allows free configuration of data retention durations so there's no more hardcoded 30 day limit for daily data. See the change notes and the GitHub repository for more details.



          $ vnstat --days --begin 2018-04-02 --end 2018-04-06

          em1 / daily

          day rx | tx | total
          -------------------------+-------------+---------------------------------------
          2018-04-02 4.88 GB | 1.95 GB | 6.83 GB %%%%%%%%%%%%%%%%%:::::::
          2018-04-03 3.56 GB | 1.09 GB | 4.66 GB %%%%%%%%%%%%::::
          2018-04-04 3.91 GB | 2.07 GB | 5.99 GB %%%%%%%%%%%%%%:::::::
          2018-04-05 2.61 GB | 1.63 GB | 4.24 GB %%%%%%%%%:::::
          2018-04-06 3.29 GB | 1.43 GB | 4.72 GB %%%%%%%%%%%:::::
          -------------------------+-------------+---------------------------------------
          sum of 5 18.25 GB | 8.17 GB | 26.43 GB





          share|improve this answer




















          • Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
            – WinEunuuchs2Unix
            May 5 at 23:21











          • I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
            – Teemu Toivola
            May 5 at 23:37










          • Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
            – WinEunuuchs2Unix
            May 5 at 23:58











          • Thanks for adding this feature. This solution seems much cleaner.
            – ICE
            May 6 at 1:17










          • I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
            – WinEunuuchs2Unix
            May 12 at 2:34














          up vote
          2
          down vote



          accepted










          vnStat supports date and time range specific queries for all list outputs starting from version 2.0 (currently available as beta). That version also allows free configuration of data retention durations so there's no more hardcoded 30 day limit for daily data. See the change notes and the GitHub repository for more details.



          $ vnstat --days --begin 2018-04-02 --end 2018-04-06

          em1 / daily

          day rx | tx | total
          -------------------------+-------------+---------------------------------------
          2018-04-02 4.88 GB | 1.95 GB | 6.83 GB %%%%%%%%%%%%%%%%%:::::::
          2018-04-03 3.56 GB | 1.09 GB | 4.66 GB %%%%%%%%%%%%::::
          2018-04-04 3.91 GB | 2.07 GB | 5.99 GB %%%%%%%%%%%%%%:::::::
          2018-04-05 2.61 GB | 1.63 GB | 4.24 GB %%%%%%%%%:::::
          2018-04-06 3.29 GB | 1.43 GB | 4.72 GB %%%%%%%%%%%:::::
          -------------------------+-------------+---------------------------------------
          sum of 5 18.25 GB | 8.17 GB | 26.43 GB





          share|improve this answer




















          • Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
            – WinEunuuchs2Unix
            May 5 at 23:21











          • I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
            – Teemu Toivola
            May 5 at 23:37










          • Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
            – WinEunuuchs2Unix
            May 5 at 23:58











          • Thanks for adding this feature. This solution seems much cleaner.
            – ICE
            May 6 at 1:17










          • I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
            – WinEunuuchs2Unix
            May 12 at 2:34












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          vnStat supports date and time range specific queries for all list outputs starting from version 2.0 (currently available as beta). That version also allows free configuration of data retention durations so there's no more hardcoded 30 day limit for daily data. See the change notes and the GitHub repository for more details.



          $ vnstat --days --begin 2018-04-02 --end 2018-04-06

          em1 / daily

          day rx | tx | total
          -------------------------+-------------+---------------------------------------
          2018-04-02 4.88 GB | 1.95 GB | 6.83 GB %%%%%%%%%%%%%%%%%:::::::
          2018-04-03 3.56 GB | 1.09 GB | 4.66 GB %%%%%%%%%%%%::::
          2018-04-04 3.91 GB | 2.07 GB | 5.99 GB %%%%%%%%%%%%%%:::::::
          2018-04-05 2.61 GB | 1.63 GB | 4.24 GB %%%%%%%%%:::::
          2018-04-06 3.29 GB | 1.43 GB | 4.72 GB %%%%%%%%%%%:::::
          -------------------------+-------------+---------------------------------------
          sum of 5 18.25 GB | 8.17 GB | 26.43 GB





          share|improve this answer












          vnStat supports date and time range specific queries for all list outputs starting from version 2.0 (currently available as beta). That version also allows free configuration of data retention durations so there's no more hardcoded 30 day limit for daily data. See the change notes and the GitHub repository for more details.



          $ vnstat --days --begin 2018-04-02 --end 2018-04-06

          em1 / daily

          day rx | tx | total
          -------------------------+-------------+---------------------------------------
          2018-04-02 4.88 GB | 1.95 GB | 6.83 GB %%%%%%%%%%%%%%%%%:::::::
          2018-04-03 3.56 GB | 1.09 GB | 4.66 GB %%%%%%%%%%%%::::
          2018-04-04 3.91 GB | 2.07 GB | 5.99 GB %%%%%%%%%%%%%%:::::::
          2018-04-05 2.61 GB | 1.63 GB | 4.24 GB %%%%%%%%%:::::
          2018-04-06 3.29 GB | 1.43 GB | 4.72 GB %%%%%%%%%%%:::::
          -------------------------+-------------+---------------------------------------
          sum of 5 18.25 GB | 8.17 GB | 26.43 GB






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 5 at 22:29









          Teemu Toivola

          26925




          26925











          • Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
            – WinEunuuchs2Unix
            May 5 at 23:21











          • I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
            – Teemu Toivola
            May 5 at 23:37










          • Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
            – WinEunuuchs2Unix
            May 5 at 23:58











          • Thanks for adding this feature. This solution seems much cleaner.
            – ICE
            May 6 at 1:17










          • I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
            – WinEunuuchs2Unix
            May 12 at 2:34
















          • Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
            – WinEunuuchs2Unix
            May 5 at 23:21











          • I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
            – Teemu Toivola
            May 5 at 23:37










          • Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
            – WinEunuuchs2Unix
            May 5 at 23:58











          • Thanks for adding this feature. This solution seems much cleaner.
            – ICE
            May 6 at 1:17










          • I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
            – WinEunuuchs2Unix
            May 12 at 2:34















          Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
          – WinEunuuchs2Unix
          May 5 at 23:21





          Even though I have my own answer +1 to yours for updating us. Thank you. I see why you didn't use 2018-05-02 and 2018-05-06 as parameters instead of 2018-04-02 and 2018-04-06. It's because the day history only increases from 30 days to 60 days?
          – WinEunuuchs2Unix
          May 5 at 23:21













          I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
          – Teemu Toivola
          May 5 at 23:37




          I currently have the configuration set at 60 days so it would be possible to go right now back to 2018-03-07. I used 2018-04-02 - 2018-04-06 mainly to show that the 30 day limit isn't there anymore and to limit the output to few entries. That 60 days isn't hardcoded either and can be changed at any time for the configuration file. It's even possible to disable the cleanup of old data altogether if needed.
          – Teemu Toivola
          May 5 at 23:37












          Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
          – WinEunuuchs2Unix
          May 5 at 23:58





          Apologies I just noticed you are the vnstat author and/or maintainer. Congrats on a great package that's a dailiy staple in many lives!
          – WinEunuuchs2Unix
          May 5 at 23:58













          Thanks for adding this feature. This solution seems much cleaner.
          – ICE
          May 6 at 1:17




          Thanks for adding this feature. This solution seems much cleaner.
          – ICE
          May 6 at 1:17












          I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
          – WinEunuuchs2Unix
          May 12 at 2:34




          I've just posted another question you might be interested in: askubuntu.com/questions/1035141/…
          – WinEunuuchs2Unix
          May 12 at 2:34












          up vote
          1
          down vote













          Copy the code below into a file. I'm using ~/bin/vnstat-hist.sh. After saving the file mark it as executable using:





          chmod a+x ~/bin/vnstat.sh


          To run the script call it with the parameter for number of days. For example for today use vnstat-hist.sh 1. For last five days (including today) use:



          $ vnstat-hist.sh 5
          vnstat -d 5 day summary
          2018-04-27 6.21 GiB | 1.83 GiB | 8.04 GiB | 780.45 kbit/s
          2018-04-28 5.97 GiB | 1.05 GiB | 7.02 GiB | 681.20 kbit/s
          2018-04-29 8.27 GiB | 1.47 GiB | 9.74 GiB | 945.40 kbit/s
          2018-04-30 4.09 GiB | 1.35 GiB | 5.44 GiB | 527.97 kbit/s
          2018-05-01 1.36 GiB | 1.13 GiB | 2.49 GiB | 315.40 kbit/s
          Total:32.73



          vnstat-hist.sh Bash script



          Note this program can be shorter but hopefully the design is easier to follow for novices.



          #!/bin/bash

          # NAME: vnstat-hist.sh
          # PATH: $HOME/bin
          # DESC: Written for AU Q&A: https://askubuntu.com/questions/1030345/get-network-usage-from-specific-date-on-terminal/1030399?noredirect=1#comment1675801_1030399
          # Get total vnStat bytes from x days ago to today.
          # Parameter 1 = number of days: 1= today, 2= yesterday + today, etc.

          # DATE: May 1, 2018.

          re='^[0-9]+$'
          if ! [[ $1 =~ $re ]] ; then
          echo "Error: Parameter 1 must be number of days" >&2; exit 1
          fi

          # Get body of vnstat -d into file, ie strip headings and total lines
          # First get count of all lines, then delete 2 total lines & 5 heading lines

          vnstat -d > /tmp/vnstat-hist.txt
          NumLines=$(cat /tmp/vnstat-hist.txt | wc -l)
          NumLines=$(( NumLines - 2))
          cat /tmp/vnstat-hist.txt | head -n $NumLines > /tmp/vnstat-hist2.txt
          NumLines=$(( NumLines - 5))
          cat /tmp/vnstat-hist2.txt | tail -n $NumLines > /tmp/vnstat-hist.txt

          MaxDays=$(cat /tmp/vnstat-hist.txt | wc -l)

          DayCount="$1"
          (( $DayCount > $MaxDays )) && DayCount=$MaxDays
          cat /tmp/vnstat-hist.txt | tail -n $DayCount > /tmp/vnstat-hist2.txt
          echo "vnstat -d $DayCount day summary"
          awk 'sum+=$8; END print "Total:" sum 1' /tmp/vnstat-hist2.txt

          # Clean up temp files
          rm -f /tmp/vnstat-hist.txt
          rm -f /tmp/vnstat-hist2.txt

          exit 0





          share|improve this answer






















          • Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
            – ICE
            May 1 at 11:52










          • Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
            – WinEunuuchs2Unix
            May 1 at 15:09










          • Yes. It's from the past 30 days.
            – ICE
            May 1 at 15:15






          • 1




            @ICE You're welcome. I enjoyed learning some new things putting it together.
            – WinEunuuchs2Unix
            May 2 at 0:47






          • 1




            @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
            – WinEunuuchs2Unix
            May 6 at 1:49














          up vote
          1
          down vote













          Copy the code below into a file. I'm using ~/bin/vnstat-hist.sh. After saving the file mark it as executable using:





          chmod a+x ~/bin/vnstat.sh


          To run the script call it with the parameter for number of days. For example for today use vnstat-hist.sh 1. For last five days (including today) use:



          $ vnstat-hist.sh 5
          vnstat -d 5 day summary
          2018-04-27 6.21 GiB | 1.83 GiB | 8.04 GiB | 780.45 kbit/s
          2018-04-28 5.97 GiB | 1.05 GiB | 7.02 GiB | 681.20 kbit/s
          2018-04-29 8.27 GiB | 1.47 GiB | 9.74 GiB | 945.40 kbit/s
          2018-04-30 4.09 GiB | 1.35 GiB | 5.44 GiB | 527.97 kbit/s
          2018-05-01 1.36 GiB | 1.13 GiB | 2.49 GiB | 315.40 kbit/s
          Total:32.73



          vnstat-hist.sh Bash script



          Note this program can be shorter but hopefully the design is easier to follow for novices.



          #!/bin/bash

          # NAME: vnstat-hist.sh
          # PATH: $HOME/bin
          # DESC: Written for AU Q&A: https://askubuntu.com/questions/1030345/get-network-usage-from-specific-date-on-terminal/1030399?noredirect=1#comment1675801_1030399
          # Get total vnStat bytes from x days ago to today.
          # Parameter 1 = number of days: 1= today, 2= yesterday + today, etc.

          # DATE: May 1, 2018.

          re='^[0-9]+$'
          if ! [[ $1 =~ $re ]] ; then
          echo "Error: Parameter 1 must be number of days" >&2; exit 1
          fi

          # Get body of vnstat -d into file, ie strip headings and total lines
          # First get count of all lines, then delete 2 total lines & 5 heading lines

          vnstat -d > /tmp/vnstat-hist.txt
          NumLines=$(cat /tmp/vnstat-hist.txt | wc -l)
          NumLines=$(( NumLines - 2))
          cat /tmp/vnstat-hist.txt | head -n $NumLines > /tmp/vnstat-hist2.txt
          NumLines=$(( NumLines - 5))
          cat /tmp/vnstat-hist2.txt | tail -n $NumLines > /tmp/vnstat-hist.txt

          MaxDays=$(cat /tmp/vnstat-hist.txt | wc -l)

          DayCount="$1"
          (( $DayCount > $MaxDays )) && DayCount=$MaxDays
          cat /tmp/vnstat-hist.txt | tail -n $DayCount > /tmp/vnstat-hist2.txt
          echo "vnstat -d $DayCount day summary"
          awk 'sum+=$8; END print "Total:" sum 1' /tmp/vnstat-hist2.txt

          # Clean up temp files
          rm -f /tmp/vnstat-hist.txt
          rm -f /tmp/vnstat-hist2.txt

          exit 0





          share|improve this answer






















          • Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
            – ICE
            May 1 at 11:52










          • Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
            – WinEunuuchs2Unix
            May 1 at 15:09










          • Yes. It's from the past 30 days.
            – ICE
            May 1 at 15:15






          • 1




            @ICE You're welcome. I enjoyed learning some new things putting it together.
            – WinEunuuchs2Unix
            May 2 at 0:47






          • 1




            @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
            – WinEunuuchs2Unix
            May 6 at 1:49












          up vote
          1
          down vote










          up vote
          1
          down vote









          Copy the code below into a file. I'm using ~/bin/vnstat-hist.sh. After saving the file mark it as executable using:





          chmod a+x ~/bin/vnstat.sh


          To run the script call it with the parameter for number of days. For example for today use vnstat-hist.sh 1. For last five days (including today) use:



          $ vnstat-hist.sh 5
          vnstat -d 5 day summary
          2018-04-27 6.21 GiB | 1.83 GiB | 8.04 GiB | 780.45 kbit/s
          2018-04-28 5.97 GiB | 1.05 GiB | 7.02 GiB | 681.20 kbit/s
          2018-04-29 8.27 GiB | 1.47 GiB | 9.74 GiB | 945.40 kbit/s
          2018-04-30 4.09 GiB | 1.35 GiB | 5.44 GiB | 527.97 kbit/s
          2018-05-01 1.36 GiB | 1.13 GiB | 2.49 GiB | 315.40 kbit/s
          Total:32.73



          vnstat-hist.sh Bash script



          Note this program can be shorter but hopefully the design is easier to follow for novices.



          #!/bin/bash

          # NAME: vnstat-hist.sh
          # PATH: $HOME/bin
          # DESC: Written for AU Q&A: https://askubuntu.com/questions/1030345/get-network-usage-from-specific-date-on-terminal/1030399?noredirect=1#comment1675801_1030399
          # Get total vnStat bytes from x days ago to today.
          # Parameter 1 = number of days: 1= today, 2= yesterday + today, etc.

          # DATE: May 1, 2018.

          re='^[0-9]+$'
          if ! [[ $1 =~ $re ]] ; then
          echo "Error: Parameter 1 must be number of days" >&2; exit 1
          fi

          # Get body of vnstat -d into file, ie strip headings and total lines
          # First get count of all lines, then delete 2 total lines & 5 heading lines

          vnstat -d > /tmp/vnstat-hist.txt
          NumLines=$(cat /tmp/vnstat-hist.txt | wc -l)
          NumLines=$(( NumLines - 2))
          cat /tmp/vnstat-hist.txt | head -n $NumLines > /tmp/vnstat-hist2.txt
          NumLines=$(( NumLines - 5))
          cat /tmp/vnstat-hist2.txt | tail -n $NumLines > /tmp/vnstat-hist.txt

          MaxDays=$(cat /tmp/vnstat-hist.txt | wc -l)

          DayCount="$1"
          (( $DayCount > $MaxDays )) && DayCount=$MaxDays
          cat /tmp/vnstat-hist.txt | tail -n $DayCount > /tmp/vnstat-hist2.txt
          echo "vnstat -d $DayCount day summary"
          awk 'sum+=$8; END print "Total:" sum 1' /tmp/vnstat-hist2.txt

          # Clean up temp files
          rm -f /tmp/vnstat-hist.txt
          rm -f /tmp/vnstat-hist2.txt

          exit 0





          share|improve this answer














          Copy the code below into a file. I'm using ~/bin/vnstat-hist.sh. After saving the file mark it as executable using:





          chmod a+x ~/bin/vnstat.sh


          To run the script call it with the parameter for number of days. For example for today use vnstat-hist.sh 1. For last five days (including today) use:



          $ vnstat-hist.sh 5
          vnstat -d 5 day summary
          2018-04-27 6.21 GiB | 1.83 GiB | 8.04 GiB | 780.45 kbit/s
          2018-04-28 5.97 GiB | 1.05 GiB | 7.02 GiB | 681.20 kbit/s
          2018-04-29 8.27 GiB | 1.47 GiB | 9.74 GiB | 945.40 kbit/s
          2018-04-30 4.09 GiB | 1.35 GiB | 5.44 GiB | 527.97 kbit/s
          2018-05-01 1.36 GiB | 1.13 GiB | 2.49 GiB | 315.40 kbit/s
          Total:32.73



          vnstat-hist.sh Bash script



          Note this program can be shorter but hopefully the design is easier to follow for novices.



          #!/bin/bash

          # NAME: vnstat-hist.sh
          # PATH: $HOME/bin
          # DESC: Written for AU Q&A: https://askubuntu.com/questions/1030345/get-network-usage-from-specific-date-on-terminal/1030399?noredirect=1#comment1675801_1030399
          # Get total vnStat bytes from x days ago to today.
          # Parameter 1 = number of days: 1= today, 2= yesterday + today, etc.

          # DATE: May 1, 2018.

          re='^[0-9]+$'
          if ! [[ $1 =~ $re ]] ; then
          echo "Error: Parameter 1 must be number of days" >&2; exit 1
          fi

          # Get body of vnstat -d into file, ie strip headings and total lines
          # First get count of all lines, then delete 2 total lines & 5 heading lines

          vnstat -d > /tmp/vnstat-hist.txt
          NumLines=$(cat /tmp/vnstat-hist.txt | wc -l)
          NumLines=$(( NumLines - 2))
          cat /tmp/vnstat-hist.txt | head -n $NumLines > /tmp/vnstat-hist2.txt
          NumLines=$(( NumLines - 5))
          cat /tmp/vnstat-hist2.txt | tail -n $NumLines > /tmp/vnstat-hist.txt

          MaxDays=$(cat /tmp/vnstat-hist.txt | wc -l)

          DayCount="$1"
          (( $DayCount > $MaxDays )) && DayCount=$MaxDays
          cat /tmp/vnstat-hist.txt | tail -n $DayCount > /tmp/vnstat-hist2.txt
          echo "vnstat -d $DayCount day summary"
          awk 'sum+=$8; END print "Total:" sum 1' /tmp/vnstat-hist2.txt

          # Clean up temp files
          rm -f /tmp/vnstat-hist.txt
          rm -f /tmp/vnstat-hist2.txt

          exit 0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 2 at 0:50

























          answered May 1 at 4:15









          WinEunuuchs2Unix

          35.4k758132




          35.4k758132











          • Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
            – ICE
            May 1 at 11:52










          • Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
            – WinEunuuchs2Unix
            May 1 at 15:09










          • Yes. It's from the past 30 days.
            – ICE
            May 1 at 15:15






          • 1




            @ICE You're welcome. I enjoyed learning some new things putting it together.
            – WinEunuuchs2Unix
            May 2 at 0:47






          • 1




            @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
            – WinEunuuchs2Unix
            May 6 at 1:49
















          • Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
            – ICE
            May 1 at 11:52










          • Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
            – WinEunuuchs2Unix
            May 1 at 15:09










          • Yes. It's from the past 30 days.
            – ICE
            May 1 at 15:15






          • 1




            @ICE You're welcome. I enjoyed learning some new things putting it together.
            – WinEunuuchs2Unix
            May 2 at 0:47






          • 1




            @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
            – WinEunuuchs2Unix
            May 6 at 1:49















          Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
          – ICE
          May 1 at 11:52




          Thanks but I don't want to get network usage on specific date, I want to get network usage from specific date to the current date (now).
          – ICE
          May 1 at 11:52












          Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
          – WinEunuuchs2Unix
          May 1 at 15:09




          Is the date more than 30 days in the past? If so the records may have to be logged to a history file using cron each month.
          – WinEunuuchs2Unix
          May 1 at 15:09












          Yes. It's from the past 30 days.
          – ICE
          May 1 at 15:15




          Yes. It's from the past 30 days.
          – ICE
          May 1 at 15:15




          1




          1




          @ICE You're welcome. I enjoyed learning some new things putting it together.
          – WinEunuuchs2Unix
          May 2 at 0:47




          @ICE You're welcome. I enjoyed learning some new things putting it together.
          – WinEunuuchs2Unix
          May 2 at 0:47




          1




          1




          @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
          – WinEunuuchs2Unix
          May 6 at 1:49




          @ICE I agree 100%. Unfortunately I'm using an older version of vnStat in Ubuntu 16.04 so that option wasn't available when I wrote my answer.
          – WinEunuuchs2Unix
          May 6 at 1:49












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1030345%2fget-network-usage-from-specific-date-on-terminal%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

          Trouble downloading packages list due to a “Hash sum mismatch” error

          How do I move numbers in filenames, in a batch renaming operation?