Deleting files based on Substring match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting files based on Substring match
# 1  
Old 04-17-2012
Deleting files based on Substring match

In folder there are files
(eg ABS_18APR2012_XYZ.csv
DSE_17APR2012_ABE.csv) .
My requirement is to delete all the files except today's timestamp

I tried doing this to list all the files not having today's date timestamp


Code:
#!/bin/ksh

DATE=`date +"%d%h%Y"`
DIR=/data/rfs/
FOLDER=Test
FILE=$DIR$FOLDER

UDATE="$(echo $DATE | tr '[a-z]' '[A-Z]')"

ls $FILE | grep -v "${UDATE}"

exit 0

This is listing all the files not having today's date timestamp. Now I have to delete the files which are listed .Kindly help me out how I can delete the files.

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by methyl; 04-18-2012 at 01:31 PM.. Reason: more code tags
# 2  
Old 04-17-2012
loose the quotes and ^ . Quotes wont allow your variable to expand, and ^ searches for file name starting with variable name, which in your case is at the end/middle. use following
Code:
ls $FILE | grep -v "$UPPDATE"

more precisely use:
Code:
ls $FILE | grep -v "$UPPDATE.csv"


Last edited by 47shailesh; 04-17-2012 at 02:34 PM.. Reason: variable typo correction and weak quote addition as suggested by Scrutinizer in post #4
This User Gave Thanks to 47shailesh For This Post:
# 3  
Old 04-17-2012
Better to use find if it has -mtime:

Code:
find . -name '*.csv' -mtime +1 -exec rm {} \;

That would delete .csv files older than 1 day.
This User Gave Thanks to neutronscott For This Post:
# 4  
Old 04-17-2012
Quote:
Originally Posted by 47shailesh
loose the quotes and ^ . Quotes wont allow your variable to expand, and ^ searches for file name starting with variable name, which is your case is at the end. use following
Code:
ls $FILE | grep -v $UPADTE

more precisely use:
Code:
ls $FILE | grep -v $UPADTE.csv

The proper advice would be to use weak quotes
Code:
ls "$FILE" | grep -v "$UPDATE.csv"

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-17-2012
In addition to the above posts, the script in post #1 contains obvious typing errors. The inconsistent spelling of all references to what probably should be $UPDATE .
This User Gave Thanks to methyl For This Post:
# 6  
Old 04-17-2012
thanks for pointing it out, made appropriate correction in post#2
This User Gave Thanks to 47shailesh For This Post:
# 7  
Old 04-18-2012
Hi neutronscott,
This will remove files according to modification time but in this scenario one can modify file at any moment. So this query will not give correct results

---------- Post updated at 12:42 AM ---------- Previous update was at 12:40 AM ----------

Hi methyl,

thanks for pointing it out the typing error.Made required change

---------- Post updated at 12:47 AM ---------- Previous update was at 12:42 AM ----------

Hi 47shailesh,

$UPPDATE is a substring having todays date pattern as filename would be somewhat like this
(eg ABS_18APR2012_XYZ.csv
DSE_17APR2012_ABE.csv)
so what I am trying to do is removing all the files other than todays date pettern

but this
Code:
ls $FILE | grep -v "$UPPDATE"

will list the files who is having todays date pattern

Last edited by Scrutinizer; 04-18-2012 at 03:03 AM.. Reason: code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

New files based off match or no match

Trying to match $2 in original_targets with $2 of new_targets . If the two numbers match exactly then a match.txt file is outputted using the information in the new_targets in the beginning 4 fields $1, $2, $3, $4 and value of $4 in the original_targets . If there is "No Match" then a no... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

4. UNIX for Dummies Questions & Answers

deleting files based on the suffix

how do we delete files based on the suffix??? (1 Reply)
Discussion started by: saggiboy10
1 Replies

5. Shell Programming and Scripting

Deleting files based on their size

I have several files in a folder and I would like to delete the ones that do not contain all the required information (size) let say 1kb. Any ideas? (4 Replies)
Discussion started by: Xterra
4 Replies

6. Shell Programming and Scripting

Deleting the files based on the date's

I have to write one script which will delete the files in the below passion. If today is 17-Feb-2010 then the script delete only 17-JAN-2010 files from the directory. Could you please help me, How will I delete the files when the year is leap year, if today is 30th Mar 2010 then how will... (1 Reply)
Discussion started by: kandi.reddy
1 Replies

7. UNIX for Dummies Questions & Answers

deleting lines from a delimited files based on a 2nd file

I need a little help... I have a file with 3 fields, Time/Date, IP address, UniqueID I have a 2nd file of UniqueIDs. I want to either (which ever is easier): 1. delete entries in file 1 that have a UniqueID in file 2 2. create a new file with the fields from File 1, excluding the... (4 Replies)
Discussion started by: goldie363
4 Replies

8. UNIX for Dummies Questions & Answers

deleting files based on file name and modified time

Hi, I have some log files created in the following fashion Ex: file name modified date 1) s.log1 01-jan-08 2) s.log2 02-jan-08 3) s.log3 03-jan-08 4) s.log4 04-jan-08 Now I want to have the latest 2 logs and delete the others. Can you tell me the one liner /... (1 Reply)
Discussion started by: ammu
1 Replies

9. Shell Programming and Scripting

command for deleting log files based on some condition

Hello, Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days. Any help will be highly appreciated. Thanks, Pulkit (4 Replies)
Discussion started by: pulkit
4 Replies

10. Shell Programming and Scripting

Traversing thru dirs and deleting files based on date

Hi Folks I am pretty new to unix and shellscripting. I need help on writing logic on traversing recursively through a set of directories under a top-level folder and delete files(mostly text) which are 1 month old. Can you people help me on this? Thanks a lot Ravi (5 Replies)
Discussion started by: ravi2082
5 Replies
Login or Register to Ask a Question