• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

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
0
Shawn Connelly
Asked:
Shawn Connelly
  • 7
  • 7
1 Solution
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Shawn ConnellyTechnical WriterAuthor Commented:
This is absolutely awesome!! Thank you. :)
0
 
Shawn 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
 
Shawn 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
 
Shawn ConnellyTechnical WriterAuthor Commented:
Awesome! Thank you!
0
 
Shawn 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
 
Shawn 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
 
Shawn 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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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