Doug Van
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:
Thank you
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 *
Thank you
To keep only the most recent 10 mclog files
rm $(find . -type f | xargs ls -t | tail +11)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is absolutely awesome!! Thank you. :)
ASKER
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!
I have a programming background so I should be able to grasp this without too much difficulty.
Thank you again!
ASKER
Yes, I like the idea of keeping the last 10 days of .mclog files intact.
I think I am supposed to replace:
The final version should be:
Is that correct?
I think I am supposed to replace:
rm $(find . -type f | xargs ls -t | tail +11)
WITHfind . -type f -name '*.mclog' -mtime +10 | xargs rm -f
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 '{}' \;
Is that correct?
ASKER
Awesome! Thank you!
ASKER
Hello Simon,
Could you please confirm my question at 2014-08-05 at 14:59:58? Thank you.
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".
ASKER
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...
But before removing the folders, copy the .mclog files to another folder, named c:/temp/logs
Thanks again.
:(
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 *
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 \;
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!
ASKER
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
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 *
No need for another question - just happy to answer this one!
Open in new window