Results 1 to 5 of 5

Thread: Quick question about shell expansion

  1. #1
    Join Date
    Nov 2008
    Location
    Houston, Texas USA
    Posts
    50

    Default Quick question about shell expansion

    Hiya. I just have a quick question about something that I did that had an unexpected result.

    Something ridiculous happened and hundreds of files erroneously got dropped in my Documents folder, so I went about deleting. At one point, I wanted to delete all the files that began with 'l' except for a few that began with 'letter', so I did this:

    rm l[^etter]*

    Interestingly, that also left two files that started with 'letalis' that I'd wanted to delete. Obviously, there's something I don't know about shell expansion here. Why did it leave those two along with the ones I wanted left alone? How would I have made it where it would have deleted everything except those that started with 'letter'?

    By the way, I'm using BASH.

    Thanks.
    Monkey pants!!

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Quick question about shell expansion

    The construct [] denotes a character class, not a string as you imagine. What you were asking for was:

    l, then any letter that is not (because of the ^, ! would also work) e, t, or r (duplicates are ignored), then anything.

    Since the second letter of letalis is e, this did not match so was left out of the expansion.

  3. #3
    Join Date
    Nov 2008
    Location
    Houston, Texas USA
    Posts
    50

    Default Re: Quick question about shell expansion

    Oh geez. I actually thought I knew that. I think I need more sleep.

    So what would have accomplished what I was trying to accomplish?

    And thank you very much, by the way. That was a serious brain fart on my part.
    Monkey pants!!

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Quick question about shell expansion

    Either using a pipe to filter out filenames that match letter* or using the extended pattern matching operator !(pattern-list) described in the man page which I have never tried.

    Sometimes it's faster to reduce the namespace in an ad hoc way:

    rm l[!e]*

    and then deal with the few stragglers that begin with le but not letter.

  5. #5
    Join Date
    Nov 2008
    Location
    Houston, Texas USA
    Posts
    50

    Default Re: Quick question about shell expansion

    Thank you. It's the extended pattern matching that I was looking for.
    Monkey pants!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •