Find the answer to your Linux question:
Results 1 to 6 of 6
I have a duplicate bunch of of photos in various subdirectories where the duplicates have had an '_2' added to the file name ... thus all duplicates end in *_2.jpg. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    2

    Find cmd can't find "*_2.jpg" files


    I have a duplicate bunch of of photos in various subdirectories where the duplicates have had an '_2' added to the file name ... thus all duplicates end in *_2.jpg.

    But I cant get the find command to filter these (The aim of game being to -exec rm those buggers.) (I'm using Ubuntu intrepid with bash shell.)

    cmd> find . -name "*_2.jpg" # doesn't return anything - zilch

    cmd> find . -name "*2.jpg" # returns all those files ending *2.jpg such as '140_4082.jpg' apart from those where the 2 is prefixed with an underscore such as '140_4082_2.jpg'

    cmd> find . -name "*_2*" # returns all files such as 'img_2210.jpg' which I don't want to rm but does include those prefixed _2.jpg such as '140_4085_2.jpg' which I do want.

    So I can have '.jpg' Or I can have '_' in the expression but it seems I just can't have both at the same time?

    Hmmmm - any guru's of find out there - John

  2. #2
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    117
    Can't think of why this problem would be occurring, as described. It works on my machine...

    I don't know how familiar you are with Unix, and I don't want to be insulting - but could this be a case-sensitivity issue? "*_2.jpg" won't match file_2.JPG (nor file_2.jpeg, but that's a different problem...) - you can get around this by using "find -iname" instead of "find -name"...

    Among other things, it appears that none of the example patterns you indicated actually included a *_2.jpg file in the results? That might lend extra credence to my theory that this is a capitalization issue...

    Alternately, maybe try 'find . -name "*_2.*' - note the dot after the 2... Or 'find . -name "*_2.???"' if you want to get only files that end in underscore-two-dot-(three-character extension)...

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    2
    Please - insult away - I deserve it. Yes - JPG not jpg. I thought it should work .. too tired to think straight.

    Thanks tetssujin - John

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,529
    You need to use single quotes around the file name with the wild-card, not double quotes. IE: find . -type f -name '*_2.jpg'
    When you use double quotes, the shell tries to expand it, resulting in incorrect, or in your case, no results. The single quotes "guard" the wild card so it is passed as-is to the find command, which uses it internally with its regular-expression evaluator.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    117
    Quote Originally Posted by Rubberman View Post
    You need to use single quotes around the file name with the wild-card, not double quotes. IE: find . -type f -name '*_2.jpg'
    When you use double quotes, the shell tries to expand it
    No it doesn't.

    Code:
    > mkdir test; cd test; touch a b c; ls
    a  b  c
    > echo *
    a b c
    > echo "*"
    *
    >
    Double-quotes expand variables and other substitution forms - they don't expand globs. (At least, this is the behavior of Bash on my machine with my configuration...)

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,529
    Quote Originally Posted by tetsujin View Post
    No it doesn't.

    Code:
    > mkdir test; cd test; touch a b c; ls
    a  b  c
    > echo *
    a b c
    > echo "*"
    *
    >
    Double-quotes expand variables and other substitution forms - they don't expand globs. (At least, this is the behavior of Bash on my machine with my configuration...)
    I stand corrected.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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