Operating System - HP-UX
1833797 Members
3087 Online
110063 Solutions
New Discussion

Re: Remove only files recursively, to preserve directory

 
SOLVED
Go to solution
Abe Li Kito
Occasional Contributor

Remove only files recursively, to preserve directory

Hi,
Being a newbie in UNIX, I'm wondering what's the easiest way to remove all files recursively in a filesystem but preserve the directories structure. I'm looking at ~800GB of data.

My customer has this weird request to remove all the data, but need the directory structure to remain intact.

Appreciate all the help. Thanks.
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: Remove only files recursively, to preserve directory

Just use find(1):
$ find root-of-path-to-remove -type f -exec rm -f +

You can pass multiple paths if you need.
SANTOSH S. MHASKAR
Trusted Contributor

Re: Remove only files recursively, to preserve directory

Hi,

Dennis' solution can be modified as below

$ find root-of-path-to-remove ! -type d -exec rm -f {} +

because as per Dennis' solution it will remove
only files of type file (will not remove directories but will keep device files, named pipes etc.)


-Santosh
Peter Nikitka
Honored Contributor

Re: Remove only files recursively, to preserve directory

Hi,

having many files in the directories, it's better to use 'xargs', so no new process needs to be started for the remeval of every single file:

find dir ! -type f | xargs rm

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
TwoProc
Honored Contributor
Solution

Re: Remove only files recursively, to preserve directory

re: Peters' posting I agree with the theory, but I think there is a typo (the ! makes it not remove files, but everything else).

He's right though, if that 800G is composed of lots and lots of files, then xargs would be a much faster solution.

find -type f | xargs -i rm -f {}

Or better yet, all but directories (which is what you asked for).

find ! -type d | xargs -i rm -f {}
We are the people our parents warned us about --Jimmy Buffett
James R. Ferguson
Acclaimed Contributor

Re: Remove only files recursively, to preserve directory

Hi:

As for the use of 'xargs' instead of '-exec' as an argument to 'find', notice that Dennis used the newer "+" termination in lieu of the older ";".

Using '-exec cnd \+' causes bundling (aggregation) of arguments like 'xargs' does; vastly increasing performance. The problem with '-exec cmd \;' is that a new process is spawned for every entity found.

The "+" terminator must be escaped as "+" is special to the shell just like ";". This works on 11.11 and above.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Remove only files recursively, to preserve directory

>JRF: The "+" terminator must be escaped as "+" is special to the shell just like ";".

"+" is not special in any "real" shell that I know of. (Though the man page indicates what you said.) At one time "+" was special for context sensitive files for diskless, but that's gone.
Arturo Galbiati
Esteemed Contributor

Re: Remove only files recursively, to preserve directory

Hi,
find dir ! -type d | xargs rm

Rgds,
Art