[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

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?
0
nightworker
Asked:
nightworker
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now