Link to home
Start Free TrialLog in
Avatar of Doug Van
Doug VanFlag for Canada

asked on

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
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of Doug Van

ASKER

This is absolutely awesome!! Thank you. :)
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!
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?
Awesome! Thank you!
Hello Simon,

Could you please confirm my question at 2014-08-05 at 14:59:58? Thank you.
Yes, your final version looks good! The "find" was supposed to replace the"rm".
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.
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 \;
BTW, I can't think of any good online or physical scripting guides - I've just picked it up over 30 years!
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
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!