LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Deleting files based on Substring match (https://www.linuxquestions.org/questions/linux-newbie-8/deleting-files-based-on-substring-match-940400/)

manushi88 04-18-2012 02:48 AM

Deleting files based on Substring match
 
In folder there are files
(eg ABS_18APR2012_XYZ.csv
DSE_17APR2012_ABE.csv) .
My requirement is to delete all the files except today's timestamp

I tried doing this to list all the files not having today's date timestamp



Code:

#!/bin/ksh

DATE=`date +"%d%h%Y"`
DIR=/data/rfs/
FOLDER=Test
FILE=$DIR$FOLDER

UDATE="$(echo $DATE | tr '[a-z]' '[A-Z]')"

ls $FILE | grep -v "${UDATE}"

exit 0

This is listing all the files not having today's date timestamp. Now I have to delete the files which are listed .Kindly help me out how I can delete the files.

acid_kewpie 04-18-2012 02:55 AM

if you're happy with the output, you should be able to just pipe the output into rm via xargs:

./myscript.ksh | xargs rm -f

job done. Other methods also are possible, but from your starting point it should work fine. Personally I'd probably just use a single find statement and not need a script at all.

druuna 04-18-2012 02:55 AM

Hi,

You can pipe the output to rm using xargs:
Code:

ls $FILE | grep -v "${UDATE}" | xargs rm -i
The -i switch will make rm interactive (in case of errors/false hits, you can remove it when you are sure).

BTW: The line that changes DATE to upper case might(!) not be needed:
Code:

#!/bin/ksh

DATE=`date +"%d%h%Y"`
DIR=/data/rfs/
FOLDER=Test
FILE=$DIR$FOLDER

ls $FILE | grep -iv "${DATE}" | xargs rm -i

exit 0

The -i switch used with grep makes the search case insensitive.

Hope this helps.

Tinkster 04-18-2012 02:56 AM

Hi, welcome to LQ!

Try piping to
xargs rm


edit: Toooooo slooooow :}


Cheers,
Tink

jv2112 04-18-2012 04:44 AM

You could use find to search for files older than todays file and delete them. :twocents:


Code:


Test First --- to ensure these are the files you want to erase.

find /path/of/interest/ -type f -and ! -newermt 2012-04-18 -exec ls -l {} \;

Nuke em -->

find /path/of/interest/ -type f -and ! --newermt 20012-04-18 -exec rm -fv {} \;


manushi88 04-19-2012 05:49 AM

HI,

Thanks to all for your valuable and helpful suggestions

manushi88 04-19-2012 05:51 AM

Quote:

Originally Posted by jv2112 (Post 4655968)
You could use find to search for files older than todays file and delete them. :twocents:


Code:


Test First --- to ensure these are the files you want to erase.

find /path/of/interest/ -type f -and ! -newermt 2012-04-18 -exec ls -l {} \;

Nuke em -->

find /path/of/interest/ -type f -and ! --newermt 20012-04-18 -exec rm -fv {} \;


hi,
What -newermt option will do?

jv2112 04-19-2012 06:59 AM

It will search based on date listed . ! = not

So not newer than date xxxx delete.

David the H. 04-19-2012 01:14 PM

Grepping the output of ls? Please, just say no.

If a simple find command won't do, I'd run the filenames in a loop and use the shell's built-in testing ability.

Code:

for file in *.csv; do

        [[ ! $file =~ $UDATE ]] && rm "$file"

done

If you want to make it more robust, you should load the list of filenames into an array first, and work on that. Avoid storing lists of things inside single scalar variables.

I'm not too familiar with ksh, but it should have built-in features for converting variable values to upper or lower case. A quick test in ksh shows that bash's ${var^^} pattern doesn't work, but setting "typeset -u var" first does, at least, which means there's likely some way to echo it at will as well.

Code:

$ typeset -u var=foo
$ echo "$var"
FOO


Also, $(..) is highly recommended over `..`.


Finally, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.


All times are GMT -5. The time now is 05:10 PM.