How to write an Auto scripting for deleting old files using
shell script and made a cron job to run on daily basis

Answer Posted / amit bondwal

#!/bin/bash
find /home/amitbondwal/ -type f -mtime +30 -exec rm -f {} \;
done
#change the directory path or name and number of days
#according to your requirement. This script will delete all
#the file older than 30 days in /home/amitbondwal directory

save the above script in a text file and run it by crontab

crontab -e

00 02 * * * /bin/sh /home/amitbondwal/scriptname.sh

This setting will run this script at 2 AM everyday.

Is This Answer Correct ?    12 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which shell is the best?

576


What is the use of .sh file?

608


What is the best shell scripting language?

541


Print a given number, in reverse order using a shell script such that the input is provided using command line argument only.

703


Rewrite the command to print the sentence and convert the variable to plural: echo “i like $variable”.

569






What is difference between bash and shell?

556


I have to write Shells (Linux + Unix)for publishing packages and reports. Is it possible ? What are the differents executable programs ineed to call ?

1685


What is a shell in operating system?

591


What is sudo command?

553


What is mac default shell?

614


What does $$ mean in shell script?

588


What can scripts do?

666


What does $@ mean bash?

668


Why should we use shell scripts?

592


Why are there shells on the beach?

597