How to use regular expressions with find or with rm command ?
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
$ 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.
@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, 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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.