3

I have a bash script that uses inotifywait to copy files that are created in a directory.

But it is copying vim temporary files that I don't want.

E.g. if I create foo.txt and edit it in vim, I end up with some/all of these:

foo.txt~
.foo.txt.swp
.foo.txt.swx
4913

I'm trying to write regex to match those and this is what I have so far:

'^\*\.sw??$'

I was hoping that would at least match the swp and swx files but it doesn't.

2 Answers 2

2
^.+?\.sw.?$

Explanation:

^ Start of string
.+? - Matches any character 1 or more (non greedy)
\. - matches literal .
sw.? - Matches sw followed by 1 character
$ - End of string
5
  • Why not use asterisk instead of ".+?" ? Or ".*"? Nov 17, 2012 at 18:32
  • @ButtleButkus Would mean the same thing here, but that would also match files like .swp. That is some hidden file you probably don't want to match. So I changed it. Nov 17, 2012 at 18:38
  • Makes sense but for some reason your regex didn't work and juergen's did. Except I also need to match "4913" also. Nov 17, 2012 at 18:42
  • 1
    @ButtleButkus Just noticed, I made a mistake there. It was sw.? Changed regex now. If you want to match 4913 as well, ^(.+\.sw.?|4913)$ Nov 17, 2012 at 18:47
  • @AnirudhRamanathan what does the question marks do in ^.+?\.sw.?$? is it to make it match foo.swp and bar.sw Feb 6, 2015 at 10:40
1

try this regular expression

^.+\.sw[px]$
1
  • Works great but what about "4913"? I tried this but it doesn't work: ^.+\.sw?[px]$|^4913$ Nov 17, 2012 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.