

Here, dot (.) - Indicates the current directory. It does define $HOME, so you could use $HOME/DIRECTORY1/DIRECTORY2. -mtime +30 -print The above command will find and display the older files which are older than 30 day in the current working directory. mtime - Indicates the file modification time and is used to find files older than 30 days. If there is no files exists to remove older than 10 days, It should not do anything. Here, dot (.) - Indicates the current directory. 1 05-23-2012 rajeshjohney Registered User 3, 0 Delete files older than 10 Days in a directory Hi All I want to remove the files with name like data.csv from the directory older than 10 days. From man find: Numeric arguments can be specified asįourth, be sure you have Write access to the directories in /home/USER/DIRECTORY1/DIRECTORY2/.įifth, do you mean /home/USER/DIRECTORY1/DIRECTORY2/ or /home/$USER/DIRECTORY1/DIRECTORY2/? If $USER is for the user's userid, you have a problem: cron doesn't define $USER in the runtime enviroinment. The above command will find and display the older files which are older than 30 day in the current working directory. You should use -mtime +29, which find sees as " more than 29", which is what you want. Third, the find test " -mtime 29" is telling find "Find the file's mtime, and return True if it's equal to 29.
#Bash delete files older than 30 days how to
I use filename because they are for backups so they would look like this: . To begin, lets see how to list files showing their date using the ls (List) command followed by the -l flag for long.

Second, always, Always, ALWAYS test find with -print, and get it to work, before considering -delete. I use one to delete backups older than 10 days and it looks something like this: 50 17 find /path/to/files/filename -type f -mtime +10 xargs rm. Keeping a command in crontab makes configuring, logging and debugging harder, and the crontab command parser isn't as clever as bash's. Hi Donald: Using -mtime +1 means to return true if the modification time is larger than n or in this case larger than one (1). If you have an encrypted home directory ( cat /home/.ecryptfs/$USER/.ecryptfs/Private.mnt) you'll have to store your script outside your $HOME directory tree.

command in a bash script, and call that script from your crontab.
