Conditional delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional delete
# 8  
Old 08-29-2018
The
Code:
2[0-9][0-9][0-9][01][0-9][0-3][0-9]_[0-2][0-9][0-5][0-9].tar.gz

has a Y3K problem and is less readable than
Code:
????????_????.tar.gz

The latter is certainly precise enough.
# 9  
Old 08-29-2018
Quote:
Originally Posted by Don Cragun
Hi onenessboy,
You can start by showing us the complete, exact output produced by the command:
Code:
df -P /backup

If the output from the above command doesn't complain about an unknown -P option, the percentage of space used on the filesystem containing /backup should be in field #5 on line #2 of the output from the above command.

If it does complain about an unknown -P option, show us the complete, exact output from the command:
Code:
df /backup

so we can figure out which field and line we need to examine to determine if you have reached your goal.

Hi RudiC,
Why not:
Code:
ls -r1 2[0-9][0-9][0-9][01][0-9][0-3][0-9]_[0-2][0-9][0-5][0-9].tar.gz | awk -F_ 'T[$1]++ {print "echo rm " $0}'  | sh

Because that will remove old files from the most recent date first. And onenessboy wants to remove old backup files from the oldest date first. And he wants to add code to exit the script when the capacity on that filesystem drops below 60% after each date change when one or more backup files were removed for any given date.
Hi Don Cragun,

I tried your command ..it does not complain about -P option...the output is below

Code:
[root@ip-11-66-77-99 ~]$ df -P /backup
Filesystem     1024-blocks      Used       Available     Capacity   Mounted on
/dev/xvdf        515928320  299040832    192733092         61%     /backup

# 10  
Old 08-29-2018
Didn't you say the disk was 300GB? Looks more like 500GB...?


EDIT: Does your OS provide the stat command?
# 11  
Old 08-29-2018
Quote:
Originally Posted by RudiC
Didn't you say the disk was 300GB? Looks more like 500GB...?


EDIT: Does your OS provide the stat command?
Hi RudiC,

apologies, actually since we had issues with disc space team added more space to it recently) I did not notice, the original design was to have 300 GB. Ideally each tar file size around 2.6 GB approx..so all the huddle here

However objective of solution is same...to remove file as i explained in previous posts.

Yes , stat command working.. I said man stat it showed help
# 12  
Old 08-29-2018
Assuming the stat command on your system allows for the -f (--file-system) option, and shamelessly stealing from Don Cragun's earlier post, and NOT being able to ultimately test this on my system, I'd propose this (printing some meaningful numbers for debug purposes) and ask you to comment back:
Code:
{ cd /backup; stat -fc"%b %a %S" .; stat -c"%n %b %B" 2018*.tar.gz | sort; } | awk '
NR == 1         {print Needed = ($1 * PCT - $2) * $3 
                 next
                }

                {split ($1, T, "_")
                 if (T[1] == KnownTime) print "echo rm " LastFile
                 KnownTime = T[1]
                 LastFile = $1
                 print SUM += $2 * $3
                 if (SUM >= Needed) exit
                }
 ' PCT=0.4

Once happy with what is delivered, we can pipe this into an sh command to be executed.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 08-29-2018
Quote:
Originally Posted by RudiC
Assuming the stat command on your system allows for the -f (--file-system) option, and shamelessly stealing from Don Cragun's earlier post, and NOT being able to ultimately test this on my system, I'd propose this (printing some meaningful numbers for debug purposes) and ask you to comment back:
Code:
{ cd /backup; stat -fc"%b %a %S" .; stat -c"%n %b %B" 2018*.tar.gz | sort; } | awk '
NR == 1         {print Needed = ($1 * PCT - $2) * $3 
                 next
                }

                {split ($1, T, "_")
                 if (T[1] == KnownTime) print "echo rm " LastFile
                 KnownTime = T[1]
                 LastFile = $1
                 print SUM += $2 * $3
                 if (SUM >= Needed) exit
                }
 ' PCT=0.4

Once happy with what is delivered, we can pipe this into an sh command to be executed.
Hi RudiC,

Thankyou very much for snippet, shall try it on different test machine, sorry for dumb question, which line one specifies 60 % as thread hold...I wanted to test on a sample % as threshold on test machine. because test machine i have 10 gb of these tar files
# 14  
Old 08-29-2018
PCT=0.4 defines the necessary free space... this minus the actually available space yields the files' sizes to be deleted.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional delete -- New glitch

Hi Please dont consider this as duplicated post.. I am using below pattern to find delete files to bringdown disc size.. however how i can make sure ist going to correct folder and searching for files... while print "echo rm " LastFile correctly print files names for deletion, but when i... (7 Replies)
Discussion started by: onenessboy
7 Replies

2. UNIX for Beginners Questions & Answers

(g)awk conditional substitution issues when attempting to delete character

A portion of my input is as follows: 1087 IKON01,49 A WA- -1 . -1 . 0 W WA- -1 . -1 . 0 . -1 . -1 -1 -1 -1 -1 -1 W 1088 IKON01,49 A J.@QU80MW. 2... (2 Replies)
Discussion started by: jvoot
2 Replies

3. Shell Programming and Scripting

Conditional execution

Here's an interesting (to me, anyway) little puzzle. Background: I have a process that restores a number of large(ish) files from tape backup. As an individual file is being written, it is owned by root, then the ownership is changed when that file is complete. Since this process can take... (3 Replies)
Discussion started by: edstevens
3 Replies

4. Shell Programming and Scripting

Conditional search and delete using SED / Shell script

Hi, I want to perform a conditional search and remove my search string. Input string: "abcdaabcadgfaarstab" Character to search: "a" Condition: Remove all "a" in the input string except if it is "aa" Output string: "bcdaabcdgfaarstb" Can you please help me in this? (5 Replies)
Discussion started by: dominiclajs
5 Replies

5. Shell Programming and Scripting

if conditional statement

Hi, I have a script like this: sample.sh mapping=$1 if then echo "program passed" fi I'm running the above script as ./sample.sh pass The script is not getting executed and says "integer expression expected" Could anyone kindly help me? (2 Replies)
Discussion started by: badrimohanty
2 Replies

6. Shell Programming and Scripting

conditional statement

Hi all, The following code is to find if a list of numbers from one file are within the range in another file. awk -F, '\ BEGIN { while ((getline < "file2") > 0) file2=$3 } {for (col1 in file2) if ($0>=30 && $1<=45) print $0} ' FILE1 But where I have the number 30 and 45, I... (3 Replies)
Discussion started by: dr_sabz
3 Replies

7. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

8. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

9. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

10. Shell Programming and Scripting

Conditional Statements

How can I compare two decimal values within a function using Bash? Function fun2 isn't comparing the decimal values. Is there a way to do this using Bash or Korn? #!/bin/bash set -x x=1 z=110 function fun1() { i=`bc << EOF 2>> /dev/null scale=3 ... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question