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 |
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. |
Hi,
You can pipe the output to rm using xargs: Code:
ls $FILE | grep -v "${UDATE}" | xargs rm -i BTW: The line that changes DATE to upper case might(!) not be needed: Code:
#!/bin/ksh Hope this helps. |
Hi, welcome to LQ!
Try piping to xargs rm edit: Toooooo slooooow :} Cheers, Tink |
You could use find to search for files older than todays file and delete them. :twocents:
Code:
|
HI,
Thanks to all for your valuable and helpful suggestions |
Quote:
What -newermt option will do? |
It will search based on date listed . ! = not
So not newer than date xxxx delete. |
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 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 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. |