How can I delete time-stamped folders if one has already been created that day?












0















Folders are created every time I (re)boot.



They have the format below: (the time will always vary)



.mozilla_2019_01_08_08:02
.mozilla_2019_01_09_20:16
.mozilla_2019_01_10_01:16
.mozilla_2019_01_10_18:12


Here, I want to be able to delete the folder .mozilla_2019_01_10_18:12 when it is created as an earlier one already exists on ..._2019_01_10. Better would be to stop it being created, i.e. if one has already been created that day.



I can't use cron @daily or specific times as I never know how often or when the computer will be (re)booted.



How can I do this in an existing script please?



Here is my script:



##!/bin/bash
##
# COPY .mozilla FOLDER AND LEAVE LAST 10 COPIES (1 per day?)
#
sleep 30
#
if [ -e /home/david/nas-backups/mozilla/ ]
then
cp -a /home/david/.mozilla /home/david/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla /home/david/nas-backups
/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)

# Show result of copy message
export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla Backup Finished

Backed up to:
/nas-backups/mozilla/backups/" --width=600 --height=200

## Working on how to keep only first copy per day ##

# Limit to last 10 alphabetical copies (courtesy of @Vijay)
cd ~/nas-backups/mozilla/backups
total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head
-n $remove)


# Show folder so I can manually delete extra daily copies
nautilus /home/david/nas-backups/mozilla/backups

else

export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla backup FAILED

- nas-backups either not mounted or not availeble" --width=450
--height=150
fi









share|improve this question




















  • 1





    Maybe posting the entire contents of the "existing script" could help people help you.

    – DK Bose
    Jan 10 at 2:46











  • Who creates those folders? I don't have anything like that on my system.

    – AlexP
    Jan 10 at 3:41











  • @user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it.

    – David
    Jan 11 at 0:13


















0















Folders are created every time I (re)boot.



They have the format below: (the time will always vary)



.mozilla_2019_01_08_08:02
.mozilla_2019_01_09_20:16
.mozilla_2019_01_10_01:16
.mozilla_2019_01_10_18:12


Here, I want to be able to delete the folder .mozilla_2019_01_10_18:12 when it is created as an earlier one already exists on ..._2019_01_10. Better would be to stop it being created, i.e. if one has already been created that day.



I can't use cron @daily or specific times as I never know how often or when the computer will be (re)booted.



How can I do this in an existing script please?



Here is my script:



##!/bin/bash
##
# COPY .mozilla FOLDER AND LEAVE LAST 10 COPIES (1 per day?)
#
sleep 30
#
if [ -e /home/david/nas-backups/mozilla/ ]
then
cp -a /home/david/.mozilla /home/david/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla /home/david/nas-backups
/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)

# Show result of copy message
export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla Backup Finished

Backed up to:
/nas-backups/mozilla/backups/" --width=600 --height=200

## Working on how to keep only first copy per day ##

# Limit to last 10 alphabetical copies (courtesy of @Vijay)
cd ~/nas-backups/mozilla/backups
total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head
-n $remove)


# Show folder so I can manually delete extra daily copies
nautilus /home/david/nas-backups/mozilla/backups

else

export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla backup FAILED

- nas-backups either not mounted or not availeble" --width=450
--height=150
fi









share|improve this question




















  • 1





    Maybe posting the entire contents of the "existing script" could help people help you.

    – DK Bose
    Jan 10 at 2:46











  • Who creates those folders? I don't have anything like that on my system.

    – AlexP
    Jan 10 at 3:41











  • @user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it.

    – David
    Jan 11 at 0:13
















0












0








0








Folders are created every time I (re)boot.



They have the format below: (the time will always vary)



.mozilla_2019_01_08_08:02
.mozilla_2019_01_09_20:16
.mozilla_2019_01_10_01:16
.mozilla_2019_01_10_18:12


Here, I want to be able to delete the folder .mozilla_2019_01_10_18:12 when it is created as an earlier one already exists on ..._2019_01_10. Better would be to stop it being created, i.e. if one has already been created that day.



I can't use cron @daily or specific times as I never know how often or when the computer will be (re)booted.



How can I do this in an existing script please?



Here is my script:



