LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-24-2013, 05:47 AM   #1
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Rep: Reputation: Disabled
Exclamation How to use regular expressions with find or with rm command ?


Hello Everybody,

I have a few files that have a ~ (tilde) at the end.
e.g. Document.txt~, output.log~ etc, output.o~.
My objective : Delete such files using regular expressions.
I can delete them using rm *.???~ but want to use the $ metacharacter to match the last character. I mean to say, i want to use $ to match the ~, which is the last character.
I first tried rm command :
$ rm "~$" but it failed.
I then tried the find command :
$ find . -name "~$" -exec rm {} /; but it too failed.

Can someone how can i use the $ metacharacter in the rm or find command to delete the files ?
 
Old 10-24-2013, 05:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
$ is not understood that way using find or rm, therefore it must fail. $ means end of line (end of string) when you use regexp, but rm and find does not use that kind of regexp. rm '*~' or find . -name '\*~' means exactly the same.
 
Old 10-24-2013, 06:08 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"find" accepts regex, and will honour "~$" (properly constructed). See the manpage.

Last edited by syg00; 10-24-2013 at 06:09 AM.
 
Old 10-24-2013, 06:17 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by syg00 View Post
"find" accepts regex, and will honour "~$" (properly constructed). See the manpage.
oh yes, find -regex accepts regexp, find -name does not. -name accepts only shell patterns.
 
Old 10-24-2013, 07:49 AM   #5
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Thanks everybody, i will check the options and post the results..
Thanks again
 
Old 10-24-2013, 11:46 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
glad to help you
if you really want to say thanks just press yes
 
Old 10-25-2013, 02:17 AM   #7
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
@pan64 : I tried the -regextype and -regex but could not get any results.
Here is the log :
[san@localhost test_dir]$ ls -l | grep "~$"
-rw-rw-r-- 1 san san 3000 Oct 22 23:36 man_kill.txt~
-rw-rw-r-- 1 san san 0 Oct 24 23:30 testfile.o~
[san@localhost test_dir]$ find . -regextype grep
.
./testfile.log
./man_kill.txt~
./testfile.o~
[san@localhost test_dir]$ find . -regextype grep -regex '~$'
[san@localhost test_dir]$
[san@localhost test_dir]$ find . -regextype grep -regex '~$' -print
[san@localhost test_dir]$


Can anyone plz say where i am going wrong ?? I want find to list all the files ending with ~ (tilde) as the ls -l | grep "~$" does above.
 
Old 10-25-2013, 02:40 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,790

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
find . -regextype grep -regex '.*~$'
probably works, looks like ^ at the beginning and $ at the end are implied (therefore -regex '.*~' is enough)
 
1 members found this post helpful.
Old 10-26-2013, 06:53 AM   #9
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
@pan64, thanks... it works. One thing i would like to know is that when i am using
find . -regextype grep -regex '~$' (without .*) i am not getting the results. The ~$ is used by grep to match the last character. I also specify the regextype as grep in the find command. But without the .*, the ~$ does not work.
I want to know how to construct the regular expressions with find command in each case when the regextype is
1. emacs
2. posix-awk
3. posix-basic
4. posix-egrep
5. posix-extended ?
Could you plz lead me to some source from where i could learn more on the use of the regular expressions with the find command ? I have gone thru the man but honestly, it gives only basic information abt the find options.

Last edited by techie_san778; 10-26-2013 at 06:58 AM.
 
Old 10-26-2013, 08:14 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by techie_san778 View Post
One thing i would like to know is that when i am using
find . -regextype grep -regex '~$' (without .*) i am not getting the results. The ~$ is used by grep to match the last character. I also specify the regextype as grep in the find command. But without the .*, the ~$ does not work.
The find manpage is pretty clear on that:
-regex pattern
File name matches regular expression pattern. This is a match on the
whole path, not a search. For example, to match a file named ‘./fubar3’,
you can use the regular expression ‘.*bar.’ or ‘.*b.*3’, but not
‘f.*r3’.
The pattern needs to match the whole path, including the command line argument under which the file was found. The anchoring of "^" at the beginning and "$" at the end is implied, so your explicit "$" at the end is redundant.

Unless this is just an exercise in regular expressions, I don't understand why you are not simply using
Code:
find . -name '*~' -print -delete
The "-print" is optional -- you can leave that out if you want it to work silently. Try it without the "-delete" so you can see that it does find what you want.

Last edited by rknichols; 10-26-2013 at 08:24 AM.
 
Old 10-28-2013, 07:10 AM   #11
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
How about finding files in the grep way ?
grep : ls -l | grep "^-"
I know there is much a straightforward way :
$ find -type f -name, but just for the sake of having more experience with regular exps.
i want to have all the files in the current directory to be listed by find.
 
  


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
find and regular expressions rs232 Programming 4 08-11-2012 02:51 AM
[SOLVED] Using Regular Expressions With Find Crowey Programming 4 03-18-2010 01:33 AM
[SOLVED] Why this grep command with regular expressions not working on my system? Andrew Dufresne Linux - Newbie 12 10-01-2009 02:38 PM
How do I make a command accept regular expressions AwesomeMachine Linux - Newbie 3 06-01-2007 09:09 AM
Using Regular Expressions to find unwanted characters professorfrink Programming 1 10-07-2003 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:19 AM.

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