drew77
1
TARGET_DIR='/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/'
REGEX='[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}:[0-9]{2}' # regular expression that match to: date '+%Y-%m-%d_%H:%M'
LATEST_FILE="$(ls "$TARGET_DIR" | egrep "^${REGEX}$" | tail -1)"
find "$TARGET_DIR" ! -name "$LATEST_FILE" -type f -regextype egrep -regex ".*/${REGEX}$" -exec rm -f {} +
# not working find "$TARGET_DIR" ! -name "$LATEST_FILE" -type f -regextype egrep -regex ".*/${REGEX}$" -print -f {} +
The REGEX and find statement is complicated and I do not understand much of what is going on behind the scenes.
I would like to modify it so it deletes these type of files from the next dir.
Ubuntu_Scripts_2018-08-22-20-00.zip
/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/
try this:
REGEX='[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}[.]zip'
Misread then wrong answer, i apologize :o
Regards
Peasant
drew77
4
This isn't deleting any files.
#!/bin/bash
TARGET_DIR='/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/'
REGEX='[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}[.]zip'
LATEST_FILE="$(ls "$TARGET_DIR" | egrep "^${REGEX}$" | tail -1)"
find "$TARGET_DIR" ! -name "$LATEST_FILE" -type f -regextype egrep -regex ".*/${REGEX}$" -exec rm -f {} +
you need to work with the REGEX to match your file spec(s):
#!/bin/bash
TARGET_DIR='/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/'
REGEX='Ubuntu_Scripts_[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}[.]zip'
LATEST_FILE="$(ls "$TARGET_DIR" | egrep "${REGEX}$" | tail -1)"
find "$TARGET_DIR" ! -name "$LATEST_FILE" -type f -regextype egrep -regex ".*/${REGEX}$" -exec rm -f {} +
drew77
6
Ok. How do I find out about those
[0-9]{4} parts?
I would be glad to read up, but don't know where to look.
start with man egrep
looking at the REGULAR EXPRESSIONS
chapter.
drew77
8
Thanks. This works.
# http://www.cs.columbia.edu/~tal/3261/fall07/handout/egrep_mini-tutorial.htm
#REGEX='[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}:[0-9]{2}' # regular expression that match to: date '+%Y-%m-%d_%H:%M'
#!/bin/bash
TARGET_DIR='/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/'
REGEX='Ubuntu_Scripts_[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}[.]zip' # Ubuntu_Scripts_2018-08-23-10-00.zip
LATEST_FILE="$(ls "$TARGET_DIR" | egrep "^${REGEX}$" | tail -1)"
find "$TARGET_DIR" ! -name "$LATEST_FILE" -type f -regextype egrep -regex ".*/${REGEX}$" -exec rm -f {} +