Shell script to delete oldest files and folders












4














I have a system running. It creates 1 folder for each day, naming it by date. Inside each folder, it stores videos from security cameras, naming the files by time. You'd have something like this:



You'd have something like this:



The folders are uploaded to the cloud, file by file, as it is created. However, my local storage limit is 16GB.



This way, I've made a script (in bash) to keep deleting old files as storage reaches a certain percentage. This is what the script should do:




  • Detects the storage %.


    • If the storage percentage is at 75%:


      • Count the number of folders in the output's root folder.


        • If more than one folder:


          • Go to the oldest folder and count files.


            • If it has files, delete the 10 oldest files.

            • If it is empty, go back to the output's root folder and delete the oldest empty folder.





        • If only one folder:


          • Go to the only folder and delete the 10 oldest files.









  • The cron job will run the script every minute, but the script will only delete stuff if the main condition is met.


This is the script itself (https://github.com/andreluisos/motioneyeos/blob/master/storage_cleaner_script/storage_cleaner_script.sh):



#! /bin/sh

##### VARIABLES #####

RootFolder="/data/output/Camera1" #Must change to your default output folder. Originally, it's "/data/output/Camera1".
cd $RootFolder
FileCounter=$(find . -name "*.mkv" -o -name "*.avi" -o -name "*.swf" -o -name "*.flv" -o -name "*.mov" -o -name "*.mp4" | wc -l | xargs) #Must change, if the output format if different than the basic ones available originally on MotionEyeOS.
FolderCounter=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | xargs)
CurrentStorage=`df -h | grep -vE "^Filesystem|tmpfs|/tmp|/boot|/home|/dev/root" | awk '{print $5}' | cut -d'%' -f1`
StorageThreshold=75 #Define the storage % to trigger the script.

##### FUNCTIONS #####

function multiplefolders () {
echo "More than one folder detected."
cd `ls -Ft | grep '/$' | tail -1`
echo "Entering the oldest folder..."
if [ "$FileCounter" -eq 0 ];
then
echo "No Files detected."
cd $RootFolder
echo "Going back to the output's root folder..."
rm -rf `ls -Ft | grep '/$' | tail -1`
echo "Detecting and removing the oldest empty folder...";
else
ls -t | tail -10 | xargs rm
echo "Deleting the oldest files...";
fi
}

function singlefolder () {
echo "Only one folder detected."
echo "Entering the only folder..."
cd `ls -Ft | grep '/$' | head -1`
ls -t | tail -10 | xargs rm
echo "Deleting the oldest files."
}

function checkfolders () {
if [ "$FolderCounter" -gt 1 ];
then
multiplefolders
fi

if [ "$FolderCounter" -eq 1 ];
then
singlefolder
fi

if [ "$FolderCounter" -eq 0 ];
then
echo "No folders detected. Please check if the output folder's path is correct."
fi
}

##### SCRIPT STARTER #####

if [ $CurrentStorage -ge $StorageThreshold ];
then
checkfolders
else
echo "Storage threshold not yet reached."
fi


Thing is that the script isn't (apparently) counting the number of files inside the oldest folder (when more then one folder is detected) correctly.



Instead of going back to the root folder and delete the oldest empty folder, it keeps deleting files from the newest folder (apparently).



In other words, when you have 2 folders (the oldest one empty and the newest one with videos in it), it should delete the oldest and empty folder, but I keep getting:



More than one folder detected.
Entering the oldest folder...
Deleting the oldest files...









