Operating System - HP-UX
1832283 Members
2448 Online
110041 Solutions
New Discussion

Delete files while leaving directories

 
SOLVED
Go to solution
Greg Roberts
Advisor

Delete files while leaving directories

I need to delete all the files in a file tree while leaving the directories (like deleting all the files in /sbin on down leaving the directories, just example). Any easy way???
9 REPLIES 9
Rick Garland
Honored Contributor

Re: Delete files while leaving directories

The find command can be of use.

find / -type f -exec rm {} ;

You are telling find to start where you want, only type of files, and remove them.
f. halili
Trusted Contributor
Solution

Re: Delete files while leaving directories

You can put these line on a script:

find /tmp/reck -type f -exec ls -al {} ;
echo done

Which means recursively find on /tmp/reck all regular files and delete them and echo done...........enjoy - fnhalili
derekh
Maureen Gunkel
Trusted Contributor

Re: Delete files while leaving directories

Greg:
Rick's answer looks right, except he forgot the '' before the semi-colon. The command I use is:
find -depth -exec rm {} ; 2>/dev/null

the 2>/dev/null is so that I don't get the annoying '/usr/bin (just for example) is a directory'.

HTH
No matter where you go, there you are.
f. halili
Trusted Contributor

Re: Delete files while leaving directories

find /tmp/reck -type f -exec ls -al {} ;
echo done


Sorry.... don't forget the before the ;
derekh
Maureen Gunkel
Trusted Contributor

Re: Delete files while leaving directories

Greg-
Don't forget the backslash before the semi-colon - apparantly this page (or my browser) doesn't display it!
Mo
No matter where you go, there you are.
Rick Garland
Honored Contributor

Re: Delete files while leaving directories

Forgot, the "backslashes" do not always appear in these posts.

Put the backslash in after the curly braces and before the semicolon.
I always use a space between the close curly brace and the backslash character.
Greg Roberts
Advisor

Re: Delete files while leaving directories

What's up??? I got 5 replies and can't read any of them (or assign points). Tried on an other sun and a Wintell. All just show orginall message. Of course I won't be able to read this once sent??????
f. halili
Trusted Contributor

Re: Delete files while leaving directories

find /tmp/reck -type f -exec ls -al {} ;
echo done
=================
I forgot the before the semi-colon earlier
derekh
f. halili
Trusted Contributor

Re: Delete files while leaving directories

So..... I could not see the "backslash" that I type in my earlier responses before the "semi-colon". Please dont forget the backslash before the semicolon in the script for there is another line of command.
thanks
derekh