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
LVL 9
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Seth SimmonsSr. Systems AdministratorCommented:
find . -type f -exec rm -f {} \;

Open in new window

0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Thanks seth, but that deletes all files in the subfolders as well.
Can we limit to current folder?
0
Seth SimmonsSr. Systems AdministratorCommented:
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
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Gerwin Jansen, EE MVETopic Advisor Commented:
You can add -maxdepth like this:

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
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
0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
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...
0
Gerwin Jansen, EE MVETopic Advisor Commented:
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?
0
Gerwin Jansen, EE MVETopic Advisor Commented:
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.
0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
I am on Solaris.
This worked there.
0
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Admin,
if you can, please grant the points if it's allowable.
I just don't know how to handle this.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.