0

In a directory, I would like to delete the files based on the appended date at the end of the file, these files are XML back files. The files should be deleted based on the below criteria.

In each set, if it contains files count more than 12 (per particular set of backup files in their name) then delete the old files, latest files should remain in the same directory.

Examples

Set 1:
Wf_ABCDEF_HGD_AFFGF_DFDF_201601011224.XML

NOTE: The above file in set 1 should not delete.

Set 2:
Wf_ABHGD_AFFGF_DFDF_201101211221.XML   
Wf_ABHGD_AFFGF_DFDF_201202211222.XML   
Wf_ABHGD_AFFGF_DFDF_201304411223.XML   
Wf_ABHGD_AFFGF_DFDF_201406611224.XML   
Wf_ABHGD_AFFGF_DFDF_201508811225.XML   
Wf_ABHGD_AFFGF_DFDF_201609911226.XML   
Wf_ABHGD_AFFGF_DFDF_201707711227.XML   
Wf_ABHGD_AFFGF_DFDF_201801081228.XML   
Wf_ABHGD_AFFGF_DFDF_201901911229.XML   
Wf_ABHGD_AFFGF_DFDF_202001811210.XML   
Wf_ABHGD_AFFGF_DFDF_202101511211.XML   
Wf_ABHGD_AFFGF_DFDF_202201811212.XML   
Wf_ABHGD_AFFGF_DFDF_202301811213.XML   
Wf_ABHGD_AFFGF_DFDF_202401911214.XML   
Wf_ABHGD_AFFGF_DFDF_202501041215.XML   
Wf_ABHGD_AFFGF_DFDF_202601031216.XML

The below files should be deleted as these are old files based on the file append date.

Wf_ABHGD_AFFGF_DFDF_201101211221.XML
Wf_ABHGD_AFFGF_DFDF_201202211222.XML
Wf_ABHGD_AFFGF_DFDF_201304411223.XML
Wf_ABHGD_AFFGF_DFDF_201406611224.XML
Set 3:-
Wf_ABCDEFGHIJKLMONM_201101211221.XML   
Wf_ABCDEFGHIJKLMONM_201202211222.XML   
Wf_ABCDEFGHIJKLMONM_201304411223.XML
Wf_ABCDEFGHIJKLMONM_201406611224.XML   
Wf_ABCDEFGHIJKLMONM_201508811225.XML   
Wf_ABCDEFGHIJKLMONM_201609911226.XML   
Wf_ABCDEFGHIJKLMONM_201707711227.XML   
Wf_ABCDEFGHIJKLMONM_201801081228.XML   
Wf_ABCDEFGHIJKLMONM_201901911229.XML   
Wf_ABCDEFGHIJKLMONM_202001811210.XML   
Wf_ABCDEFGHIJKLMONM_202101511211.XML

The above files should not be deleted as this set is equal to or below 12 files.

  • 1
    Well your points 1 and 3 in the Question are nothing but the same . Because count < 12 Is the super set of count = 1 .. isn't it? – ss_iwe Aug 16 '18 at 4:34
  • if it is less then 12 then it should not delete – surajreddy2 Aug 16 '18 at 4:42
  • 1
    That's what, the logic checking less than 12 also covers the other point. You need not have a separate logic checking if the file count is 1 since 1 < 12. File count be 1 or anything. Until its less than 12 , count < 12 logic should be sufficient! What say you? – ss_iwe Aug 16 '18 at 4:48
  • In one set, if it has only 1 record then it should not delete if it is more than 12 in the set it should delete the old date files. I guess we can achieve by using count, do you have a script based on the above file structure and scenario? – surajreddy2 Aug 16 '18 at 4:53
  • 1
    As told by @saisasanka scenario 1 and 3, look same. – msp9011 Aug 16 '18 at 5:23
0

With find and Bash Offset Expansion ${parameter:offset:length}, you could do:

find -type f -execdir bash -c 'printf "%s\n" "${@:1:${#@}-12}" ' _ {} +
./Wf_ABHGD_AFFGF_DFDF_201101211221.XML
./Wf_ABHGD_AFFGF_DFDF_201202211222.XML
./Wf_ABHGD_AFFGF_DFDF_201304411223.XML
./Wf_ABHGD_AFFGF_DFDF_201406611224.XML

The ${#@} is returning the total number of files (or arguments in mean).

repeat above for every set of files you have by specifying the pattern in -name option of find command above.

Or to get patterns by script:

# !/bin/bash

allFILEs=(*.XML)
PATTERNs=$(printf "%s\n" "${allFILEs[@]%_*}" |sort -u)

for pattern in ${PATTERNs[@]};
do
    find -type f -name "*${pattern}*" -execdir bash -c '
        printf "%s\n" "${@:1:${#@}-12}"' _ {} + 2>/dev/null;
done

change printf with rm (in find ... command) when you had test and see which files(s) being deleting.

0

The following code should do all scenario :

All 3 of your Scenario has fallen on only one condition, we need to delete the oldest files if the backup file count is greater than 12

cd /path/to/dir
FilePattern=(`ls --format single-column *.XML | rev | cut -d '_' -f2-  | rev | sort -u`)

for FILE in ${FilePattern[@]}
do
    File_count=`ls "$FILE"_*.XML | wc -w`
    if [ $File_count -gt 12 ]; then
        REMOVE_count=`expr $File_count - 12`
        ls --format single-column "$FILE"_*.XML | head -n $REMOVE_count |  xargs rm -f
    fi
done
  • File_count will get the number of files avail in the directory
  • if the removal process starts only if file count is greater than 12.
  • REMOVE_count calculate the count of files greater than 12
  • ls --format single-column will list files in a single column in ascending order.
  • head to get the oldest files
  • rm to remove
  • Siva@ Thanks for your script, I have tried above script but it is getting delete the.XML files which fall under less than 12 also, I need to keep if less than 12 XML files. – surajreddy2 Aug 16 '18 at 7:20
  • @surajreddy2 I hope u missed double quotes around $FILE – msp9011 Aug 16 '18 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.