LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-18-2012, 02:48 AM   #1
manushi88
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 04-18-2012, 02:55 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you're happy with the output, you should be able to just pipe the output into rm via xargs:

./myscript.ksh | xargs rm -f

job done. Other methods also are possible, but from your starting point it should work fine. Personally I'd probably just use a single find statement and not need a script at all.
 
1 members found this post helpful.
Old 04-18-2012, 02:55 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You can pipe the output to rm using xargs:
Code:
ls $FILE | grep -v "${UDATE}" | xargs rm -i
The -i switch will make rm interactive (in case of errors/false hits, you can remove it when you are sure).

BTW: The line that changes DATE to upper case might(!) not be needed:
Code:
#!/bin/ksh

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

ls $FILE | grep -iv "${DATE}" | xargs rm -i

exit 0
The -i switch used with grep makes the search case insensitive.

Hope this helps.
 
1 members found this post helpful.
Old 04-18-2012, 02:56 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

Try piping to
xargs rm


edit: Toooooo slooooow :}


Cheers,
Tink
 
1 members found this post helpful.
Old 04-18-2012, 04:44 AM   #5
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb

You could use find to search for files older than todays file and delete them.


Code:
Test First --- to ensure these are the files you want to erase.

find /path/of/interest/ -type f -and ! -newermt 2012-04-18 -exec ls -l {} \;

Nuke em -->

find /path/of/interest/ -type f -and ! --newermt 20012-04-18 -exec rm -fv {} \;
 
1 members found this post helpful.
Old 04-19-2012, 05:49 AM   #6
manushi88
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
HI,

Thanks to all for your valuable and helpful suggestions
 
Old 04-19-2012, 05:51 AM   #7
manushi88
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jv2112 View Post
You could use find to search for files older than todays file and delete them.


Code:
Test First --- to ensure these are the files you want to erase.

find /path/of/interest/ -type f -and ! -newermt 2012-04-18 -exec ls -l {} \;

Nuke em -->

find /path/of/interest/ -type f -and ! --newermt 20012-04-18 -exec rm -fv {} \;
hi,
What -newermt option will do?
 
Old 04-19-2012, 06:59 AM   #8
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
It will search based on date listed . ! = not

So not newer than date xxxx delete.
 
Old 04-19-2012, 01:14 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Grepping the output of ls? Please, just say no.

If a simple find command won't do, I'd run the filenames in a loop and use the shell's built-in testing ability.

Code:
for file in *.csv; do

	[[ ! $file =~ $UDATE ]] && rm "$file"

done
If you want to make it more robust, you should load the list of filenames into an array first, and work on that. Avoid storing lists of things inside single scalar variables.

I'm not too familiar with ksh, but it should have built-in features for converting variable values to upper or lower case. A quick test in ksh shows that bash's ${var^^} pattern doesn't work, but setting "typeset -u var" first does, at least, which means there's likely some way to echo it at will as well.

Code:
$ typeset -u var=foo
$ echo "$var"
FOO

Also, $(..) is highly recommended over `..`.


Finally, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
 
  


Reply



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
[SOLVED] Select lines based on key match danielbmartin Programming 10 03-09-2012 11:03 AM
[SOLVED] file Renaming based on a pattern match Guyverix Programming 6 01-10-2011 04:40 PM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 11:31 AM
how to find an exact substring match? ldp Programming 7 02-22-2005 06:28 AM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 07:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:29 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
Open Source Consulting | Domain Registration