LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to remove all files from a directory which are 24 hr old (https://www.linuxquestions.org/questions/programming-9/how-to-remove-all-files-from-a-directory-which-are-24-hr-old-800816/)

siris 04-08-2010 11:15 AM

how to remove all files from a directory which are 24 hr old
 
hi

I was preparing a script which will remove all my files from directory which are 24 hour old.

I tried some thing like this

find . \( -name 'log.*' -mtime +1 \) -exec rm {};
but it is throughing error like : missing argument to exec.

please help me in this regard.

ncsuapex 04-08-2010 11:26 AM

You're trying to delete files OVER 24 hours old?

find /path -name "log.*" -mtime -1 -type f -exec ls -lh {} \;

Run that first to see the files incase the command is not what you're looking for.

then run:

find /path -name "log.*" -mtime -1 -type f -exec rm -f {} \;


Change the / to your path.

siris 04-09-2010 12:45 AM

Thanks,

but the command find /path -name "log.*" -mtime -1 -type f -exec ls -lh {} \;
is throwing some error saing that: missing arguments to -exec.

I tried one more thing like find /sisback -name "log.1*" -type f -mtime -1 |xargs rm
but it is showing that-- /sisback no such file or directory. though I have a directory in the same name in my present working directory.

Need your help please...

crts 04-09-2010 02:46 AM

Quote:

Originally Posted by siris (Post 3929477)
Thanks,

but the command find /path -name "log.*" -mtime -1 -type f -exec ls -lh {} \;
is throwing some error saing that: missing arguments to -exec.

I tried one more thing like find /sisback -name "log.1*" -type f -mtime -1 |xargs rm
but it is showing that-- /sisback no such file or directory. though I have a directory in the same name in my present working directory.

Need your help please...

Try this
Code:

find /path -name "log.*" -mtime -1 -type f -exec ls -lh '{}' \;
Notice the single quotes around '{}'.

jschiwal 04-09-2010 03:45 AM

Usually the error you gave means you didn't end the -exec command with `\;'.

You can also use the -delete command.
find /dir -mtime +1 -delete

The command with -exec ls could have been:
find /dir -mtime +1 -ls

bakdong 04-09-2010 03:54 AM

It might also be worth looking at tmpwatch.

DESCRIPTION
tmpwatch recursively removes files which haven't been
accessed for a given number of hours. Normally, it's used
to clean up directories which are used for temporary hold-
ing space such as /tmp.

bakdong 04-09-2010 04:13 AM

Quote:

Originally Posted by siris (Post 3929477)
I tried one more thing like find /sisback -name "log.1*" -type f -mtime -1 |xargs rm
but it is showing that-- /sisback no such file or directory. though I have a directory in the same name in my present working directory.

You're getting an error because you're looking for /sisback, i.e. the sisback directory off the root directory, not the current directory. If you really want the current directory you need './sisback'

Code:

find /path -name "log.*" -mtime -1 -type f -exec ls -lh {} \;
May need to be:

Code:

find /path -name "log.*" -mtime -1 -type f -exec ls -lh '{}' ';'
Depending on how your shell expansion is working.

ncsuapex 04-09-2010 08:16 AM

What OS and what shell are you using? The examples I posted work for me on a CentOS OS using bash shell.

siris 04-10-2010 01:23 AM

Thanks to all...

I am using Fedora-9 and bash shell...


All times are GMT -5. The time now is 06:14 PM.