Shell script to create one file and append the results

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












2















The following script will run every 1 hour. The script creates a new file each time it has run.

The first echo will push header and the for loop will push the results.



But what I additionally want is:



  • every day the header should go only once into the file and it should keep appending the for loop results to the same file.

  • it should create only one CSV file per day.

  • it should delete any old files with name result_ something.

The code I have



echo "collectiontime,hostname,diskusage,directory" > /home/result_$CURR_TIME_EPOCH.csv

for i in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
do
T=`sudo du -sh $i 2> /dev/null`
if [ $? -eq 0 ]; then
T=`echo $T | awk 'print $1","$2'`
echo `date +%s`,$HOSTNAME,$T
fi

done >> /home/result_$CURR_TIME_EPOCH.csv









share|improve this question




























    2















    The following script will run every 1 hour. The script creates a new file each time it has run.

    The first echo will push header and the for loop will push the results.



    But what I additionally want is:



    • every day the header should go only once into the file and it should keep appending the for loop results to the same file.

    • it should create only one CSV file per day.

    • it should delete any old files with name result_ something.

    The code I have



    echo "collectiontime,hostname,diskusage,directory" > /home/result_$CURR_TIME_EPOCH.csv

    for i in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
    do
    T=`sudo du -sh $i 2> /dev/null`
    if [ $? -eq 0 ]; then
    T=`echo $T | awk 'print $1","$2'`
    echo `date +%s`,$HOSTNAME,$T
    fi

    done >> /home/result_$CURR_TIME_EPOCH.csv









    share|improve this question


























      2












      2








      2








      The following script will run every 1 hour. The script creates a new file each time it has run.

      The first echo will push header and the for loop will push the results.



      But what I additionally want is:



      • every day the header should go only once into the file and it should keep appending the for loop results to the same file.

      • it should create only one CSV file per day.

      • it should delete any old files with name result_ something.

      The code I have



      echo "collectiontime,hostname,diskusage,directory" > /home/result_$CURR_TIME_EPOCH.csv

      for i in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
      do
      T=`sudo du -sh $i 2> /dev/null`
      if [ $? -eq 0 ]; then
      T=`echo $T | awk 'print $1","$2'`
      echo `date +%s`,$HOSTNAME,$T
      fi

      done >> /home/result_$CURR_TIME_EPOCH.csv









      share|improve this question
















      The following script will run every 1 hour. The script creates a new file each time it has run.

      The first echo will push header and the for loop will push the results.



      But what I additionally want is:



      • every day the header should go only once into the file and it should keep appending the for loop results to the same file.

      • it should create only one CSV file per day.

      • it should delete any old files with name result_ something.

      The code I have



      echo "collectiontime,hostname,diskusage,directory" > /home/result_$CURR_TIME_EPOCH.csv

      for i in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
      do
      T=`sudo du -sh $i 2> /dev/null`
      if [ $? -eq 0 ]; then
      T=`echo $T | awk 'print $1","$2'`
      echo `date +%s`,$HOSTNAME,$T
      fi

      done >> /home/result_$CURR_TIME_EPOCH.csv






      shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 11 at 23:07









      guntbert

      1,06111017




      1,06111017










      asked Jan 11 at 19:58









      john doejohn doe

      111




      111




















          2 Answers
          2






          active

          oldest

          votes


















          2














          Just test if the file exists, and if not: echo the header:



          if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
          echo "collectiontime,hostname,diskusage,directory" > "/home/result_$CURR_TIME_EPOCH.csv"
          fi





          share|improve this answer
































            0














            As Ljm Dullaart suggests, only output the header if the output file does not exist.



            This is a suggestion for general improvement of your script (assuming GNU awk or mawk):



            if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
            echo 'collectiontime,hostname,diskusage,directory' >"/home/result_$CURR_TIME_EPOCH.csv"
            fi

            for dir in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
            do
            sudo du -sh "$dir" |
            awk -v OFS=',' ' print strftime("%s"), $1, $2 '
            done >>"/home/result_$CURR_TIME_EPOCH.csv" 2>/dev/null


            If you are using bash, you may benefit in terms of readability, from using an array for the directories (and a separate variable for the output file):



            dirs=( /data01
            /opt
            /opt/splunk
            /opt/splunk/var/lib/splunk/kvstore
            /opt/splunk/var/run/searchpeers
            /opt/splunk/var/run )

            outfile="/home/result_$CURR_TIME_EPOCH.csv"

            if [ ! -f "$outfile" ]; then
            echo 'collectiontime,hostname,diskusage,directory' >"$outfile"
            fi

            for dir in "$dirs[@]"; do
            sudo du -sh "$dir" |
            awk -v OFS=',' ' print strftime("%s"), $1, $2 '
            done >>"$outfile" 2>/dev/null


            To delete old files, you could use, for example,



            find /home -maxdepth 1 -type f -name 'result_*.cvs' -ctime +1 -delete


            This would find and delete all regular files that has names matching the given pattern and that has a ctime timestamp of more than one day ago. Subdirectories of /home would not be searched thanks to -maxdepth 1.






            share|improve this answer






















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494008%2fshell-script-to-create-one-file-and-append-the-results%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Just test if the file exists, and if not: echo the header:



              if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
              echo "collectiontime,hostname,diskusage,directory" > "/home/result_$CURR_TIME_EPOCH.csv"
              fi





              share|improve this answer





























                2














                Just test if the file exists, and if not: echo the header:



                if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                echo "collectiontime,hostname,diskusage,directory" > "/home/result_$CURR_TIME_EPOCH.csv"
                fi





                share|improve this answer



























                  2












                  2








                  2







                  Just test if the file exists, and if not: echo the header:



                  if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                  echo "collectiontime,hostname,diskusage,directory" > "/home/result_$CURR_TIME_EPOCH.csv"
                  fi





                  share|improve this answer















                  Just test if the file exists, and if not: echo the header:



                  if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                  echo "collectiontime,hostname,diskusage,directory" > "/home/result_$CURR_TIME_EPOCH.csv"
                  fi






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 11 at 22:11









                  Kusalananda

                  127k16239393




                  127k16239393










                  answered Jan 11 at 22:04









                  Ljm DullaartLjm Dullaart

                  61829




                  61829























                      0














                      As Ljm Dullaart suggests, only output the header if the output file does not exist.



                      This is a suggestion for general improvement of your script (assuming GNU awk or mawk):



                      if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                      echo 'collectiontime,hostname,diskusage,directory' >"/home/result_$CURR_TIME_EPOCH.csv"
                      fi

                      for dir in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
                      do
                      sudo du -sh "$dir" |
                      awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                      done >>"/home/result_$CURR_TIME_EPOCH.csv" 2>/dev/null


                      If you are using bash, you may benefit in terms of readability, from using an array for the directories (and a separate variable for the output file):



                      dirs=( /data01
                      /opt
                      /opt/splunk
                      /opt/splunk/var/lib/splunk/kvstore
                      /opt/splunk/var/run/searchpeers
                      /opt/splunk/var/run )

                      outfile="/home/result_$CURR_TIME_EPOCH.csv"

                      if [ ! -f "$outfile" ]; then
                      echo 'collectiontime,hostname,diskusage,directory' >"$outfile"
                      fi

                      for dir in "$dirs[@]"; do
                      sudo du -sh "$dir" |
                      awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                      done >>"$outfile" 2>/dev/null


                      To delete old files, you could use, for example,



                      find /home -maxdepth 1 -type f -name 'result_*.cvs' -ctime +1 -delete


                      This would find and delete all regular files that has names matching the given pattern and that has a ctime timestamp of more than one day ago. Subdirectories of /home would not be searched thanks to -maxdepth 1.






                      share|improve this answer



























                        0














                        As Ljm Dullaart suggests, only output the header if the output file does not exist.



                        This is a suggestion for general improvement of your script (assuming GNU awk or mawk):



                        if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                        echo 'collectiontime,hostname,diskusage,directory' >"/home/result_$CURR_TIME_EPOCH.csv"
                        fi

                        for dir in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
                        do
                        sudo du -sh "$dir" |
                        awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                        done >>"/home/result_$CURR_TIME_EPOCH.csv" 2>/dev/null


                        If you are using bash, you may benefit in terms of readability, from using an array for the directories (and a separate variable for the output file):



                        dirs=( /data01
                        /opt
                        /opt/splunk
                        /opt/splunk/var/lib/splunk/kvstore
                        /opt/splunk/var/run/searchpeers
                        /opt/splunk/var/run )

                        outfile="/home/result_$CURR_TIME_EPOCH.csv"

                        if [ ! -f "$outfile" ]; then
                        echo 'collectiontime,hostname,diskusage,directory' >"$outfile"
                        fi

                        for dir in "$dirs[@]"; do
                        sudo du -sh "$dir" |
                        awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                        done >>"$outfile" 2>/dev/null


                        To delete old files, you could use, for example,



                        find /home -maxdepth 1 -type f -name 'result_*.cvs' -ctime +1 -delete


                        This would find and delete all regular files that has names matching the given pattern and that has a ctime timestamp of more than one day ago. Subdirectories of /home would not be searched thanks to -maxdepth 1.






                        share|improve this answer

























                          0












                          0








                          0







                          As Ljm Dullaart suggests, only output the header if the output file does not exist.



                          This is a suggestion for general improvement of your script (assuming GNU awk or mawk):



                          if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                          echo 'collectiontime,hostname,diskusage,directory' >"/home/result_$CURR_TIME_EPOCH.csv"
                          fi

                          for dir in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
                          do
                          sudo du -sh "$dir" |
                          awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                          done >>"/home/result_$CURR_TIME_EPOCH.csv" 2>/dev/null


                          If you are using bash, you may benefit in terms of readability, from using an array for the directories (and a separate variable for the output file):



                          dirs=( /data01
                          /opt
                          /opt/splunk
                          /opt/splunk/var/lib/splunk/kvstore
                          /opt/splunk/var/run/searchpeers
                          /opt/splunk/var/run )

                          outfile="/home/result_$CURR_TIME_EPOCH.csv"

                          if [ ! -f "$outfile" ]; then
                          echo 'collectiontime,hostname,diskusage,directory' >"$outfile"
                          fi

                          for dir in "$dirs[@]"; do
                          sudo du -sh "$dir" |
                          awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                          done >>"$outfile" 2>/dev/null


                          To delete old files, you could use, for example,



                          find /home -maxdepth 1 -type f -name 'result_*.cvs' -ctime +1 -delete


                          This would find and delete all regular files that has names matching the given pattern and that has a ctime timestamp of more than one day ago. Subdirectories of /home would not be searched thanks to -maxdepth 1.






                          share|improve this answer













                          As Ljm Dullaart suggests, only output the header if the output file does not exist.



                          This is a suggestion for general improvement of your script (assuming GNU awk or mawk):



                          if [ ! -f "/home/result_$CURR_TIME_EPOCH.csv" ]; then 
                          echo 'collectiontime,hostname,diskusage,directory' >"/home/result_$CURR_TIME_EPOCH.csv"
                          fi

                          for dir in /data01 /opt /opt/splunk /opt/splunk/var/lib/splunk/kvstore /opt/splunk/var/run/searchpeers /opt/splunk/var/run
                          do
                          sudo du -sh "$dir" |
                          awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                          done >>"/home/result_$CURR_TIME_EPOCH.csv" 2>/dev/null


                          If you are using bash, you may benefit in terms of readability, from using an array for the directories (and a separate variable for the output file):



                          dirs=( /data01
                          /opt
                          /opt/splunk
                          /opt/splunk/var/lib/splunk/kvstore
                          /opt/splunk/var/run/searchpeers
                          /opt/splunk/var/run )

                          outfile="/home/result_$CURR_TIME_EPOCH.csv"

                          if [ ! -f "$outfile" ]; then
                          echo 'collectiontime,hostname,diskusage,directory' >"$outfile"
                          fi

                          for dir in "$dirs[@]"; do
                          sudo du -sh "$dir" |
                          awk -v OFS=',' ' print strftime("%s"), $1, $2 '
                          done >>"$outfile" 2>/dev/null


                          To delete old files, you could use, for example,



                          find /home -maxdepth 1 -type f -name 'result_*.cvs' -ctime +1 -delete


                          This would find and delete all regular files that has names matching the given pattern and that has a ctime timestamp of more than one day ago. Subdirectories of /home would not be searched thanks to -maxdepth 1.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 11 at 22:23









                          KusalanandaKusalananda

                          127k16239393




                          127k16239393



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494008%2fshell-script-to-create-one-file-and-append-the-results%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown






                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)