I am trying to find out how to tell if a given file is 30 days or older. How is that type of thing done in shell scripts. Thanks, Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.centos.org/pipermail/centos/attachments/20050915/e412e896/attachment.html>
Alfred von Campe
2005-Sep-15 16:26 UTC
[CentOS] how to tell if a file is older than 30 days?
> I am trying to find out how to tell if a given file is 30 days or > older. > How is that type of thing done in shell scripts.You want to get the modification time of the file most likely. This can be done using the "stat" command. This will return the "Time of last modification as seconds since Epoch" of the given file: stat --format="%Y" /path/to/file Just compare this number to the current time (in seconds since Epoch) and you can determine the age of a file. Alfred
From: Jerry Geis [mailto:geisj at pagestation.com]> > I am trying to find out how to tell if a given file is 30 days or older. > How is that type of thing done in shell scripts.find directoryname -mtime +30 -print Will list all files in a directory that have not been modified in the last 30 days. Read the man page for 'find'. It can find files based on almost anything. Bowie
Bryan J. Smith
2005-Sep-15 16:33 UTC
[CentOS] Re: how to tell if a file is older than 30 days? -- [practices] find -mtime usage
[ We could _really_ use a "practices" list. ;-] Jerry Geis <geisj at pagestation.com> wrote:> I am trying to find out how to tell if a given file is 30 > days or older.Do you mean accessed time (atime), changed attributes time (ctime) or modification time (mtime)? I'll assume you want "mtime". The find command is extremely powerful. E.g., find / -mtime +30 This will give you all files in the system that have been modified over 30 days ago (i.e., 31+). If you just want to look in the _current_directory_: find . -maxdepth 1 -mtime +30 If you want to look at a particular file: find /home/myfile -maxdepth 0 -mtime +30 If the file is listed, it was last modified 31+ days ago.> How is that type of thing done in shell scripts.Here's an example script that takes a list of files and moves the files under /home/old (full path and attributes preserved) if they are older than 30 days. Syntax: fold30 file [file...] Script: 1: [ "$1" == "" ] && exit 1 2: mylist="$*" 3: for myfile in ${mylist}; do 4: if [ -f "${myfile}" ]; then 5: if [ "`find ${myfile} -mtime +30`" != "" ]; then 6: echo ${myfile} | cpio -pmdvu /home/old/${myfile} 7: rm -f ${myfile} 8: fi 9: fi A: done The main conditional on line 5 is what does the job. If the result of the find command is not an empty string, then the name was returned and it is older than 30 days. Now move it to the same path under /home/old (using cpio) and remove the original. NOTE: It will _not_ work if spaces are used in the paths, that's something I've yet to see work correctly (I can never get the "IFS" variable set to work proper in the loop) in bash, so I professionally use tcsh instead. Bash does not seem to work with quotes in a loop like tcsh does, and the Advanced Bash Scripting Guide (ABSG) is just _dead_wrong_ on using quotes for most bash releases (although it works fine across all tcsh versions I've used). -- Bryan J. Smith | Sent from Yahoo Mail mailto:b.j.smith at ieee.org | (please excuse any http://thebs413.blogspot.com/ | missing headers)
Jerry Geis wrote:> I am trying to find out how to tell if a given file is 30 days or older. > How is that type of thing done in shell scripts. > > Thanks, > > JerryAs with all things *nix, there is more than one way to do the job. You might start with this idea -- or not. If you wanted to list all files in the current directory over 29 days old (you said "30 or older) this would do it: $ find . -ctime +29 -print --OR-- those older than 29 days in your home directory $ find ~ -ctime +29 -print --OR-- to discover older rpms in the yum cache $ find /var/cache/yum -ctime +29 -print | grep .rpm Man find is your friend. Don't be overwhelmed by its many options; start simple and build up to your goal.> >------------------------------------------------------------------------ > >_______________________________________________ >CentOS mailing list >CentOS at centos.org >http://lists.centos.org/mailman/listinfo/centos > >