##!/bin/bash
##
# COPY .mozilla FOLDER AND LEAVE LAST 10 COPIES (1 per day?)
#
sleep 30
#
if [ -e /home/david/nas-backups/mozilla/ ]
then
cp -a /home/david/.mozilla /home/david/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla /home/david/nas-backups
/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)

# Show result of copy message
export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla Backup Finished

Backed up to:
/nas-backups/mozilla/backups/" --width=600 --height=200

## Working on how to keep only first copy per day ##

# Limit to last 10 alphabetical copies (courtesy of @Vijay)
cd ~/nas-backups/mozilla/backups
total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head
-n $remove)


# Show folder so I can manually delete extra daily copies
nautilus /home/david/nas-backups/mozilla/backups

else

export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla backup FAILED

- nas-backups either not mounted or not availeble" --width=450
--height=150
fi









share|improve this question
















Folders are created every time I (re)boot.



They have the format below: (the time will always vary)



.mozilla_2019_01_08_08:02
.mozilla_2019_01_09_20:16
.mozilla_2019_01_10_01:16
.mozilla_2019_01_10_18:12


Here, I want to be able to delete the folder .mozilla_2019_01_10_18:12 when it is created as an earlier one already exists on ..._2019_01_10. Better would be to stop it being created, i.e. if one has already been created that day.



I can't use cron @daily or specific times as I never know how often or when the computer will be (re)booted.



How can I do this in an existing script please?



Here is my script:



##!/bin/bash
##
# COPY .mozilla FOLDER AND LEAVE LAST 10 COPIES (1 per day?)
#
sleep 30
#
if [ -e /home/david/nas-backups/mozilla/ ]
then
cp -a /home/david/.mozilla /home/david/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla /home/david/nas-backups
/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)

# Show result of copy message
export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla Backup Finished

Backed up to:
/nas-backups/mozilla/backups/" --width=600 --height=200

## Working on how to keep only first copy per day ##

# Limit to last 10 alphabetical copies (courtesy of @Vijay)
cd ~/nas-backups/mozilla/backups
total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head
-n $remove)


# Show folder so I can manually delete extra daily copies
nautilus /home/david/nas-backups/mozilla/backups

else

export DISPLAY=:0 && zenity --info --title "Backup Information Box"
--text=".mozilla backup FAILED

- nas-backups either not mounted or not availeble" --width=450
--height=150
fi






cron delete timestamp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 at 23:14







David

















asked Jan 10 at 2:17









DavidDavid

104




104








  • 1





    Maybe posting the entire contents of the "existing script" could help people help you.

    – DK Bose
    Jan 10 at 2:46











  • Who creates those folders? I don't have anything like that on my system.

    – AlexP
    Jan 10 at 3:41











  • @user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it.

    – David
    Jan 11 at 0:13
















  • 1





    Maybe posting the entire contents of the "existing script" could help people help you.

    – DK Bose
    Jan 10 at 2:46











  • Who creates those folders? I don't have anything like that on my system.

    – AlexP
    Jan 10 at 3:41











  • @user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it.

    – David
    Jan 11 at 0:13










1




1





Maybe posting the entire contents of the "existing script" could help people help you.

– DK Bose
Jan 10 at 2:46





Maybe posting the entire contents of the "existing script" could help people help you.

– DK Bose
Jan 10 at 2:46













Who creates those folders? I don't have anything like that on my system.

– AlexP
Jan 10 at 3:41





Who creates those folders? I don't have anything like that on my system.

– AlexP
Jan 10 at 3:41













@user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it.

– David
Jan 11 at 0:13







@user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it.

– David
Jan 11 at 0:13












1 Answer
1






active

oldest

votes


















0














One backup per day: Don't create more backups if one exists for today



Add a condition that checks if a backup folder for today does not exist. If there is no folder for today then only the cp and mv will be executed.



Note the * at the end of the second folder check. This allows for the time stamp at the end of the folder name, as you have done.



Here is the code fragment:



