Cron Job help

I need to write the cron job for the following scenario, Please help me out

The CRON job runs sometime at night on Saturday and checks if there are more than eight files in the /PROCESSED folder. Any files over and above eight are deleted based on ascending order of date (eight most recent files are retained).\

Thanks in Advance.
Sandeep Dwivedi PMP

To be set in the crontab :

ls -t /PROCESSED/* | tail +9 | xargs rm
1 Like

Thanks

I get the below error message when I try to execute this command
______________________________________________
$ ls -t /home/sandeep/* | tail +9 | xargs rm
tail: cannot open `+9' for reading: No such file or directory
rm: missing operand
Try `rm --help' for more information.

which OS are you running ?
tail doesn't have the "+" option ?
(i have tested it on my system and it worked : FreeBSD, the + option also exists on Solaris)
(if the tail fails, it is normal that the xargs rm fails)

Are you sur you currently have more than 8 files in your /home/sandeep ?

(you initially stated that the directory was /PROCESSED instead of /home/sandeep

1 Like

The + option may not be available in all unix flavors. The + option takes from a line forward. Thus

file1=
a1
b2
c3
d4

command like the following
>tail -n +2 file1

gives
b2
c3
d4

Suggest your try the command syntax at your prompt. Also verify your default shell vs. the shell your script is running.

1 Like

Ok then ...

ls -t /PROCESSED/* | tail -n +9 | xargs rm
1 Like

Tried running this command on anather UNIX machine
ls -t /PROCESSED/* | tail +9 | xargs rm
I get the following error message
usage:rm [-fiRr] file ...

looks more like you have less than 8 files, and so the command rm does not appreciate to be called for nothing and reminds you its usage...

1 Like

Thanks! That Worked!

If you want get rid off error message you can redirect the error standard output (associated to file descriptor 2) to the trash (/dev/null)

ls -t /PROCESSED/* | tail +9 | xargs rm 2>/dev/null
1 Like

Thanks Everyone!
Superb Forum, Great Guyz