Find the answer to your Linux question:
Results 1 to 3 of 3
Hi everybody, I've have a shell script called by crontab every week. This script makes a tar file of different files, and save this tar file into a directory. This ...
  1. #1
    Just Joined!
    Join Date
    Jul 2006
    Posts
    51

    [SOLVED] Remove the oldest file of a directory

    Hi everybody,

    I've have a shell script called by crontab every week.
    This script makes a tar file of different files, and save this tar file into a directory.

    This directory contains 4 tar files, one of every week since I started to execute the script.

    Now I would like to automatically remove the oldest tar file of the directory every time the script is invoqued again.

    How can I do that??
    Can anyone help me??

  2. #2
    Just Joined!
    Join Date
    Apr 2006
    Location
    NE Ohio
    Posts
    7
    Aleix,
    Something like this should do what you're describing...
    ls -t -r -1 /var/log/kdm.log.* | head --lines 1 | xargs rm

    lists the "kdm.log" files in "/var/log", in reverse modificationtime order, one file per line; uses "head" to just pull the first line; then executes the "rm" command.

    Similarly, you could do a...
    ls -t -1 /var/log/kdm.log.* | tail --lines 1 | xargs rm

    which omits the reverse sort and takes the last line as the argument to the rm command.

    --Bryan
    (This post contains code that is supplied as an example only, without any warranties, expressed or implied.)

  3. #3
    Just Joined!
    Join Date
    Jul 2006
    Posts
    51

    It works

    It works very well...

    thanks Bryan!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •