The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
delete line from file if successful partial string found cronjob78 UNIX for Advanced & Expert Users 7 05-14-2009 02:35 AM
cronjob: Partial script error sundar63 Shell Programming and Scripting 2 11-07-2008 02:57 AM
shell script to delete directories... Stephan Shell Programming and Scripting 8 05-18-2008 11:00 AM
shell script to delete directories... Stephan Shell Programming and Scripting 5 05-16-2008 08:11 AM
Need Help: Delete a file by Shell Script r3edi Shell Programming and Scripting 5 07-11-2005 07:13 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-02-2009
q8devilish q8devilish is offline
Registered User
  
 

Join Date: Jun 2009
Location: Kuwait
Posts: 16
shell script to partial delete

guys i need some help in writing a shell script, what i am trying to write is a shell script that can delete generated trace files my software usually generated everyday, i will give an example to make it easy to understand
Code:
root@cms-db # pwd
/pcard17/trace
root@cms-db # ls -l HSM_VERIF.TRC*
-rw-r--r--   1 pwrcard  dba      4194432 Jul  1 00:18 HSM_VERIF.TRC111111545
-rw-r--r--   1 pwrcard  dba      4194432 Jul  1 00:51 HSM_VERIF.TRC111111546
-rw-r--r--   1 pwrcard  dba      4195092 Jul  1 01:29 HSM_VERIF.TRC111111547
-rw-r--r--   1 pwrcard  dba      4194432 Jul  1 02:22 HSM_VERIF.TRC111111548
-rw-r--r--   1 pwrcard  dba      4195092 Jul  1 04:02 HSM_VERIF.TRC111111549
-rw-r--r--   1 pwrcard  dba      4194432 Jul  1 05:33 HSM_VERIF.TRC111111550
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 06:16 HSM_VERIF.TRC111111551
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 06:40 HSM_VERIF.TRC111111552
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 06:58 HSM_VERIF.TRC111111553
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 07:15 HSM_VERIF.TRC111111554
-rw-r--r--   1 pwrcard  dba      4195752 Jul  2 07:34 HSM_VERIF.TRC111111555
-rw-r--r--   1 pwrcard  dba      4195488 Jul  2 07:52 HSM_VERIF.TRC111111556
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 08:04 HSM_VERIF.TRC111111557
-rw-r--r--   1 pwrcard  dba      4195224 Jul  2 08:16 HSM_VERIF.TRC111111558
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 08:27 HSM_VERIF.TRC111111559
-rw-r--r--   1 pwrcard  dba      4195224 Jul  2 08:37 HSM_VERIF.TRC111111560
-rw-r--r--   1 pwrcard  dba      4195620 Jul  2 08:45 HSM_VERIF.TRC111111561
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 08:57 HSM_VERIF.TRC111111562
-rw-r--r--   1 pwrcard  dba      4194828 Jul  2 09:06 HSM_VERIF.TRC111111563
-rw-r--r--   1 pwrcard  dba      4195356 Jul  2 09:13 HSM_VERIF.TRC111111564
-rw-r--r--   1 pwrcard  dba      4194696 Jul  2 09:20 HSM_VERIF.TRC111111565
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 09:28 HSM_VERIF.TRC111111566
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 09:36 HSM_VERIF.TRC111111567
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 09:43 HSM_VERIF.TRC111111568
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 09:50 HSM_VERIF.TRC111111569
-rw-r--r--   1 pwrcard  dba      4195092 Jul  2 09:56 HSM_VERIF.TRC111111570
-rw-r--r--   1 pwrcard  dba      4194564 Jul  2 10:04 HSM_VERIF.TRC111111571
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 10:11 HSM_VERIF.TRC111111572
-rw-r--r--   1 pwrcard  dba      4195092 Jul  2 10:17 HSM_VERIF.TRC111111573
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 10:23 HSM_VERIF.TRC111111574
-rw-r--r--   1 pwrcard  dba      4194828 Jul  2 10:29 HSM_VERIF.TRC111111575
-rw-r--r--   1 pwrcard  dba      4194432 Jul  2 10:34 HSM_VERIF.TRC111111576
-rw-r--r--   1 pwrcard  dba       886908 Jul  2 10:36 HSM_VERIF.TRC111111577
what i am trying to do is crating a script that will delete only yesterday files and keep only latest 2 files for security reasons.

my attempt in creating was like that to delete all yesterday file
Code:
#!/bin/bash
clear
echo "Please Enter YESTERDAYS Date in this order Mmm dd"
echo -n "Enter Date:"
read otaibi
ls -l /pcard17/trace | grep "$otaibi" | grep HSM_VERIF.* | nawk '{print $9}' > /pcard17/trace/delete.txt
clear
cd /pcard17/trace
cat /pcard17/trace/delete.txt | while read line; do rm "$line" ; done
now how can i delete yesterday files and keep latest 2 files?
  #2 (permalink)  
Old 07-02-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,391
you can use find command to find and delete yesterdays files
Code:
find your/dir/path -type f -name "HSM_VERIF.*" -mtime +1 -exec rm {} \;
and to keep only latest two you mean latest two of yesterdays files??
  #3 (permalink)  
Old 07-06-2009
q8devilish q8devilish is offline
Registered User
  
 

Join Date: Jun 2009
Location: Kuwait
Posts: 16
yes i would like to keep latest two of yesterdays files.
  #4 (permalink)  
Old 07-06-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
Example day Jul 2
Code:
ls -alt HSM_VERIF.* | grep " Jul  2 " | sed -n '3,$p' |  xargs rm -f
Or
Code:
ls -alt HSM_VERIF.* | grep " Jul  2 " | sed -n '3,$p' | while read line
do
      rm -f "$line"
done
  #5 (permalink)  
Old 07-13-2009
q8devilish q8devilish is offline
Registered User
  
 

Join Date: Jun 2009
Location: Kuwait
Posts: 16
thanks for the help it works like a charm
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0