cancel
Showing results for 
Search instead for 
Did you mean: 

Delete directory and files

SOLVED
Go to solution
rhansen
Frequent Advisor

Delete directory and files

Hello,

I need to delete a specific directory on a number of servers.

The directories are under /home.

/home/abc_123
/home/abc_123.09122009

The thing is I need to delete only the directory with the date in it i.e. /home/abc_123.09122009 and not the /home/abc_123.

Can someone please advise on how to do it.

Thanks.
6 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Delete directory and files

Hi:

# rm -rf /home/abc_123.09122009

...which will remove (recursively) the directory and all its contents.

Assuming that you have 'ssh' (or if you don't, use 'remsh' and do something like:

while read HOST X
do
ssh ${HOST} -n rm -rf /home/abc_123.09122009
done < file_of_hosts

...where the 'file_of_hosts" has the names or IP addresses of the servers to which you want to connect.

Substitute 'remsh ${HOST) -n ...' if necessary.

Regards!

...JRF...
Tingli
Esteemed Contributor
Solution

Re: Delete directory and files

Or, use

rm -f /home/abc_123.????????
F Verschuren
Esteemed Contributor

Re: Delete directory and files

When removing files always fist test it whit ls so you are realy sure you are removing not to match...

rm -r /home/abc_123.[0-9]*

the -f option will only be needed if the files are in used, looking ad your request is looks like that you do not want to remove files that are in used... so never use -f (unless you realy need it and you know what you are doing.
Julián Aimar
Frequent Advisor

Re: Delete directory and files

Hi, execute

# rm -r /home/abc_123.????200?

Buena suerte !!!
Matti_Kurkela
Honored Contributor

Re: Delete directory and files

If you wish to use wildcards when executing commands remotely, you must use backslashes or quotes to prevent the local shell from attempting to expand the wildcards.

You'll want the wildcard expression to be transferred to the remote host as-is, and then the remote host should expand the wildcards before actually executing the command.

Here's the suggestion of James, modified to delete directories with any date, not just .09122009.

while read HOST X
do
ssh ${HOST} -n rm -rf '/home/abc_123.????????'
done < file_of_hosts

MK
MK
Suraj K Sankari
Honored Contributor

Re: Delete directory and files

Hi,

#rm -Rf

is the command

If you satisfied with the answer and your problem got solved then please make a habit to assign points,
Those people who give there valuable time for your problem they should expect some apparition from you in terms of points.

To know how to assign points please go through the below link

http://forums13.itrc.hp.com/service/forums/helptips.do?admit=109447627+1256027208667+28353475#33

Thanks