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.
|
"find" accepts regex, and will honour "~$" (properly constructed). See the manpage.
|
Quote:
|
Thanks everybody, i will check the options and post the results..
Thanks again :) |
glad to help you
if you really want to say thanks just press yes |
@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. |
find . -regextype grep -regex '.*~$'
probably works, looks like ^ at the beginning and $ at the end are implied (therefore -regex '.*~' is enough) |
@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. |
Quote:
-regex patternThe 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.File name matches regular expression pattern. This is a match on the Unless this is just an exercise in regular expressions, I don't understand why you are not simply using Code:
find . -name '*~' -print -delete |
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. |