Unix Programming - deleting a file by date

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > deleting a file by date





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author deleting a file by date
Ryan Gaffuri

2004-01-23, 5:11 pm

I am going to store 3 files in a directory. Every day I want to delete
the oldest one. How do I do that? The files will have part of their
name exactly the same.

also, if i have less than 3 files in the directory, I do not want to
delete any.

how do i do this? my scripting skills are weak. I need to do something
like this I think

ls -lrt *filename* | cut -d" " -f2 | how do i know to delete?
Stephane CHAZELAS

2004-01-23, 5:11 pm

2003/11/20, 12:13(-08), Ryan Gaffuri:
quote:

> I am going to store 3 files in a directory. Every day I want to delete
> the oldest one. How do I do that? The files will have part of their
> name exactly the same.


[...]
quote:

> ls -lrt *filename* | cut -d" " -f2 | how do i know to delete?



With zsh:

rm ./*filename*(Om[1])

With any shell:

rm "`ls -rt ./*filename* | head -1`"

(provided the file names don't contain NL characters).

--
Stéphane ["Stephane.Chazelas" at "free.fr"]
Eric Sosman

2004-01-23, 5:11 pm

Stephane CHAZELAS wrote:
quote:

>
> 2003/11/20, 12:13(-08), Ryan Gaffuri:
> [...]
>
> With zsh:
>
> rm ./*filename*(Om[1])
>
> With any shell:
>
> rm "`ls -rt ./*filename* | head -1`"
>
> (provided the file names don't contain NL characters).



This doesn't meet the (snipped) requirement "also, if i
have less than 3 files in the directory, I do not want to
delete any."

Here's a solution in (so sue me) csh:

set files = ( `ls -rt` )
if ( $#files >= 3 ) rm $files[1]

If there's a chance that the directory might hold a Really
Large number of files, so many that they'd overflow the limit
on command line length, the first line should instead be

set files = ( `ls -rt | head -3` )

--
Eric.Sosman@sun.com
Stephane CHAZELAS

2004-01-23, 5:11 pm

2003/11/20, 17:08(-05), Eric Sosman:
[...]
quote:

[...][QUOTE][color=darkred]
> This doesn't meet the (snipped) requirement "also, if i
> have less than 3 files in the directory, I do not want to
> delete any."



Oops, I missed that requirement.

So, with Bourne like shells:

set x ./*filename*; shift
[ "$#" -ge 3 ] && rm "`ls -rt \"$@\" | head -1`"

And if there may be too many files:

IFS='
'
set -f
set x `ls -rt | grep filename | head -3`
[ "$#" -eq 4 ] && rm "./$2"

With zsh:

list=(./*filename*(Om[1,3]))
(($#list == 3)) && rm $list[1]

--
Stéphane ["Stephane.Chazelas" at "free.fr"]
Bill Marcum

2004-01-23, 5:11 pm

On 20 Nov 2003 12:13:36 -0800, Ryan Gaffuri
<rgaffuri@cox.net> wrote:
quote:

> I am going to store 3 files in a directory. Every day I want to delete
> the oldest one. How do I do that? The files will have part of their
> name exactly the same.
>
> also, if i have less than 3 files in the directory, I do not want to
> delete any.
>
> how do i do this? my scripting skills are weak. I need to do something
> like this I think
>
> ls -lrt *filename* | cut -d" " -f2 | how do i know to delete?



ls -t *filename* | (
read file1 && read file2 && read file3 && rm "$file3" )

--
You can go anywhere you want if you look serious and carry a clipboard.
Stephane CHAZELAS

2004-01-23, 5:11 pm

2003/11/20, 17:43(-05), Bill Marcum:
[...]
quote:

[...][QUOTE][color=darkred]
>
> ls -t *filename* | (
> read file1 && read file2 && read file3 && rm "$file3" )



Should be:

ls -rt ./*filename* | ( IFS=
read -r file && read -r z && read -r z && rm "$file" )

to meet the requirements.


--
Stéphane ["Stephane.Chazelas" at "free.fr"]
Carlos J. G. Duarte

2004-01-23, 5:12 pm

Ryan Gaffuri wrote:
quote:

> I am going to store 3 files in a directory. Every day I want to delete
> the oldest one. How do I do that? The files will have part of their
> name exactly the same.
>
> also, if i have less than 3 files in the directory, I do not want to
> delete any.
>
> how do i do this? my scripting skills are weak. I need to do something
> like this I think
>
> ls -lrt *filename* | cut -d" " -f2 | how do i know to delete?



Ergh... do it with sed:
ls -tr *file* | sed 'x;s/^/./;x;$!d;x;/.../!d;x;s/^/rm -f /' | sh

.... just kidding about the do it with, but it works, anyway ;-)

--
carlos

phil-news-nospam@ipal.net

2004-01-23, 5:12 pm

On 20 Nov 2003 12:13:36 -0800 Ryan Gaffuri <rgaffuri@cox.net> wrote:

| I am going to store 3 files in a directory. Every day I want to delete
| the oldest one. How do I do that? The files will have part of their
| name exactly the same.
|
| also, if i have less than 3 files in the directory, I do not want to
| delete any.

If you can generalize the definition this way: keep the 2 most recent files
the programming it involves sorting the file names by their time, removing
the 2 most recent from the list, and deleting all that remain in the list,
if any.


| how do i do this? my scripting skills are weak. I need to do something
| like this I think

Hire someone who's scripting/programming skills are strong, especially
someone who has years of experience and has seen many kinds of failure
modes from unusual circumstances and knows how to make a script know
when not to destroy everything.

--
-----------------------------------------------------------------------------
| Phil Howard KA9WGN | http://linuxhomepage.com/ http://ham.org/ |
| (first name) at ipal.net | http://phil.ipal.org/ http://ka9wgn.ham.org/ |
-----------------------------------------------------------------------------
Frank da Cruz

2004-01-23, 5:13 pm

In article <bpmmtu025t8@enews4.newsguy.com>, phil-news-nospam@ipal.net wrote:
: On 20 Nov 2003 12:13:36 -0800 Ryan Gaffuri <rgaffuri@cox.net> wrote:
:
:| I am going to store 3 files in a directory. Every day I want to delete
:| the oldest one. How do I do that? The files will have part of their
:| name exactly the same.
:|
:| also, if i have less than 3 files in the directory, I do not want to
:| delete any.
:
: If you can generalize the definition this way: keep the 2 most recent files
: the programming it involves sorting the file names by their time, removing
: the 2 most recent from the list, and deleting all that remain in the list,
: if any.
:
:| how do i do this? my scripting skills are weak. I need to do something
:| like this I think
:
If you have C-Kermit, it's easy:

http://www.columbia.edu/kermit/ckermit.html

Here's the script:

define somedirectory /tmp ; or whatever
cd \m(somedirectory)
if fail stop 1 "CD \m(somedirectory) failed"
directory /sort:date /array:&a *
if fail stop 1 "DIRECTORY \m(somedirectory) failed"
for \%i 3 \fdim(&a) 1 {
delete /list /simulate \&a[\%i]
if fail stop 1 "DELETE \&a[\%i] failed"
}

This deletes all but the two newest files. Brief explanation:

. The directory name is defined symbolically so you only have to
change it one place.

. Each step is checked for success before proceeding to the next so,
for example, we don't delete files from the wrong directory.

. The DIRECTORY command puts the names of the selected file (* in this
case) into the array \&a[] in the specified order (chronological by
modtime in this case).

. The FOR loop deletes all files after the first two (i.e. starting at
file number 3). \fdim(&a) is the number of elements in the array.
The /SIMULATE switch is for testing -- remove it after satisfying
yourself that the script does what you want.

Use Kermit's HELP command to find out more about the DIRECTORY, DELETE,
and other commands. For example, you could use a single DELETE command
to clean out all files that are more than a week old except those that
have "keep" in their names:

delete /before:-1week /except:*keep* *

Add the /RECURSIVE switch if you want to do this in a whole directory
tree.

- Frank
Ryan Gaffuri

2004-01-23, 5:13 pm

Frank da Cruz <fdc@columbia.edu> wrote in message news:<slrnbrv7fu.hb2.fdc@sesame.cc.columbia.edu>...
quote:

> In article <bpmmtu025t8@enews4.newsguy.com>, phil-news-nospam@ipal.net wrote:
> : On 20 Nov 2003 12:13:36 -0800 Ryan Gaffuri <rgaffuri@cox.net> wrote:
> :
> :| I am going to store 3 files in a directory. Every day I want to delete
> :| the oldest one. How do I do that? The files will have part of their
> :| name exactly the same.
> :|
> :| also, if i have less than 3 files in the directory, I do not want to
> :| delete any.
> :
> : If you can generalize the definition this way: keep the 2 most recent files
> : the programming it involves sorting the file names by their time, removing
> : the 2 most recent from the list, and deleting all that remain in the list,
> : if any.
> :
> :| how do i do this? my scripting skills are weak. I need to do something
> :| like this I think
> :
> If you have C-Kermit, it's easy:
>
> http://www.columbia.edu/kermit/ckermit.html
>
> Here's the script:
>
> define somedirectory /tmp ; or whatever
> cd \m(somedirectory)
> if fail stop 1 "CD \m(somedirectory) failed"
> directory /sort:date /array:&a *
> if fail stop 1 "DIRECTORY \m(somedirectory) failed"
> for \%i 3 \fdim(&a) 1 {
> delete /list /simulate \&a[\%i]
> if fail stop 1 "DELETE \&a[\%i] failed"
> }
>
> This deletes all but the two newest files. Brief explanation:
>
> . The directory name is defined symbolically so you only have to
> change it one place.
>
> . Each step is checked for success before proceeding to the next so,
> for example, we don't delete files from the wrong directory.
>
> . The DIRECTORY command puts the names of the selected file (* in this
> case) into the array \&a[] in the specified order (chronological by
> modtime in this case).
>
> . The FOR loop deletes all files after the first two (i.e. starting at
> file number 3). \fdim(&a) is the number of elements in the array.
> The /SIMULATE switch is for testing -- remove it after satisfying
> yourself that the script does what you want.
>
> Use Kermit's HELP command to find out more about the DIRECTORY, DELETE,
> and other commands. For example, you could use a single DELETE command
> to clean out all files that are more than a week old except those that
> have "keep" in their names:
>
> delete /before:-1week /except:*keep* *
>
> Add the /RECURSIVE switch if you want to do this in a whole directory
> tree.
>
> - Frank



im on Korn Shell
Frank da Cruz

2004-01-23, 5:13 pm

In article <1efdad5b.0311240614.622c572c@posting.google.com>,
Ryan Gaffuri wrote:
: Frank da Cruz <fdc@columbia.edu> wrote in message
: news:<slrnbrv7fu.hb2.fdc@sesame.cc.columbia.edu>...
:> In article <bpmmtu025t8@enews4.newsguy.com>,
:> phil-news-nospam@ipal.net wrote:
:> : On 20 Nov 2003 12:13:36 -0800 Ryan Gaffuri <rgaffuri@cox.net> wrote:
:> :
:> :| I am going to store 3 files in a directory. Every day I want to delete
:> :| the oldest one. How do I do that? The files will have part of their
:> :| name exactly the same.
:> :| ...
:> :| how do i do this? my scripting skills are weak. I need to do something
:> :| like this I think
:> :
:> If you have C-Kermit, it's easy:
:>
:> http://www.columbia.edu/kermit/ckermit.html
:
: im on Korn Shell
:
Kermit is a program. You can run it from Korn shell, or Bash, or any other
shell, just like you would run Perl.

- Frank
Mike Chirico

2004-01-23, 5:17 pm


"Carlos J. G. Duarte" <cjgd@clix.pt> wrote in message
news:bplp71$1peojg$1@ID-201565.news.uni-berlin.de...[QUOTE][color=darkred]
> Ryan Gaffuri wrote:

#!/bin/bash
#
# Only keep the most recent NUMLOGS log files
# that haven't changed status in the last n*24 hours
#
# The find command finds all '*.log' files
# "-ctime +n" for files change status > n*24
# "-maxdepth 1" only search the current directory
# "-type f" standard files
# "-printf "%A@" returns seconds since Jan 1, 1970
#
# Sort does just that...sort
#
# awk gets count of files > NUMLOGS
# the name plus the path

# User defined variables
LOGFILE='*.c'
NUMLOGS=2
CTIME='+3' # n*24 hours
#
#
for i in `find . -ctime "${CTIME}" -type f -maxdepth 1 -name
${LOGFILE}" -printf "%A@ %p\n" 2>/dev/null|sort |awk 'NR > '"${NUMLOGS}"'
{print $2}'`
do
echo $i;
# Change echo to rm $i once you're convinced it works
# or could archive
done


Regards,

Mike Chirico


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com