Deleting files based on Substring match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting files based on Substring match
# 8  
Old 04-18-2012
ls $FILE | grep -v "$UPPDATE" will list files other than todays date check -v option of grep.
Other alternative to delete such files is
Code:
find $FILE -maxdepth 1 ! -name \*"$UPPDATE"* -exec rm {} \;


Last edited by 47shailesh; 04-18-2012 at 02:54 PM.. Reason: changed . to $FILE to look in a that directory
This User Gave Thanks to 47shailesh For This Post:
# 9  
Old 04-18-2012
Quote:
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.
Code:
#!/bin/ksh

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

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

ls "${TARGET}" 2>/dev/null | grep -v "${UDATE}" | while read FILENAME
do
         # Remove echo when tested
         echo rm "${FILENAME}"
done
exit 0


Last edited by methyl; 04-18-2012 at 01:45 PM.. Reason: make variable same case as rest of script. change name of $FILE to $TARGET (it isn't a file). allow for no files
This User Gave Thanks to methyl For This Post:
# 10  
Old 04-19-2012
HI,

Thanks to all for your valuable and helpful suggestions
 
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