About Linux FAQ

Browse More FAQs:

Find and Delete File If It Is More Than One Hour Old in UNIX Shell

Posted by Vivek on Sunday April 20, 08 (4 weeks ago) @11:43 am

Q. How do I find out file last modification time using a shell script or command? How do I delete or take any other custom action for all files more than one hour old in /home/ftp/incoming/raw/ directory?

A. There are many ways (commands) to find out file modification time under UNIX / Linux operating system. You can try any one of the following command:

find command (with -cmin switch)

$ find /home/ftp/incoming/raw/ -maxdepth 1 -cmin +60 -name FileName
The -cmin option will print FileName's status was last changed n minutes ago. This command will print all file names more than one hour old.

stat command (with -c switch)

To find time of last change as seconds since Epoch, enter:
$ stat -c %Z /path/to/file

date command (with -r switch)

To display the last modification time of FILE, enter:
$ date -r /path/to/file
I recommend using find command as it has -exec option to take action on all matching file such as move or delete files:
$ find /home/ftp/incoming/raw/ -maxdepth 1 -cmin +60 -name "*" -exec /bin/rm -f {} \;

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Linux / UNIX FAQ:

Discussion on This FAQ

  1. Jeff Schroeder Says:

    find … -exec is BAD!

    For every single result, it will fork off a copy of the command you exec. So if the above find command returns 500 files, rm will be forked 500 times.

    Instead of doing it the above way, try this:
    find /home/ftp/incoming/raw/ -maxdepth 1 -cmin +60 | xargs rm -rf

    The -name ‘*’ argument to find was redundant.

  2. ojo Says:

    Isn’t it better to use null character as a files separator (because of spaces in file/dir names)? like this:

    find /home/ftp/incoming/raw/ -print0 -maxdepth 1 -cmin +60 | xargs -0 rm -rf

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , ~ Last updated on: April 20, 2008

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.