HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
Showing results for 
Search instead for 
Did you mean: 

Delete directory and files

Go to solution
Frequent Advisor

Delete directory and files


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

The directories are under /home.


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.

James R. Ferguson
Acclaimed Contributor

Re: Delete directory and files


# 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
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.


Esteemed Contributor

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 !!!
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
ssh ${HOST} -n rm -rf '/home/abc_123.????????'
done < file_of_hosts

Suraj K Sankari
Honored Contributor

Re: Delete directory and files


#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