 |
|
 |
|
01-23-04 10: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?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10: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"]
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10:11 PM
2003/11/20, 17:08(-05), Eric Sosman:
[...]quote:
[...][QUOTE]
> 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"]
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10: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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10:11 PM
2003/11/20, 17:43(-05), Bill Marcum:
[...]quote:
[...][QUOTE]
>
> 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"]
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10: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 fil
es
: 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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: deleting a file by date |
 |
 |
|
|
01-23-04 10: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 wro
te:
> : 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 f
iles
> : the programming it involves sorting the file names by their time, removi
ng
> : the 2 most recent from the list, and deleting all that remain in the lis
t,
> : 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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 01:23 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|