LinuxQuestions.org
Review your favorite Linux distribution.
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, 05: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, 06:02 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,564
Blog Entries: 19

Rep: Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446
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 09:02 AM.
 
Old 12-17-2016, 07:46 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
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 05:33 AM. Reason: emph added
 
Old 12-17-2016, 09:57 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,367

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
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, 11:06 AM   #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 11:10 AM.
 
Old 12-18-2016, 11:38 AM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
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, 12:30 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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 10:39 AM
delete a html file along with its associated folder beopen Linux - General 4 08-17-2014 12:43 PM
A Command to Delete the Oldest Sub-folder in a Specific Folder imayneed Linux - Newbie 13 08-05-2012 07:53 PM
detect a new file in a folder.... os2 Programming 1 03-12-2006 10:52 AM

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

All times are GMT -5. The time now is 06:31 PM.

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
Open Source Consulting | Domain Registration