[SOLVED] Need help with wildcards to pick correct file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Hi guys. Wildcards always give me troubles, so I'm hoping someone can help me. Below is a directory listing, and the current wildcard pattern I was using. Only problem with this is that is doesn't account for the _0_ or the _K1_ part that I just realized I need to account for. and with that part, a K1 would be the one I want to find over the 0, a K2 over a K1 and 0, and K3 over a K2, K1, 0, etc etc, that I would want to find and copy to another folder. Right now I just have a cp with that wildcard below, but realize i now need to account for this other part and I have no idea how in this .sh script I have that was just some simple cp's with wildcard patterns. Any ideas?
-rw-rw-rw- 1 user group 7218255 Jul 25 23:32 Junk_D_2_07-26-2013_0_Stuff_MoreJunk.pdf
-rw-rw-rw- 1 user group 7650126 Jul 26 00:01 Junk_D_2_07-26-2013_K1_Stuff_MoreJunk.pdf
Hi druuna. Yeah, you have it right. The highest numbered K file or the 0 file if no K's exist (while still matching the other stuff in my ll) is the one I need to yank out of that folder.
That was my fear that I can't do this with cp and wildcards alone. Putting simple commands like cp and mv in a .sh are about the extent of my scripting skills haha.
Higher K should in theory always be the newest, but in the system generating those files it's user input so someone could get them out of order I suppose, but the timestamp would probably be ok to key off of, as I truly do want the last version (0 or K#) of that file.
- 1: the ls -tr ... | tail -1 looks for files that match your wildcard, newest one last. the tail -1 makes sure that only the last one is shown. The $( .... ) construct makes sure that this is done before the cp command executes.
- 2: the output of the first part is used in the cp command. Do _NOT_ forget the double quotes. They are there to make sure that cp can handle possible special characters (a space in the filename being one of those).
If that isn't accurate enough, you can try using bash/ksh's extended globbing patterns. Or if you need something in the middle of the list, run a simple for loop over the array and match the filename you want with a case construct or something.
OHHH, back-ticks, to the left of the 1 on the keyboard. I think I've only ever used that key for ~ so totally forgot about those haha. It worked with the back-ticks!!
Thanks David. I understand the general idea of an array, and I have seen where people put ranges of stuff in square brackets before too, not really knowing how that worked. I'll look at your examples though and try to figure out why those would work too, as I'd like to learn about that way as well.