LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2006, 11:12 AM   #1
chefsride
LQ Newbie
 
Registered: Nov 2006
Posts: 2

Rep: Reputation: 0
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.
 
Old 11-06-2006, 12:45 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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!
 
Old 11-06-2006, 02:18 PM   #3
chefsride
LQ Newbie
 
Registered: Nov 2006
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 11-06-2006, 02:52 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
new to scripting mifan Linux - Newbie 2 08-17-2005 01:10 PM
scripting help Abe_the_Man Linux - General 1 11-03-2004 06:30 PM
Scripting help JediMasterTux Linux - Newbie 2 07-13-2004 02:29 AM
scripting help versaulis Linux - Software 8 11-22-2003 08:08 PM


All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration