LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script to remove files older than 3 days (http://www.linuxquestions.org/questions/linux-general-1/bash-script-to-remove-files-older-than-3-days-462290/)

rust8y 07-09-2006 05:27 AM

Bash script to remove files older than 3 days
 
Can someone help me with a bash script to remove files older than 3 days in directory /u1/database/prod/arch?

druuna 07-09-2006 05:53 AM

Hi,

You could use a 'simple' one-liner for this:

find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

Or as bash script:
Code:

#!/bin/bash

find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

The only 2 commands used are find and rm.

Find looks for files (-type f), this to exclude directories, that are older then 3 days (-mtime +3). All it finds is given to rm (-exec rm {} \; ).

You could also place the rm statement outside of find, which is supposed to be faster:

find /u1/database/prod/arch -type f -mtime +3 | xargs rm

All the three examples do their searching recursively.

man find for details.

Hope this helps.

rust8y 07-09-2006 08:49 PM

Thank you for your help.

rust8y 07-09-2006 08:53 PM

find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

What does the ; at the end do?

perfect_circle 07-09-2006 09:02 PM

man is your friend. Try
Code:

man find
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

this command finds all the files under /u1/database/prod/arch and it's subfolders, that are "regular files" (-type f) not directories, device files or something like that, and that have been modified at least 3 days ago (-mtime +3) and then executes "rm <filename>" for those files.

tiekookeit 09-04-2009 09:19 AM

Wonderful
 
Thanks for your help!!!

catkin 09-04-2009 10:08 AM

Quote:

Originally Posted by rust8y (Post 2327172)
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

What does the ; at the end do?

The "\;" at the end tells find where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.

Photar 10-06-2009 12:57 PM

Quote:

Originally Posted by catkin (Post 3669731)
The "\;" at the end tells find where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.

You can also do ';'

trunikov 11-09-2009 08:09 AM

I would like to say that you can use option -delete to remove files instead of tricks with rm and xargs.
Sample:

find /path/dir -name "*.bz2" -type f -Btime +30d -delete

Also keep in mind that file node actually has three times: created, last accessed, last modified.

ziggy25 12-02-2009 07:43 AM

Quote:

Originally Posted by catkin (Post 3669731)
The "\;" at the end tells find where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.

Hi,

Can you explain what the {} \; characters are for.

thanks

druuna 12-02-2009 07:51 AM

Hi,

The \; part is already explained by catkin in the post you quoted.

The {} holds what is found by find and given to the executed command. -exec <command> {} is the general form.

Hope this helps.

pixellany 12-02-2009 07:52 AM

Quote:

Originally Posted by ziggy25 (Post 3777052)
Hi,

Can you explain what the {} \; characters are for.

thanks

The post you quoted already explained part of it
Code:

(\;)
"{}" is a placeholder---look at the man page for find--under the -exec command

chrism01 12-02-2009 11:21 PM

@trunikov

1. Unix does not a have file creation time
Quote:

-ctime n
File's status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times.
http://linux.die.net/man/1/find

2. If you've really got RH9 (Shrike), you should really update to a current distro. That one hasn't been updated in years and would be likely to be exploited.
http://en.wikipedia.org/wiki/Red_Hat_Linux
Try Fedora 12 or Centos 5.4

will177 01-07-2010 03:48 PM

Quote:

Originally Posted by trunikov (Post 3750269)
I would like to say that you can use option -delete to remove files instead of tricks with rm and xargs.
Sample:

find /path/dir -name "*.bz2" -type f -Btime +30d -delete

I like this way, but -Btime does not exist on my version of find, and it's not +30d for me but just +30

So I ended up using:


find /path/dir -mtime +30 -delete


as I wanted to delete all files and all directories under /path/dir

Thanks!

dsoria 10-21-2010 06:01 PM

hello!
 
Hello

im new..

regars to all


All times are GMT -5. The time now is 07:42 PM.