Link to home
Start Free TrialLog in
Avatar of Evan Cutler
Evan CutlerFlag for United States of America

asked on

how to delete files but preserve subfolders in unix

greetings,
I have a master folder.
In the master folder, I have files and subfolders.
I want to delete the files but retain the subfolders.

rm -f * attempts to remove the  subfolders and throws errors.

Thanks
Avatar of Seth Simmons
Seth Simmons
Flag of United States of America image

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

Open in new window

Avatar of Evan Cutler

ASKER

Thanks seth, but that deletes all files in the subfolders as well.
Can we limit to current folder?
then the command you have will do what you want so not sure what the issue is

that command without -R will not be recursive; it will see the folder, knows it can't remove it because it's a folder and -R wasn't specified and reports an error which is expected behaviour
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you so much guys.
I used an ls command to find the files and then removed them.

ls |grep -v ^d|xargs rm -f
I've requested that this question be closed as follows:

Accepted answer: 0 points for arcee123's comment #a39534364

for the following reason:

This solution was more stable in my environment using SOLARIS.

I thank you guys very much...
Your ls command does not work because ls shows you names of files and folders. The grep removes both files and folders from the list that start with a d

I think you would mean:

ls -l | grep -v ^d | xargs rm -f

but that would give you output containing security files, size etc. so the rm would fail (or at least give a whole bunch of errors).

Did you check my last comment? It works, why no accept that?
I object to the way this question is closed, the solution does not work on Linux. Closing comment is now mentioning SOLARIS which was never in the original question.
I am on Solaris.
This worked there.
Admin,
if you can, please grant the points if it's allowable.
I just don't know how to handle this.
Thanks