[SOLVED] Bash regex to remove files with 3 character followed by a space
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Bash regex to remove files with 3 character followed by a space
What would be the regular expression to allow bash commands (like rm or ls) to find files like: Sep 13, 17.00.00, Feb 14, 12.34.56, Mar 15, 09:12:16.
I have a program which logs to the /var/log/ directory. I want to remove the files from this directory. Currently, I'm using the command
Code:
rm *,*
but I'd like to know if there is a regex I can write which will search for 3 alphabetic characters, followed by a space followed by 2 digits and a comma.
Something like this
Code:
[A-Z][a-z]2[:space:][0-9]2,
The only problem is, that code doesn't work when used in conjunction with ls or rm
I'm sure this is simple, but I'm just curious as to how to accomplish this.
BTW, if this question is answered somewhere else, please just point me to that post.
Distribution: Debian ("jessie", "squeeze"), Linux Mint (Serena), XUbuntu
Posts: 221
Rep:
Better than *,*
I don't have a complete answer to your question, but I have something more specific than what you are using now, thus
safer for your rm command.
The code below makes two nonsense files with spaces in the file names after the first two characters, then
two more characters, then a comma and then more characters. It then ls's and finds the files.
(At this
point I refrain from making comments (beyond this one!) about what a pain it is to accommodate the mac and other ``friendly'' OS's
that stick spaces in file names when an underscore is so much less trouble!)
Code:
touch ab\ df,dsfds
touch xr\ df,dfew
richard@jagger:~/temp$ ls -l ??\ ??,*
-rw-r--r-- 1 richard richard 0 Sep 14 17:22 ab df,dsfds
-rw-r--r-- 1 richard richard 0 Sep 14 17:21 xr df,dfew
I do not know how to do the A-Z and 0-9 trick you were suggesting. I will read an answer like that with some interest.
The [A-Z] and [a-z] just means, well, A-Z. Normally I'd use it like: rm [A-Z]*.[0-9].bz2 to delete the backed up, bzipped log files starting with a capital letter.
The [:space:] is from PHP's POSIX regex library. According to some bash basics that I've read, [:space:] is supported, but I never could get it working.
I did not know about '?'. I've further refined your answer, to one that I feel best suits my needs:
Code:
ls ???\ [0-9][0-9],*
So, you solved my problem! Thanks a million, mate!
Also, this one isn't a friendly OS issue, it's more an issue with many of the new date libraries making the creation of human readable times very easy. People then just pass these strings off to the create file method or logger method and viola! You have a file with spaces. In some ways, I like it because looking at a timestamp and telling the date still ain't easy for me. But then you run into the issue like I just encountered. So, it's a catch 22.
Anyways, You helped me solve my problem! Thank you! I hope this helps other people too.
Wow. Thank you for taking the time to show me how to do this via a bash script!
I have one question, if you don't mind. Why is it, that I can't use something like
Code:
[0-9]{2}
on the command line, but I can use it in a bash script? That's been my problem all along, I'll try to write a script by first trying my hand at just using the command line, and when a particular line doesn't work, I assume that that line I wrote wasn't valid code and move onto the next thing.
I'll admit, both of your solutions baffle me. I never knew about
Code:
shopt
I'm glad you shared that secret.
For the second script, you mentioned, "true RegEx". I'm assuming that means POSIX regex, but if there's a difference, could you tell me (you don't have to answer me right away, but I'm very curious).
And BTW, all three of the solutions you both provided worked on Centos5.6 as well as Mac OS X y'all are amazing.
I have one question, if you don't mind. Why is it, that I can't use something like
Code:
[0-9]{2}
on the command line, but I can use it in a bash script?
Well, bash can only interpret RegEx via its '=~' operator. This operator can only be used inside the '[[' command.
Otherwise bash can only interpret globs aka wildcards. By activating
shopt -s extglob
It can also interpret extended globs aka extended pattern matching.
Quote:
I'm glad you shared that secret.
It's not really a secret
Try 'man bash' and search for 'shopt' to get an overview of supported options.
Quote:
For the second script, you mentioned, "true RegEx".
I simply meant RegEx. I wrote "true" RegEx to emphasize that the previous solution does not use a RegEx although there is a certain resemblance between RegEx and globs. Sorry for the confusion.
I simply meant RegEx. I wrote "true" RegEx to emphasize that the previous solution does not use a RegEx although there is a certain resemblance between RegEx and globs. Sorry for the confusion.
I hope this clears things up.
Yes, you cleared things up. Thanks again for all the help. And for taking the time to explain things to me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.