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 {} \;
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 2 comments… read them below or add one }
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.
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