share|improve this question





























    4














    I have a system running. It creates 1 folder for each day, naming it by date. Inside each folder, it stores videos from security cameras, naming the files by time. You'd have something like this:



    You'd have something like this:



    The folders are uploaded to the cloud, file by file, as it is created. However, my local storage limit is 16GB.



    This way, I've made a script (in bash) to keep deleting old files as storage reaches a certain percentage. This is what the script should do:




    • Detects the storage %.


      • If the storage percentage is at 75%:


        • Count the number of folders in the output's root folder.


          • If more than one folder:


            • Go to the oldest folder and count files.


              • If it has files, delete the 10 oldest files.

              • If it is empty, go back to the output's root folder and delete the oldest empty folder.





          • If only one folder:


            • Go to the only folder and delete the 10 oldest files.









    • The cron job will run the script every minute, but the script will only delete stuff if the main condition is met.


    This is the script itself (https://github.com/andreluisos/motioneyeos/blob/master/storage_cleaner_script/storage_cleaner_script.sh):



    #! /bin/sh

    ##### VARIABLES #####

    RootFolder="/data/output/Camera1" #Must change to your default output folder. Originally, it's "/data/output/Camera1".
    cd $RootFolder
    FileCounter=$(find . -name "*.mkv" -o -name "*.avi" -o -name "*.swf" -o -name "*.flv" -o -name "*.mov" -o -name "*.mp4" | wc -l | xargs) #Must change, if the output format if different than the basic ones available originally on MotionEyeOS.
    FolderCounter=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | xargs)
    CurrentStorage=`df -h | grep -vE "^Filesystem|tmpfs|/tmp|/boot|/home|/dev/root" | awk '{print $5}' | cut -d'%' -f1`
    StorageThreshold=75 #Define the storage % to trigger the script.

    ##### FUNCTIONS #####

    function multiplefolders () {
    echo "More than one folder detected."
    cd `ls -Ft | grep '/$' | tail -1`
    echo "Entering the oldest folder..."
    if [ "$FileCounter" -eq 0 ];
    then
    echo "No Files detected."
    cd $RootFolder
    echo "Going back to the output's root folder..."
    rm -rf `ls -Ft | grep '/$' | tail -1`
    echo "Detecting and removing the oldest empty folder...";
    else
    ls -t | tail -10 | xargs rm
    echo "Deleting the oldest files...";
    fi
    }

    function singlefolder () {
    echo "Only one folder detected."
    echo "Entering the only folder..."
    cd `ls -Ft | grep '/$' | head -1`
    ls -t | tail -10 | xargs rm
    echo "Deleting the oldest files."
    }

    function checkfolders () {
    if [ "$FolderCounter" -gt 1 ];
    then
    multiplefolders
    fi

    if [ "$FolderCounter" -eq 1 ];
    then
    singlefolder
    fi

    if [ "$FolderCounter" -eq 0 ];
    then
    echo "No folders detected. Please check if the output folder's path is correct."
    fi
    }

    ##### SCRIPT STARTER #####

    if [ $CurrentStorage -ge $StorageThreshold ];
    then
    checkfolders
    else
    echo "Storage threshold not yet reached."
    fi


    Thing is that the script isn't (apparently) counting the number of files inside the oldest folder (when more then one folder is detected) correctly.



    Instead of going back to the root folder and delete the oldest empty folder, it keeps deleting files from the newest folder (apparently).



    In other words, when you have 2 folders (the oldest one empty and the newest one with videos in it), it should delete the oldest and empty folder, but I keep getting:



    More than one folder detected.
    Entering the oldest folder...
    Deleting the oldest files...









    share|improve this question



























      4












      4








      4


      2





      I have a system running. It creates 1 folder for each day, naming it by date. Inside each folder, it stores videos from security cameras, naming the files by time. You'd have something like this:



      You'd have something like this:



      The folders are uploaded to the cloud, file by file, as it is created. However, my local storage limit is 16GB.



      This way, I've made a script (in bash) to keep deleting old files as storage reaches a certain percentage. This is what the script should do:




      • Detects the storage %.


        • If the storage percentage is at 75%:


          • Count the number of folders in the output's root folder.


            • If more than one folder:


              • Go to the oldest folder and count files.


                • If it has files, delete the 10 oldest files.

                • If it is empty, go back to the output's root folder and delete the oldest empty folder.





            • If only one folder:


              • Go to the only folder and delete the 10 oldest files.









      • The cron job will run the script every minute, but the script will only delete stuff if the main condition is met.


      This is the script itself (https://github.com/andreluisos/motioneyeos/blob/master/storage_cleaner_script/storage_cleaner_script.sh):



      #! /bin/sh

      ##### VARIABLES #####

      RootFolder="/data/output/Camera1" #Must change to your default output folder. Originally, it's "/data/output/Camera1".
      cd $RootFolder
      FileCounter=$(find . -name "*.mkv" -o -name "*.avi" -o -name "*.swf" -o -name "*.flv" -o -name "*.mov" -o -name "*.mp4" | wc -l | xargs) #Must change, if the output format if different than the basic ones available originally on MotionEyeOS.
      FolderCounter=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | xargs)
      CurrentStorage=`df -h | grep -vE "^Filesystem|tmpfs|/tmp|/boot|/home|/dev/root" | awk '{print $5}' | cut -d'%' -f1`
      StorageThreshold=75 #Define the storage % to trigger the script.

      ##### FUNCTIONS #####

      function multiplefolders () {
      echo "More than one folder detected."
      cd `ls -Ft | grep '/$' | tail -1`
      echo "Entering the oldest folder..."
      if [ "$FileCounter" -eq 0 ];
      then
      echo "No Files detected."
      cd $RootFolder
      echo "Going back to the output's root folder..."
      rm -rf `ls -Ft | grep '/$' | tail -1`
      echo "Detecting and removing the oldest empty folder...";
      else
      ls -t | tail -10 | xargs rm
      echo "Deleting the oldest files...";
      fi
      }

      function singlefolder () {
      echo "Only one folder detected."
      echo "Entering the only folder..."
      cd `ls -Ft | grep '/$' | head -1`
      ls -t | tail -10 | xargs rm
      echo "Deleting the oldest files."
      }

      function checkfolders () {
      if [ "$FolderCounter" -gt 1 ];
      then
      multiplefolders
      fi

      if [ "$FolderCounter" -eq 1 ];
      then
      singlefolder
      fi

      if [ "$FolderCounter" -eq 0 ];
      then
      echo "No folders detected. Please check if the output folder's path is correct."
      fi
      }

      ##### SCRIPT STARTER #####

      if [ $CurrentStorage -ge $StorageThreshold ];
      then
      checkfolders
      else
      echo "Storage threshold not yet reached."
      fi


      Thing is that the script isn't (apparently) counting the number of files inside the oldest folder (when more then one folder is detected) correctly.



      Instead of going back to the root folder and delete the oldest empty folder, it keeps deleting files from the newest folder (apparently).



      In other words, when you have 2 folders (the oldest one empty and the newest one with videos in it), it should delete the oldest and empty folder, but I keep getting:



      More than one folder detected.
      Entering the oldest folder...
      Deleting the oldest files...









      share|improve this question















      I have a system running. It creates 1 folder for each day, naming it by date. Inside each folder, it stores videos from security cameras, naming the files by time. You'd have something like this:



      You'd have something like this:



      The folders are uploaded to the cloud, file by file, as it is created. However, my local storage limit is 16GB.



      This way, I've made a script (in bash) to keep deleting old files as storage reaches a certain percentage. This is what the script should do:




      • Detects the storage %.


        • If the storage percentage is at 75%:


          • Count the number of folders in the output's root folder.


            • If more than one folder:


              • Go to the oldest folder and count files.


                • If it has files, delete the 10 oldest files.

                • If it is empty, go back to the output's root folder and delete the oldest empty folder.





            • If only one folder:


              • Go to the only folder and delete the 10 oldest files.









      • The cron job will run the script every minute, but the script will only delete stuff if the main condition is met.


      This is the script itself (https://github.com/andreluisos/motioneyeos/blob/master/storage_cleaner_script/storage_cleaner_script.sh):



      #! /bin/sh

      ##### VARIABLES #####

      RootFolder="/data/output/Camera1" #Must change to your default output folder. Originally, it's "/data/output/Camera1".
      cd $RootFolder
      FileCounter=$(find . -name "*.mkv" -o -name "*.avi" -o -name "*.swf" -o -name "*.flv" -o -name "*.mov" -o -name "*.mp4" | wc -l | xargs) #Must change, if the output format if different than the basic ones available originally on MotionEyeOS.
      FolderCounter=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | xargs)
      CurrentStorage=`df -h | grep -vE "^Filesystem|tmpfs|/tmp|/boot|/home|/dev/root" | awk '{print $5}' | cut -d'%' -f1`
      StorageThreshold=75 #Define the storage % to trigger the script.

      ##### FUNCTIONS #####

      function multiplefolders () {
      echo "More than one folder detected."
      cd `ls -Ft | grep '/$' | tail -1`
      echo "Entering the oldest folder..."
      if [ "$FileCounter" -eq 0 ];
      then
      echo "No Files detected."
      cd $RootFolder
      echo "Going back to the output's root folder..."
      rm -rf `ls -Ft | grep '/$' | tail -1`
      echo "Detecting and removing the oldest empty folder...";
      else
      ls -t | tail -10 | xargs rm
      echo "Deleting the oldest files...";
      fi
      }

      function singlefolder () {
      echo "Only one folder detected."
      echo "Entering the only folder..."
      cd `ls -Ft | grep '/$' | head -1`
      ls -t | tail -10 | xargs rm
      echo "Deleting the oldest files."
      }

      function checkfolders () {
      if [ "$FolderCounter" -gt 1 ];
      then
      multiplefolders
      fi

      if [ "$FolderCounter" -eq 1 ];
      then
      singlefolder
      fi

      if [ "$FolderCounter" -eq 0 ];
      then
      echo "No folders detected. Please check if the output folder's path is correct."
      fi
      }

      ##### SCRIPT STARTER #####

      if [ $CurrentStorage -ge $StorageThreshold ];
      then
      checkfolders
      else
      echo "Storage threshold not yet reached."
      fi


      Thing is that the script isn't (apparently) counting the number of files inside the oldest folder (when more then one folder is detected) correctly.



      Instead of going back to the root folder and delete the oldest empty folder, it keeps deleting files from the newest folder (apparently).



      In other words, when you have 2 folders (the oldest one empty and the newest one with videos in it), it should delete the oldest and empty folder, but I keep getting:



      More than one folder detected.
      Entering the oldest folder...
      Deleting the oldest files...






      bash shell-script scripting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 16 at 12:00









      Rui F Ribeiro

      38.9k1479129




      38.9k1479129










      asked Sep 4 at 18:55









      André Luís

      284




      284






















          1 Answer
          1






          active

          oldest

          votes


















          2














          I hope we don't need to bother about whether we have a single folder or multiple folders. if we remove the files w.r.t modified time.



          Just check the Storage and remove the oldest 10 files in the folder



          if [ $CurrentStorage -ge $StorageThreshold ];
          then
          find $RootFolder -type f -printf '%T+ %pn' | sort | head -n 10 | awk '{print $NF}' | xargs rm -f
          else
          echo "Storage threshold not yet reached."
          fi




          • -type f -printf '%T+ %pn' print the files with last modified timestamp.


          • sort to get the oldest file on top.


          • head -n 10 to get 10 oldest files.


          • awk '{print $NF}' to extract the file path.


          • xargs rm -f remove the files which are extracted.


          For MAC:



           find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f


          And an empty folder would hardly occupy space of 4Kb. If you would like to remove all empty folder except the latest, include below code.



            find $RootFolder -type d -empty -printf '%T+ %pn' | sort | head -n -1 | xargs rm -rf


          Or



           ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf



          • It will remove all empty folder except the latest.






          share|improve this answer



















          • 1




            @AndréLuís are you using MAC?
            – msp9011
            Sep 4 at 20:20






          • 1




            @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
            – msp9011
            Sep 4 at 20:31








          • 1




            @AndréLuís Cheers!!!
            – msp9011
            Sep 4 at 20:33






          • 1




            @AndréLuís NO!!! you're deleting the latest folder.
            – msp9011
            Sep 4 at 21:11






          • 1




            @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
            – msp9011
            Sep 4 at 22:10











          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%2f466849%2fshell-script-to-delete-oldest-files-and-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          I hope we don't need to bother about whether we have a single folder or multiple folders. if we remove the files w.r.t modified time.



          Just check the Storage and remove the oldest 10 files in the folder



          if [ $CurrentStorage -ge $StorageThreshold ];
          then
          find $RootFolder -type f -printf '%T+ %pn' | sort | head -n 10 | awk '{print $NF}' | xargs rm -f
          else
          echo "Storage threshold not yet reached."
          fi




          • -type f -printf '%T+ %pn' print the files with last modified timestamp.


          • sort to get the oldest file on top.


          • head -n 10 to get 10 oldest files.


          • awk '{print $NF}' to extract the file path.


          • xargs rm -f remove the files which are extracted.


          For MAC:



           find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f


          And an empty folder would hardly occupy space of 4Kb. If you would like to remove all empty folder except the latest, include below code.



            find $RootFolder -type d -empty -printf '%T+ %pn' | sort | head -n -1 | xargs rm -rf


          Or



           ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf



          • It will remove all empty folder except the latest.






          share|improve this answer



















          • 1




            @AndréLuís are you using MAC?
            – msp9011
            Sep 4 at 20:20






          • 1




            @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
            – msp9011
            Sep 4 at 20:31








          • 1




            @AndréLuís Cheers!!!
            – msp9011
            Sep 4 at 20:33






          • 1




            @AndréLuís NO!!! you're deleting the latest folder.
            – msp9011
            Sep 4 at 21:11






          • 1




            @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
            – msp9011
            Sep 4 at 22:10
















          2














          I hope we don't need to bother about whether we have a single folder or multiple folders. if we remove the files w.r.t modified time.



          Just check the Storage and remove the oldest 10 files in the folder



          if [ $CurrentStorage -ge $StorageThreshold ];
          then
          find $RootFolder -type f -printf '%T+ %pn' | sort | head -n 10 | awk '{print $NF}' | xargs rm -f
          else
          echo "Storage threshold not yet reached."
          fi




          • -type f -printf '%T+ %pn' print the files with last modified timestamp.


          • sort to get the oldest file on top.


          • head -n 10 to get 10 oldest files.


          • awk '{print $NF}' to extract the file path.


          • xargs rm -f remove the files which are extracted.


          For MAC:



           find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f


          And an empty folder would hardly occupy space of 4Kb. If you would like to remove all empty folder except the latest, include below code.



            find $RootFolder -type d -empty -printf '%T+ %pn' | sort | head -n -1 | xargs rm -rf


          Or



           ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf



          • It will remove all empty folder except the latest.






          share|improve this answer



















          • 1




            @AndréLuís are you using MAC?
            – msp9011
            Sep 4 at 20:20






          • 1




            @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
            – msp9011
            Sep 4 at 20:31








          • 1




            @AndréLuís Cheers!!!
            – msp9011
            Sep 4 at 20:33






          • 1




            @AndréLuís NO!!! you're deleting the latest folder.
            – msp9011
            Sep 4 at 21:11






          • 1




            @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
            – msp9011
            Sep 4 at 22:10














          2












          2








          2






          I hope we don't need to bother about whether we have a single folder or multiple folders. if we remove the files w.r.t modified time.



          Just check the Storage and remove the oldest 10 files in the folder



          if [ $CurrentStorage -ge $StorageThreshold ];
          then
          find $RootFolder -type f -printf '%T+ %pn' | sort | head -n 10 | awk '{print $NF}' | xargs rm -f
          else
          echo "Storage threshold not yet reached."
          fi




          • -type f -printf '%T+ %pn' print the files with last modified timestamp.


          • sort to get the oldest file on top.


          • head -n 10 to get 10 oldest files.


          • awk '{print $NF}' to extract the file path.


          • xargs rm -f remove the files which are extracted.


          For MAC:



           find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f


          And an empty folder would hardly occupy space of 4Kb. If you would like to remove all empty folder except the latest, include below code.



            find $RootFolder -type d -empty -printf '%T+ %pn' | sort | head -n -1 | xargs rm -rf


          Or



           ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf



          • It will remove all empty folder except the latest.






          share|improve this answer














          I hope we don't need to bother about whether we have a single folder or multiple folders. if we remove the files w.r.t modified time.



          Just check the Storage and remove the oldest 10 files in the folder



          if [ $CurrentStorage -ge $StorageThreshold ];
          then
          find $RootFolder -type f -printf '%T+ %pn' | sort | head -n 10 | awk '{print $NF}' | xargs rm -f
          else
          echo "Storage threshold not yet reached."
          fi




          • -type f -printf '%T+ %pn' print the files with last modified timestamp.


          • sort to get the oldest file on top.


          • head -n 10 to get 10 oldest files.


          • awk '{print $NF}' to extract the file path.


          • xargs rm -f remove the files which are extracted.


          For MAC:



           find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f


          And an empty folder would hardly occupy space of 4Kb. If you would like to remove all empty folder except the latest, include below code.



            find $RootFolder -type d -empty -printf '%T+ %pn' | sort | head -n -1 | xargs rm -rf


          Or



           ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf



          • It will remove all empty folder except the latest.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 4 at 22:11

























          answered Sep 4 at 19:45









          msp9011

          3,74343863




          3,74343863








          • 1




            @AndréLuís are you using MAC?
            – msp9011
            Sep 4 at 20:20






          • 1




            @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
            – msp9011
            Sep 4 at 20:31








          • 1




            @AndréLuís Cheers!!!
            – msp9011
            Sep 4 at 20:33






          • 1




            @AndréLuís NO!!! you're deleting the latest folder.
            – msp9011
            Sep 4 at 21:11






          • 1




            @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
            – msp9011
            Sep 4 at 22:10














          • 1




            @AndréLuís are you using MAC?
            – msp9011
            Sep 4 at 20:20






          • 1




            @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
            – msp9011
            Sep 4 at 20:31








          • 1




            @AndréLuís Cheers!!!
            – msp9011
            Sep 4 at 20:33






          • 1




            @AndréLuís NO!!! you're deleting the latest folder.
            – msp9011
            Sep 4 at 21:11






          • 1




            @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
            – msp9011
            Sep 4 at 22:10








          1




          1




          @AndréLuís are you using MAC?
          – msp9011
          Sep 4 at 20:20




          @AndréLuís are you using MAC?
          – msp9011
          Sep 4 at 20:20




          1




          1




          @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
          – msp9011
          Sep 4 at 20:31






          @AndréLuís TRY find $RootFolder -type f -print0 | xargs -0 ls -ltr | head -n 10 | awk '{print $NF}' | xargs rm -f
          – msp9011
          Sep 4 at 20:31






          1




          1




          @AndréLuís Cheers!!!
          – msp9011
          Sep 4 at 20:33




          @AndréLuís Cheers!!!
          – msp9011
          Sep 4 at 20:33




          1




          1




          @AndréLuís NO!!! you're deleting the latest folder.
          – msp9011
          Sep 4 at 21:11




          @AndréLuís NO!!! you're deleting the latest folder.
          – msp9011
          Sep 4 at 21:11




          1




          1




          @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
          – msp9011
          Sep 4 at 22:10




          @AndréLuís Try , ls -lt $RootFolder/* | awk -F ":" '/total 0/{print last}{last=$1}' | tail -n +2 | xargs rm -rf
          – msp9011
          Sep 4 at 22:10


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f466849%2fshell-script-to-delete-oldest-files-and-folders%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

          Morgemoulin

          Scott Moir

          Souastre