LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting Help (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-help-499127/)

chefsride 11-06-2006 10:12 AM

Scripting Help
 
Hello all I am a newbie to Linux and I am trying to create a script that will delete files based on there date. I know that the ls -t command will give me the list of files I want. I want to delete all of the files except for the 5 latest. Any help would be greatly appreciated.

matthewg42 11-06-2006 11:45 AM

Try this command:

Code:

ls -t | \
  grep -v '^ *total' | \
  perl -n -e 'print if ($. > 5);'

If it prints the correct list of files, you can use this to remove them:

Code:

ls -t | \
  grep -v '^ *total' | \
  perl -n -e 'chomp; if ($. > 5) { unlink($_) || warn "could not delete $_: $!\n"; }'

Note that this won't delete directories - you will get a warning if you try to "unlink" one.

Be careful, there is no way to undelete files removed in this way (there may be some very hackish way to undelete depending on what filesystem you use, but success is not guaranteed).

If you want to be safer, here's a similar way which will move the files to the trash directory:

Code:

ls -t | \
  grep -v '^ *total' | \
  perl -n -e 'print if ($. > 5);' | \
  while read f; do mv "$f" ~/.Trash; done


Note that you should make sure ~/.Trash exists before starting this, or the first file will be renamed to be a file called ~/.Trash, and the other files will be moved over it one at a time... at the end of the process you would end up with the last file moved called ~/.Trash!

chefsride 11-06-2006 01:18 PM

Thank you very much for your help. Another question for you. I have a command I want to run and it is going to give me a list of information. I want to out of that list of information get a line that starts with Extent: and a Line that starts with Status: and determine based on what comes after those words do something. I know this is a little vague, but basically if after the word status: says full I want to do something with what comes after the work extent:. This can either come from a file or a command which ever would be easier.

matthewg42 11-06-2006 01:52 PM

There are several ways to do this. The one I would choose would be to use perl, but you could use awk or do it from the shell too if you like. OK, here's my perl method (save this to a file, run "chmod 755 filename" to make the file executable, and run it with ./filename (the . means "from the current directory", so if you are not in that directory use /the/full/path/to/filename, or put it in a directory listed in the PATH environment variable).

I'm making up the exact text to search for, and what to do when it is found, you'll get the idea...
Code:

#!/usr/bin/perl -w

while(<>) {
    if    ( /^Extent: full/ ) {
        system("echo 'HELP - Extent is full' | mailx -s 'extent FULL' admin@foo.com");
    elsif ( /^Extent: warning/ ) {
        system("echo 'warning - extent is nearly full' | mailx -s 'extent warning' admin@foo.com");
    }
}

This command would be run with the text you want to check as the input, for example:
Code:

./filename file.log
...would check the contents of "file.log".


All times are GMT -5. The time now is 07:46 AM.