Page 1 of 1

empty folder shell command?

Posted: Thu Apr 05, 2007 2:19 pm
by potato
Hi,

i have to empty my backup folder of my server.
I know that i can use rm filename to delete a filename, but isn't there a way to delete a folders contents, but leaving the folder itself?

Any help would be great...

friendly greetings,
tom

Posted: Thu Apr 05, 2007 2:52 pm
by Benjamin

Code: Select all

rm -fr folder/*

Posted: Fri Apr 06, 2007 12:50 am
by timvw
astions wrote:

Code: Select all

rm -fr folder/*
Wouldn't that ignore hidden files and folders (the ones that have a name starting with . )?
If i'm not mistaken rm /path/to/delete/.* removes *all* the files... (probably want to recurse (-r).. and perhaps to force (-f)).

Posted: Fri Apr 06, 2007 6:14 am
by Benjamin
Yeah, doing that would leave hidden files.. I suppose you could do this..

Code: Select all

[benjamin@viper ~]$ mkdir test
[benjamin@viper ~]$ touch test/.hidden
[benjamin@viper ~]$ rm -fr test/.* & rm -fr test/*
rm: cannot remove `.' or `..'
rm: cannot remove `.' or `..'
[benjamin@viper ~]$ ls -a test/
.  ..
[benjamin@viper ~]$
The other command you mentioned would *only* delete hidden files.

It seems like there should be a better way though.

Posted: Fri Apr 27, 2007 3:11 am
by rebus

Code: Select all

find . -type f -exec rm -rf '{}' \;
This finds all files of type file in current directory and executes rm -rf FOUND_FILE_NAME for each found file ....

try this but be carefull not to delete more then you need

Posted: Fri Apr 27, 2007 5:59 am
by timvw
rebus wrote:

Code: Select all

find . -type f -exec rm -rf '{}' \;
This finds all files of type file in current directory and executes rm -rf FOUND_FILE_NAME for each found file ....

try this but be carefull not to delete more then you need
About being carefull, the first time you run the command you probably want to prepend 'echo' to exec option

Code: Select all

find . -type f -exec echo rm -rf {}\;

Posted: Fri Apr 27, 2007 6:20 am
by Jenk
I prefer to just do..

Code: Select all

rm -rf ./directory && mkdir ./directory

Posted: Fri Apr 27, 2007 8:52 am
by Kieran Huggins
Jenk makes it all look sooooo easy..... :-)