remove files from directory and subdirectory

Hello expert,

I have log files in one directory and its subdirectory. I'd like to write a simple script to remove them.

for example, I have a.log,b.log,c.log in /home/job/
/home/job/a.log
/home/job/b.log
/home/job/c.log

then I have
/home/job/sub/ge.log
/home/job/sub/fe.log

/home/job/sub1/eee.log
/home/job/sub2/ee.log

I know I can do
rm -f /home/job/*.log in job directory.
rm -f /home/job/sub1/*.log

how i remove files in dubdirectoy without listing all the sub?
nightworkerAsked:
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.

ravenplCommented:
rm -rfv /directory # removes the directory and all within it

find /directory -type f -name '*.log' -exec rm -fv {} \; # removes only those ehich match, but it's slow

find /directory -type f -name '*.log' | while read f; do echo "\"$f\""; done | xargs rm -fv # faster
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
biraCommented:
cd /home/job
for i in `find . -name *.log`
do
echo "removendo :" $i
rm $i
done
0
XoFCommented:
bira:
your solution is insufficient and dangerous under certain circumstances:
- depending on your shell the "for" command only accepts a finite number of arguments to loop over
- spaces in filenames cannot be handled

Cheers,

-XoF-
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

gheistCommented:
find . -type f  -name "[abceg]*.log" -exec rm -i '{}'\;
0
root_startCommented:
nightworker, a complete solution preventing from errors is the following:
======================================================================
find <dir> -type f -name ''[regular expression]*.log" 2>> /dev/null | while read file
do
   rm -f ${file} 2>> /dev/null
   if [ ${?} -ne 0 ]
   then
       echo "**ERROR: Failed to remove file: [${file}]." >> ./rm_error.log
   fi
done
======================================================================

Above I used the following comment: "[regular expression]", if you have standards in the file name and I also redirected an error message to a file: "rm_error.log" if the "rm" fails.

But if you don't want to have "control" about the execution, you can use the following:
======================================================================
find <dir> -type f -name ''[regular expression]*.log" 2>> /dev/null | xargs rm -f
======================================================================
It is really fast, but there is a "problem" with the command above, if the command fails to remove a file, it will stop executing. This is a "problem" with xargs.

I hope it helps.
0
ravenplCommented:
> complete solution preventing from errors
Hmm, filenames with whitespaces do not work... Causes error, possibly security flow.
Have You tried it Yourself?
0
root_startCommented:
Ops, I forgot one thing... The ravenpl is correct and the correct one is below:

======================================================================
find <dir> -type f -name ''[regular expression]*.log" 2>> /dev/null | while read file
do
   rm -f "${file}" 2>> /dev/null
   if [ ${?} -ne 0 ]
   then
       echo "**ERROR: Failed to remove file: [${file}]." >> ./rm_error.log
   fi
done
======================================================================

Now it is ok.

Sorry.
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
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.