Find the answer to your Linux question:
Results 1 to 6 of 6
hi all I've got a shell script which suppose to delete files older then 7 days. Unfortunatly it doesn't work. here is the script: #--------------------------------------- # Configration Variables # HOSTNAME=`hostname` ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    16

    Exclamation Deleting files older then 7 days

    hi all

    I've got a shell script which suppose to delete files older then 7 days.

    Unfortunatly it doesn't work.

    here is the script:
    #---------------------------------------
    # Configration Variables
    #
    HOSTNAME=`hostname`
    BACKUP_DIR=/mysql/backup
    BACKUP_DAYS=7



    find $BACKUP_DIR -name ${HOSTNAME}* -mtime +${BACKUP_DAYS} -print -exec rm -f {} \;


    I run it as root.
    Can anyone tell me why it doesn't work.

    Thanks
    Any help appreciated

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, what exactly doesn't work about it?

    If you run it by hand, does it work? Are you sure that there are files older than 7 days in the directory? Also, consider using "-delete" instead of the "-exec" you have.

    One thing to consider would be surrounding the * in single quotes. The way you have it now, the pattern expansion is being performed by the shell. From the find man page:
    Code:
    NON-BUGS
           $ find . -name *.c -print
           find: paths must precede expression
           Usage: find [-H] [-L] [-P] [path...] [expression]
    
           This  happens  because  *.c has been expanded by the shell resulting in
           find actually receiving a command line like this:
    
           find . -name bigram.c code.c frcode.c locate.c -print
    
           That command is of course not going to work.  Instead of  doing  things
           this  way, you should enclose the pattern in quotes or escape the wild-
           card:
           $ find . -name \*.c -print
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    15
    Quote Originally Posted by alexsqlforums View Post
    hi all

    I've got a shell script which suppose to delete files older then 7 days.

    Unfortunatly it doesn't work.

    here is the script:
    #---------------------------------------
    # Configration Variables
    #
    HOSTNAME=`hostname`
    BACKUP_DIR=/mysql/backup
    BACKUP_DAYS=7



    find $BACKUP_DIR -name ${HOSTNAME}* -mtime +${BACKUP_DAYS} -print -exec rm -f {} \;


    I run it as root.
    Can anyone tell me why it doesn't work.

    Thanks
    Any help appreciated
    You should escape {} wit bachslashes like here.
    And as previous poster said you gave add '' around ${HOSTNAME}*
    find $BACKUP_DIR -name "${HOSTNAME}*" -mtime +${BACKUP_DAYS} -print -exec rm -f \{\} \;

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    16

    Sorry Guys

    THE SCRIPT DOES WORK

    I had files from nov 6th and on and thought the script would delete them on the 13th or 14th. since they are more than 7 days.

    It did delete them this morning.
    But the weird thing is it deleted nov 6th and nov 7th files.

    I'm still new to linux and learning as I go along.

    Valery and Cabhan thank you for your responses.

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Valery Reznic View Post
    You should escape {} wit bachslashes like here.
    Can you explain why, please? The shell doesn't put any significance to braces except when used in variable expansion, surely? I've tried ksh, bash and <spit>csh</spit> and they all work fine without being escaped.

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You don't have to escape the {}. Your -name field would have to have the * escaped if you didn't enclose it with "s.

    "${HOSTNAME}*"
    or
    ${HOSTNAME}\*

    Vic

Posting Permissions

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