Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

remove all files in a directory... but not delete the directory

SOLVED
Go to solution
bob the spod
Occasional Advisor

remove all files in a directory... but not delete the directory

Is it possible to completely remove all the files in a directory without removing the actual directory (in effect emptying it)

if so do i have to put the whole path name in the command?

the directory in question is /home/bobspod/jeepers

how do i empty jeepers?
you make me feel like dancing (gonna dance the night away!)
8 REPLIES
Pete Randall
Outstanding Contributor
Solution

Re: remove all files in a directory... but not delete the directory

Couple of ways:

First,
cd /home/bobspod/jeepers
rm *

Second,
cd /home/bobspod/jeepers
find . -exec rm -rf {} \;

The first method will trip over nested subdirectories, the second will remove the subdirs as well.

Pete

Pete
Rodney Hills
Honored Contributor

Re: remove all files in a directory... but not delete the directory

How about

rm /home/bobspod/jeepers/*

or if their are thousands of files under that folder, then

find /home/bobspod/jeepers -print | xargs rm

Either way will leave the folder "jeepers" intact.

HTH

-- Rod Hills
There be dragons...
John Bolene
Honored Contributor

Re: remove all files in a directory... but not delete the directory

Interesting names.

only the rm -rf will remove files and directories

the rmdir only removes a directory if there are no files inside

you want the rm *
It is always a good day when you are launching rockets! http://tripolioklahoma.org, Mostly Missiles http://mostlymissiles.com
Jeff Schussele
Honored Contributor

Re: remove all files in a directory... but not delete the directory

Hi Bob,

Yes either

rm /home/bobspod/jeepers/*

OR

cd /home/bobspod/jeepers
rm *

When you just use * then any files starting with "." will not be removed. So you may need to follow this command with

rm /home/bobspod/jeepers/.*

Note - you'll get squawks about not being anle to remove "." or "..", but that's OK as these are current & parent dir specifications & rm can't remove them

If there is dir structure below this dir then add -r to the command.

You remove dirs with the
rmdir
command

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Helen French
Honored Contributor

Re: remove all files in a directory... but not delete the directory

# find /home/bobspod/jeepers -depth -exec rm -r {} \;
Life is a promise, fulfill it!
Rodney Hills
Honored Contributor

Re: remove all files in a directory... but not delete the directory

If I take in account that subdirectories may exist under "jeepers", then do the following

cd /home/bobspod/jeepers
find . -path "./*" -prune -print | xargs rm -R

This will execute find on the current directory, but not descend into subdirectories displaying the names of each file or subdirectory. That output is piped into the xargs command which will run "rm -R" on the file/subdirectory names. See man "xargs" for it's benefits.

HTH

-- Rod Hills
There be dragons...
Israel Oros
Occasional Contributor

Re: remove all files in a directory... but not delete the directory

find /home/bobspod/jeepers -type f -exec rm -f {} \;

This instruction will only delete files but not directories under /home/bobspod/jeepers.

If you want to get rid of everything under /home/bobspod/jeepers.

Then you only need to run something like:

rm -rf /home/bobspod/jeepers/* /home/bobspod/jeepers/.*

Regards,

Israel

John Dvorchak
Honored Contributor

Re: remove all files in a directory... but not delete the directory

Israel, you example removes the jeepers directory as well. I would be cautious of rm -rf .* in the future.
If it has wheels or a skirt, you can't afford it.