empty folder shell command?

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

empty folder shell command?

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

rm -fr folder/*
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)).
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
rebus
Forum Newbie
Posts: 11
Joined: Tue Nov 07, 2006 6:13 pm
Location: Croatia, Zadar

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 {}\;
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I prefer to just do..

Code: Select all

rm -rf ./directory && mkdir ./directory
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Jenk makes it all look sooooo easy..... :-)
Post Reply