Operating System - HP-UX
1823921 Members
3062 Online
109667 Solutions
New Discussion юеВ

delete all files but not directories

 
SOLVED
Go to solution
Stefan Farrelly
Honored Contributor

delete all files but not directories


How can I delete all files in a directory but not touch any subdirectories or any files in any subdirectories using the find command ?
Im from Palmerston North, New Zealand, but somehow ended up in London...
7 REPLIES 7
Frederic Sevestre
Honored Contributor

Re: delete all files but not directories

Hi,

you can try :

#cd
# find . -type f -exec rm {} \;

It should work find.

Fr??d??ric
Crime doesn't pay...does that mean that my job is a crime ?
Stefan Farrelly
Honored Contributor

Re: delete all files but not directories


Hi Frederic,

thanks, but that command will go down and remove files in subdirectories also. I only want to touch the files in the dir im in.
Im from Palmerston North, New Zealand, but somehow ended up in London...
U.SivaKumar_2
Honored Contributor

Re: delete all files but not directories

Hi,
Why use find command ?.
#cd /test/dir
#rm *
rm: testsubdir directory
Now all the files are deleted except sub-directories.

regards,
U.SivaKumar

Innovations are made when conventions are broken
Stefan Farrelly
Honored Contributor

Re: delete all files but not directories


Well now, the find command has tons of really useful options that rm doesnt!! like mtime, ctime, atime, size etc etc.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Edward Sedgemore
Trusted Contributor
Solution

Re: delete all files but not directories

To delete all files in a dir but DONT include subdirs;

find . \( -type d ! -name . -prune \) -o \( -type f -name "*" ! -name "." -print \) -exec rm {} \;
Frederic Sevestre
Honored Contributor

Re: delete all files but not directories

Hi,

Sorry I misunderstood your problem. Why not :

# rm *

Fr??d??ric
Crime doesn't pay...does that mean that my job is a crime ?
Stefan Farrelly
Honored Contributor

Re: delete all files but not directories


Thats what I wanted! Thanks Ed.
Im from Palmerston North, New Zealand, but somehow ended up in London...