Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:25:46+00:00 2026-05-27T18:25:46+00:00

PLEASE NOTE THAT I CANNOT USE ‘find’ IN THE TARGET ENVIRONMENT I need to

  • 0

PLEASE NOTE THAT I CANNOT USE ‘find’ IN THE TARGET ENVIRONMENT

I need to delete all files more than 7 days old in a linux shell script. SOmething like:

FILES=./path/to/dir
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
  # perhaps stat each file to get the last modified date and then delete files with date older than today -7 days.

done

Can I use ‘stat’ to do this? I was trying to use

find *.gz -mtime +7 -delete

but discovered that I cannot use find on the target system (there is no permission for the cron user and this can’t be changed). Target system is Redhat Enterprise.

The file names are formatted like this:

gzip > /mnt/target03/rest-of-path/web/backups/DATABASENAME_date "+%Y-%m-%d".gz

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:25:47+00:00Added an answer on May 27, 2026 at 6:25 pm

    Since you have time in the filename then use that to time the deletion heres some code that does that :

    This script gets the current time in seconds since epoch and then calculates the timestamp 7 days ago. Then for each file parses the filename and converts the date embeded in each filename to a timestamp then compares timestamps to determine which files to delete. Using timestamps gets rid of all hassles with working with dates directly (leap year, different days in months, etc )

    The actual remove is commented out so you can test the code.

    #funciton to get timestamp X days prior to input timestamp
    # arg1 = number of days past input timestamp
    # arg2 = timestamp ( e.g. 1324505111 ) seconds past epoch
    getTimestampDaysInPast () {
        daysinpast=$1
        seconds=$2
        while [ $daysinpast -gt 0 ] ; do
        daysinpast=`expr $daysinpast - 1`
        seconds=`expr $seconds - 86400`
        done
    # make midnight
        mod=`expr $seconds % 86400`
        seconds=`expr $seconds - $mod`
        echo $seconds
    } 
    # get current time in seconds since epoch
    getCurrentTime() {
        echo `date +"%s"`
    }
    
    # parse format and convert time to timestamp
    # e.g. 2011-12-23 -> 1324505111
    # arg1 = filename with date string in format %Y-%m-%d
    getFileTimestamp () {
        filename=$1
        date=`echo $filename |  sed "s/[^0-9\-]*\([0-9\-]*\).*/\1/g"`
        ts=`date -d $date | date +"%s"`
        echo $ts
    }
    
    ########################### MAIN ############################
    # Expect directory where files are to be deleted to be first 
    # arg on commandline. If not provided then use current working
    # directory
    
    FILEDIR=`pwd`
    if [ $# -gt 0 ] ; then 
        FILEDIR=$1
    fi
    cd $FILEDIR
    
    now=`getCurrentTime`
    mustBeBefore=`getTimestampDaysInPast 7 $now`
    SAVEIFS=$IFS
    # need this to loop around spaces with filenames
    IFS=$(echo -en "\n\b")
    # for safety change this glob to something more restrictive
    for f in * ; do 
        filetime=`getFileTimestamp $f`
        echo "$filetime lt $mustBeBefore"
        if [ $filetime -lt $mustBeBefore ] ; then
        # uncomment this when you have tested this on your system
        echo "rm -f $f"
        fi
    done
    # only need this if you are going to be doing something else
    IFS=$SAVEIFS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Please note that this question is OUTDATED; RVM got way easier to use
[Please note that this is a different question from the already answered How to
(Please note that I have seen a similar question on StackOverflow recently, however I
Please note that this is not homework and i did search before starting this
Please note that I did ask this on Super User and received absolutely no
Please note that this question is from 2008 and now is of only historic
I mean things like: FK1 -> 1FK2 -> 2PK Please, note that 1FK2 is
Please note the Edit below for a lot more information, and a possible solution
Please Note: answers along the lines of 'use jquery' or 'use (insert wellknown framework)'
Please note - I am not looking for the right way to open/read a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.