LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   How to use regular expressions with find or with rm command ? (https://www.linuxquestions.org/questions/linux-server-73/how-to-use-regular-expressions-with-find-or-with-rm-command-4175481980/)

techie_san778 10-24-2013 05:47 AM

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 ?

pan64 10-24-2013 05:55 AM

$ 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.

syg00 10-24-2013 06:08 AM

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

pan64 10-24-2013 06:17 AM

Quote:

Originally Posted by syg00 (Post 5051399)
"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.

techie_san778 10-24-2013 07:49 AM

Thanks everybody, i will check the options and post the results..
Thanks again :)

pan64 10-24-2013 11:46 AM

glad to help you
if you really want to say thanks just press yes

techie_san778 10-25-2013 02:17 AM

@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.

pan64 10-25-2013 02:40 AM

find . -regextype grep -regex '.*~$'
probably works, looks like ^ at the beginning and $ at the end are implied (therefore -regex '.*~' is enough)

techie_san778 10-26-2013 06:53 AM

@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.

rknichols 10-26-2013 08:14 AM

Quote:

Originally Posted by techie_san778 (Post 5052644)
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.

techie_san778 10-28-2013 07:10 AM

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.


All times are GMT -5. The time now is 06:27 AM.