LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2016, 06:53 AM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Rep: Reputation: Disabled
Detect one file in a folder and delete everything i that folder.


This is a bit convoluted and I will do my best to explain it. Hope it makes sense.

I have written a bash script that will backup my blog using wget. It creates a tar.gz archive then a series of parity files then finally an MD5. This is then stored in a unique folder with the date under a master folder called blogbackup.

Sometimes because of connectivity issues the backup will fail and I only get a partial backup. I know the total compressed archive should be approx 220meg. As the blog will only get lager I would like to delete any backups less than 220meg. This way I wont be archiving any failed backups.

I can easily search for tar.gz files less than 220meg in all subfolders under blogbackup, however it will leave the parity and MD5 files.

What I need to figure out is how to detect a folder with a tar.gz less then 220Meg and delete EVERYTHING in that folder.

Is there a way to achieve this?

Many thanks
Tim
 
Old 12-17-2016, 07:02 AM   #2
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware, OpenBSD
Posts: 4,057
Blog Entries: 11

Rep: Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238Reputation: 2238
You could try du. Run the output through awk, looking for "tar.gz" plus <220M in field $1.

Last edited by hazel; 12-17-2016 at 10:02 AM.
 
Old 12-17-2016, 08:46 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by Entropy1024 View Post
I can easily search for tar.gz files less than 220meg in all subfolders under blogbackup, however it will leave the parity and MD5 files.
You don't sound very certain of much.
Have you considered testing the archive or some other logic not-based on "approx 220meg"?
I'd like to see some code.
Something is missing.

Last edited by Habitual; 12-19-2016 at 06:33 AM. Reason: emph added
 
Old 12-17-2016, 10:57 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,366

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Given
Code:
bash-4.4$ tree -s blogbackup/
blogbackup/
├── [       4096]  20161218A
│** └── [          0]  a.tar.gz
├── [       4096]  20161218B
│** └── [     252268]  b.tar.gz
└── [       4096]  20161218C
    └── [          0]  c.tar.gz

3 directories, 3 files
then in a bash shell
Code:
bash-4.4$ cd blogbackup/
bash-4.4$ for f in */*.tar.gz; do (( $(stat -c %s $f) < 225000 )) && echo ${f%/*}; done
20161218A
20161218C
So the code is doable, but I am with Habitual, there is likely to be a better way.
 
Old 12-18-2016, 12:06 PM   #5
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
find comes to mind to search for sizes:
Code:
find bu_folder -size -220M  -name '*tar.gz'  -
combined with either dirname or parameter expansion to get the path to the directory in which said files are found:
with dirname (the less sexy solution :-) )
Code:
find bu_folder -size -220M  -name '*tar.gz'  -exec sh -c '
        for f; do 
            echo $(dirname "$f")
        done' echo_failed_bu {} \;
getting all files in that folder:
Code:
find bu_folder -size -220M  -name '*tar.gz'  -exec sh -c '
        for f; do 
          for i in $(dirname "$f")/*; do 
            echo "$i"
          done
        done' echo_failed_bu {} \;
In case you think that is a solution to your liking, look here to get the exact syntax you need for find: http://mywiki.wooledge.org/UsingFind
and here to use parameter expansion to get the path of the file you found: http://mywiki.wooledge.org/BashFAQ/0...meterExpansion
If the final ; doesn't work, you may want to try a + instead.

Last edited by nodir; 12-18-2016 at 12:10 PM.
 
Old 12-18-2016, 12:38 PM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,653

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
A different option would be to build your backup file with a suffix of .tmp
When it finishes, rename your file to drop the .tmp suffix.

Anything with a .tmp suffix is obviously a failed backup.
 
Old 12-18-2016, 01:30 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,422
Blog Entries: 9

Rep: Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668
Quote:
Originally Posted by Entropy1024 View Post
Sometimes because of connectivity issues the backup will fail and I only get a partial backup.
can't you run the backup directly on the server and its OS?
then connectivity wouldn't be an issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Old Timeshift Folder won't Delete from Trash Folder. gael33 Linux - Newbie 2 09-04-2015 11:39 AM
delete a html file along with its associated folder beopen Linux - General 4 08-17-2014 01:43 PM
A Command to Delete the Oldest Sub-folder in a Specific Folder imayneed Linux - Newbie 13 08-05-2012 08:53 PM
detect a new file in a folder.... os2 Programming 1 03-12-2006 11:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration