[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

script to remove files > then 7 days old.

I have a a directory called upload in which I create hidden directories for users  .uername1   .username2.   How would I go about creating a script that will remove files from each hidden directory that are more then 7 days old.
0
dplinnane
Asked:
dplinnane
3 Solutions
 
sunnycoderCommented:
Hi dplinnane,

find -type f  -ctime 7


Sunnycoder
0
 
sunnycoderCommented:
find <dir> -ctime +3 -type f -exec rm {}

Be careful though. You might want to replace the
     -exec rm {} \;
by
     -print
first to make sure that you are going to delete the files you really mean to delete

also as per your requirements, you may need atime in place of ctime ... refer to find man page for details
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dplinnaneAuthor Commented:
Cool, I'll check them out.
I'm a php programmer so I can run commands using  exec(sheel command);
if I wanted to run this without using php what kind of a file would I place the code in (I don't know a whole lot about shell scripting just basic commands)  filename.ext     ext?  My aim is to run a cron job every 7days. I have figured out how to do that running a php file but am curious to know an alternative method.
Thanks.
0
 
sunnycoderCommented:
you can run this command from the prompt

you can store it in a file with the first line of the file being

#!/bin/bash

followed by the command(s)

store it as filename.sh

chmod 0755 filename.sh  (set mask as you want, but it should be executable for the uuser who wishes to execute it)
0
 
da99rmdCommented:
Then just make a cronjob with
crontab -e
and press insert and then write  
0 4 * * * (path to script)/filename.sh 1> /dev/null (and ev to make all err mess disaper 2> /dev/null)
then
shift+zz
and it will run every night at 4 removing those files. from those dirs.

/Rob
0
 
stefan73Commented:
Hi da99rmd,
> cronjob
That's the best place for cleanup jobs, yes.

Cheers,
Stefan
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now