LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to delete files (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-delete-files-607832/)

smurff 12-19-2007 06:21 AM

Trying to delete files
 
Hi All,

Im not the most experienced scripter but I need to look for certain file types that are over a certain age and remove them.

I found the following on a site but I keep getting errors. Any help would be great. Thanks and kind regards
Danny

The command in my script

Code:

/usr/bin/rm `/usr/bin/find dbbackup -name '*.dmp' -mtime +10`
And the error is

Code:

usage: rm [-fiRr] file ...

colucix 12-19-2007 07:03 AM

You can try the following syntax, using xargs
Code:

find dbbackup -name "*.dmp" -mtime +10 -print0 | xargs -0 echo rm
xargs is an utility to build and execute commands from standard input (see man xargs for details). Two notes: the -print0 option of find, and the -0 option of xargs, ensure the filenames are taken as they are (even if they contain blank spaces). These options simply print/get filenames followed by a null character, instead of newline). Also I deliberately put an "echo rm" command after xargs to let you test before actually removing anything. When you are sure of the results, take out the echo and the trick is done.

matiasar 12-19-2007 09:06 AM

RE: Trying to delete files
 
Hi Smurff,

I think that may be you can try rm to force delete (with '-f').

/bin/rm -f `/usr/bin/find dbbackup -name '*.dmp' -mtime +10`

I think you get that message when find doesn't find any archive that meets the condition. By adding -f you may get rid of that message.

Regards,
Matías


I just changed the paths to fit my system and try your command, but
Quote:

Originally Posted by smurff (Post 2995673)
Hi All,

Im not the most experienced scripter but I need to look for certain file types that are over a certain age and remove them.

I found the following on a site but I keep getting errors. Any help would be great. Thanks and kind regards
Danny

The command in my script

Code:

/usr/bin/rm `/usr/bin/find dbbackup -name '*.dmp' -mtime +10`
And the error is

Code:

usage: rm [-fiRr] file ...


techwatcher 12-19-2007 10:13 AM

I'm really new to this stuff, but are you sure you need the path in front of the 'rm?' Maybe there's just no rm within /usr/bin/?

And can you use quotes like that to pass the argument of the operation in all distros?

pixellany 12-19-2007 11:30 AM

Quote:

I think you get that message when find doesn't find any archive that meets the condition. By adding -f you may get rid of that message.
-f option is to force an action. It's not going to help find go deeper into the tree.

Quote:

And can you use quotes like that to pass the argument of the operation in all distros?
Those aren't quotes--they are "backtics"--used to pass the output of one command to another.

To smurff: The trick is to first run the find command and be sure it does what you expect. Then you can incorporate it in a more complex structure. The error message implies that "rm" was given no arguements.

smurff 12-19-2007 06:32 PM

Guys,

Thank you all so much for taking the time to answer.

Colucix, thank you. Its good to know but that command did not seem to be on my solaris box. But thank you anyway.

Matiasar, Thank you, the -f was the issue. Thank you very much.

Techwatcher, I too am quite new, well I have been doing this for quite some time but I dont go out of my confort zone to often :)

Pixellany, Thanks. I did take the line apart. Thats good advice.

Code:

[smurff@nemo ~]$ ls dbbackup/
1.dmp  2.dmp  3.x
[smurff@nemo ~]$ find dbbackup/ -name '*.dmp' -mtime +10
[smurff@nemo ~]$ rm `find dbbackup/ -name '*.dmp' -mtime +10`
rm: too few arguments
Try `rm --help' for more information.
[smurff@nemo ~]$ rm -f `find dbbackup/ -name '*.dmp' -mtime +10`
[smurff@nemo ~]$


Uncle_Theodore 12-19-2007 07:01 PM

Why don't you use the built-in functionality of find? Something like
find dbbackup -name '*.dmp' -mtime +10 -exec rm {} \;
(notice the blank between the closing bracket and the slash).

chrism01 12-19-2007 07:45 PM

Note that when you ran
find dbbackup/ -name '*.dmp' -mtime +10
you got no output; no files are old enough to match

from the man page:

TESTS
Numeric arguments can be specified as

+n for greater than n,

-n for less than n,

n for exactly n.

Try ls -l on your files to check the last modified times.

jlliagre 12-22-2007 11:46 PM

Quote:

Originally Posted by smurff (Post 2996359)
Colucix, thank you. Its good to know but that command did not seem to be on my solaris box. But thank you anyway.

I'm afraid you post on the wrong forum then.
If your question is Solaris specific, report it for it to be moved on the Solaris forum. If you are looking for a solution working on both Linux, Solaris and other systems, then ask for a move to the programming forum.


All times are GMT -5. The time now is 02:54 PM.