Operating System - HP-UX
1822008 Members
3907 Online
109639 Solutions
New Discussion юеВ

Re: find and delete files at the same time ?

 
SOLVED
Go to solution
Tan Shirley
Frequent Advisor

find and delete files at the same time ?

Hi,

Is there any command to find a list of files of a particular extension, then delete them?
Because my /home is full, and after my find command, I've got a long list of the different directories which contains the files with extension .out but it's too tedious for me to navigate into each and every directory from the list and remove them manually.
Please help !

# find /home/dgm -name *.out
/home/dgm/dgmsls74/DGM1039.out
/home/dgm/dgmsls74/DGM1019.out
/home/dgm/dgmsls74/DGM1030.out
/home/dgm/dgmsls74/DGM1067.out
/home/dgm/dgmsls74/DGM1003.out
/home/dgm/dgmecc03/DGM1037.out
/home/dgm/dgmecc03/DGM1030.out
/home/dgm/dgmecc03/DGM1011.out
/home/dgm/dgmecc03/DGM1100.out
/home/dgm/dgmecc03/DGM1020.out
/home/dgm/dgmecc03/DGM1007.out
/home/dgm/dgmecc03/DGM1042.out
/home/dgm/dgmecc03/DGM1061.out
/home/dgm/dgmecc03/DGM1091.out
/home/dgm/dgmecc03/DGM1063.out
/home/dgm/dgmecc03/DGM1035.out
/home/dgm/dgmecc03/DGM1044.out
/home/dgm/dgmecc03/DGM1050.out
/home/dgm/dgmecc03/DGM1064.out
/home/dgm/dgmecc03/DGM1049.out
/home/dgm/dgmecc03/DGM1096.out
/home/dgm/dgmecc03/DGM1082.out
/home/dgm/dgmecc03/DGM1047.out

(and the list goes on...)


Thanks a million !

14 REPLIES 14
rajsri
Frequent Advisor

Re: find and delete files at the same time ?

Hi hope this wil help you , you can find this in man page of find command.

Remove all files named a.out or *.o that have not been accessed for a week:

find / ( -name a.out -o -name '*.o' ) -atime +7 -exec rm {} ;

For your problem you can try this .

find /home *.out -exec rm {} ;

good luck .
Madanagopalan S
Frequent Advisor

Re: find and delete files at the same time ?

Try this:

find /home -name '*.out' -print -exec rm -f {} ;
--- This will remove the files with extension .out under /home dir .

hope this helps.
let Start to create peaceful and happy world
Tan Shirley
Frequent Advisor

Re: find and delete files at the same time ?

Hi Rajsri & Madanagopalan,

Thanks for your reply. I've tried both commands but failed.
I've got the following message.

# find /home *.out -exec rm {} ;
find: -exec not terminated with ';'

# find /home -name '*.out' -print -exec rm -f {} ;
find: -exec not terminated with ';'

Please advise what could have gone wrong.
Thanks !

federico_3
Honored Contributor

Re: find and delete files at the same time ?



The commands are rigth but you have to put the back slash before the semicolon at the end of instruction without white space.

federico
Maurice Peterse
Frequent Advisor

Re: find and delete files at the same time ?

hi,
this should do it:

find /home -name '*.out' -print -exec rm -f {} ";"

RikTytgat
Honored Contributor
Solution

Re: find and delete files at the same time ?

Hi,

Everybody is right, except for the backslash. Backslashes have to be escaped for them to appear correctly in these forums.

find /home -name '*.out' -print -exec rm -f {} \;

Bye,
Rik.
Tan Shirley
Frequent Advisor

Re: find and delete files at the same time ?

Hi Rik Tytgat,

Thanks a miilion !
10 points for you !

Thanks for the rest as well !
I've awarded points for you guys too !

Best Regards,
Shirley
augusto cossa
Frequent Advisor

Re: find and delete files at the same time ?

Hi,

To find certain file and then delete issue the command below:

find /home/dgm -name *.out -exec rm {} ;

Hope it's help you.

Augusto
Stefan Schulz
Honored Contributor

Re: find and delete files at the same time ?

I don't think you will have a timeproblem, but just to mention:

This find .. -exec rm construct works fine, but is pretty slow. If you have a awful lot of directories and files to scan and delete it is better to do the following.

Instead of -exec rm do a -print to a file. Then work thru the file and delete each entry. I did this in a script for "cleaning" the filesystem and reduced the time for finding and deleting by 50 %.

I dont think you will have to consider this, but just liked to tell you.

Regards

Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Mark Mitchell
Trusted Contributor

Re: find and delete files at the same time ?

this is a risky procedure, I would change it so that it moves files to a directory first before the delete or do it interactivly.
Carlos Fernandez Riera
Honored Contributor

Re: find and delete files at the same time ?


The correct command to do this is:

find /home/dgm -name "*.out" | xargs rm


see xargs man for explanations
unsupported
Stefan Schulz
Honored Contributor

Re: find and delete files at the same time ?

find ... | xargs only works if the output of the find command doesn't exceede the ARG_MAX and LINE_MAX parameters (see man xargs and man 5 limits).

As i said if you have a lot (not just some) files to delete the xargs command doesn't help you.

But again i don't think this is a problem here.
No Mouse found. System halted. Press Mousebutton to continue.
Carlos Fernandez Riera
Honored Contributor

Re: find and delete files at the same time ?


xargs command issue read args from stdin and issues commands ( as many commands as needed) to acomplish the task.

In our example must do:

rm /home/..../f1 /home/.../f2 .... /home/.../f33

rm /home/.../34 .....


In other words, instead of do 3300 rm commands , xargs issues just 33???
unsupported
Alan Riggs
Honored Contributor

Re: find and delete files at the same time ?

It is not correct that the xargs pipe will break if the number of aguments exceeds ARG_MAX or LINE_MAX. In fact, I often use xargs to handle situations in which base commands (ls, rm) fail because the argument list is too large.

In xargs, those limits simply control the size of the argument list that can be passed to each iteration of the command specified.