##!/bin/bash
if [ -d /home/david/nas-backups/mozilla/ ]
then
if [ ! -d /home/david/nas-backups/mozilla/backups/.mozilla_"$(date +%Y_%m_%d)"* ]
then
echo "No backups found for today: Making one"
cp -a /home/david/.mozilla ~/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla ~/nas-backups/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)
else
echo "Backup folder for today exists. No backups will be made today"
fi
fi


Hope this helps






share|improve this answer





















  • 1





    Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

    – David
    Jan 11 at 23:46













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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1108453%2fhow-can-i-delete-time-stamped-folders-if-one-has-already-been-created-that-day%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









0














One backup per day: Don't create more backups if one exists for today



Add a condition that checks if a backup folder for today does not exist. If there is no folder for today then only the cp and mv will be executed.



Note the * at the end of the second folder check. This allows for the time stamp at the end of the folder name, as you have done.



Here is the code fragment:



##!/bin/bash
if [ -d /home/david/nas-backups/mozilla/ ]
then
if [ ! -d /home/david/nas-backups/mozilla/backups/.mozilla_"$(date +%Y_%m_%d)"* ]
then
echo "No backups found for today: Making one"
cp -a /home/david/.mozilla ~/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla ~/nas-backups/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)
else
echo "Backup folder for today exists. No backups will be made today"
fi
fi


Hope this helps






share|improve this answer





















  • 1





    Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

    – David
    Jan 11 at 23:46


















0














One backup per day: Don't create more backups if one exists for today



Add a condition that checks if a backup folder for today does not exist. If there is no folder for today then only the cp and mv will be executed.



Note the * at the end of the second folder check. This allows for the time stamp at the end of the folder name, as you have done.



Here is the code fragment:



##!/bin/bash
if [ -d /home/david/nas-backups/mozilla/ ]
then
if [ ! -d /home/david/nas-backups/mozilla/backups/.mozilla_"$(date +%Y_%m_%d)"* ]
then
echo "No backups found for today: Making one"
cp -a /home/david/.mozilla ~/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla ~/nas-backups/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)
else
echo "Backup folder for today exists. No backups will be made today"
fi
fi


Hope this helps






share|improve this answer





















  • 1





    Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

    – David
    Jan 11 at 23:46
















0












0








0







One backup per day: Don't create more backups if one exists for today



Add a condition that checks if a backup folder for today does not exist. If there is no folder for today then only the cp and mv will be executed.



Note the * at the end of the second folder check. This allows for the time stamp at the end of the folder name, as you have done.



Here is the code fragment:



##!/bin/bash
if [ -d /home/david/nas-backups/mozilla/ ]
then
if [ ! -d /home/david/nas-backups/mozilla/backups/.mozilla_"$(date +%Y_%m_%d)"* ]
then
echo "No backups found for today: Making one"
cp -a /home/david/.mozilla ~/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla ~/nas-backups/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)
else
echo "Backup folder for today exists. No backups will be made today"
fi
fi


Hope this helps






share|improve this answer















One backup per day: Don't create more backups if one exists for today



Add a condition that checks if a backup folder for today does not exist. If there is no folder for today then only the cp and mv will be executed.



Note the * at the end of the second folder check. This allows for the time stamp at the end of the folder name, as you have done.



Here is the code fragment:



##!/bin/bash
if [ -d /home/david/nas-backups/mozilla/ ]
then
if [ ! -d /home/david/nas-backups/mozilla/backups/.mozilla_"$(date +%Y_%m_%d)"* ]
then
echo "No backups found for today: Making one"
cp -a /home/david/.mozilla ~/nas-backups/mozilla/
mv /home/david/nas-backups/mozilla/.mozilla ~/nas-backups/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)
else
echo "Backup folder for today exists. No backups will be made today"
fi
fi


Hope this helps







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 11 at 16:26

























answered Jan 11 at 16:00









user68186user68186

15.7k84768




15.7k84768








  • 1





    Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

    – David
    Jan 11 at 23:46
















  • 1





    Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

    – David
    Jan 11 at 23:46










1




1





Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

– David
Jan 11 at 23:46







Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you.

– David
Jan 11 at 23:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • 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%2faskubuntu.com%2fquestions%2f1108453%2fhow-can-i-delete-time-stamped-folders-if-one-has-already-been-created-that-day%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

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

File:DeusFollowingSea.jpg