Shell script question - adding selective removal

Anyone out there familiar with shell scripting?

I have a cleanup routine that simply wipes an entire directory. I am not at all familiar with shell scripts... so hopefully someone can assist.

What I want to do is change this script so that instead of wiping the folder, it will do the following:
- Wipe all content except files ending with .mclog
- Nice to have: keep only the last 10 days of files ending with .mclog.

This is the cleanup script:
#! /bin/bash
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR} && rm --one-file-system -fr *

Open in new window


Thank you
LVL 4
S ConnellyTechnical WriterAsked:
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.

simon3270Commented:
To stop deleting .mclog files
#! /bin/bash
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR} && find . ! -name '*.mclog' | xargs rm --one-file-system -fr

Open in new window

0
simon3270Commented:
To keep only the most recent 10 mclog files
rm $(find . -type f | xargs ls -t | tail +11)

Open in new window

0
simon3270Commented:
Just noticed the -fr on your rm - we don't want that otherwise it will delete directories containing mclog files, even though the files have been left out of the list.  So, combining the above
#! /bin/bash
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR} || exit 1
find . -type f ! -name '*.mclog' | xargs rm --one-file-system -f
rm $(find . -type f | xargs ls -t | tail +11)
find . -type d -depth -exec rmdir '{}' \;

Open in new window

I've changed the "cd" to exit if it fails (so that we only have to do it once). The next line removes any files that aren't called *.mclog.  The next one lists the files in date order (most recent first) and deletes all from the 11th onwards, while the next goes through all of the subdirectories and tries to remove them - if they are empty, they will be removed, if not they will be left alone.

I've assumed that there is only one mclog file per day.  if they may be more or less frequent, you can keep all of them from the last 10 days with
find . -type f -name '*.mclog' -mtime +10 | xargs 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

S ConnellyTechnical WriterAuthor Commented:
This is absolutely awesome!! Thank you. :)
0
S ConnellyTechnical WriterAuthor Commented:
Can you recommend any good shell script training (free or pay)?

I have a programming background so I should be able to grasp this without too much difficulty.

Thank you again!
0
S ConnellyTechnical WriterAuthor Commented:
Yes, I like the idea of keeping the last 10 days of .mclog files intact.

I think I am supposed to replace:
rm $(find . -type f | xargs ls -t | tail +11)

Open in new window

WITH
find . -type f -name '*.mclog' -mtime +10 | xargs rm -f

Open in new window


The final version should be:
#! /bin/bash
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR} || exit 1
find . -type f ! -name '*.mclog' | xargs rm --one-file-system -f
find . -type f -name '*.mclog' -mtime +10 | xargs rm -f
find . -type d -depth -exec rmdir '{}' \;

Open in new window


Is that correct?
0
S ConnellyTechnical WriterAuthor Commented:
Awesome! Thank you!
0
S ConnellyTechnical WriterAuthor Commented:
Hello Simon,

Could you please confirm my question at 2014-08-05 at 14:59:58? Thank you.
0
simon3270Commented:
Yes, your final version looks good! The "find" was supposed to replace the"rm".
0
S ConnellyTechnical WriterAuthor Commented:
Hello Simon,
:(
The script won't work but it is my fault. I didn't realize that SCRIPT_DIR changes for each run.

I would like to revisit the original script...
#! /bin/bash
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR} && rm --one-file-system -fr *

Open in new window


But before removing the folders, copy the .mclog files to another folder, named c:/temp/logs

Thanks again.
0
simon3270Commented:
That's almost a new question  :-)

Anyway, to move the mclog files, you could, once you have CDed to the right directory, use

    find . -name '*.mclog' -exec mv '{}' c:/temp/logs \;
0
simon3270Commented:
BTW, I can't think of any good online or physical scripting guides - I've just picked it up over 30 years!
0
S ConnellyTechnical WriterAuthor Commented:
Hi Simon, I'll post this as a new q when I return from vacation...that way, crediting you for new q. Ty. :)

So, in other words...
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR}
[insert your line from above]
rm --one-file-system -fr *

Sorry, I can't seem to copy/paste from my phone.

Best regards.




So
0
simon3270Commented:
Yes, you were right.  You could use:
#! /bin/bash
SCRIPT_DIR=$(dirname $(readlink -e $0))
cd ${SCRIPT_DIR} || exit 1
find . -name '*.mclog' -exec mv '{}' c:/temp/logs \;
rm --one-file-system -fr *

Open in new window

No need for another question - just happy to answer this one!
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.

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.