1

I have a number of image files that have duplicates across case (ie file.jpg File.jpg). I need a script like the following except I want to completely remove all uppercase. I know that fslint can do this, but I want to do it in terminal because there are so many..

find . -maxdepth 1 -print0 | sort -z | uniq -diz

case-insensitive search of duplicate file-names

dras
  • 13
  • 3
  • 2
    You need to be more specific. For example would `./mydir/myfiLe.jpg` be considered a duplicate of `./otherdir/myfile.jpg`? – grochmal Jul 05 '16 at 19:52
  • I'm sure somebody would find it useful to do this across different paths, but I'm primarily concerned with files in the same directory because, if I understand correctly, this particular situation poses a challenge for windows/mac in a git repo. – dras Jul 06 '16 at 14:47

1 Answers1

1

The following script may do what you want (I have it set to echo what it would do, rather than actually do it, so you can see)

#!/bin/bash

# This variable will always be in lower case.  That means that if you do
# l=Hello the result will be $l==hello.
typeset -l l

for f in *
do
  l=$f # Forces to lowercase due to typeset
  if [ "$l" != "$f" -a -e "$l" ]
  then
    echo rm "$f"
  fi
done

So, for example:

$ ls
FIle.JpG  File.jpg  file.jpg

$ rem_case_dup.sh 
rm FIle.JpG
rm File.jpg
Stephen Harris
  • 42,369
  • 5
  • 94
  • 123
  • 1
    And, if you're not certain the upper case files really are dups, you might add a test that they contain the same data (some variant of `if diff >/dev/null 2>&1` will do it, lots of other choices too). – MAP Jul 06 '16 at 01:20
  • 1
    (1) This answer would be better if you *explained* how `$l` can be different from `$f` right after you’ve assigned `l=$f`.  Please do not respond in comments; [edit] your answer to make it clearer and more complete.   (2) The use if `-a` (and `-o`) in `[`…`]` expressions is discouraged.  Consider using `if [ "$l" != "$f" ]  &&  [ -e "$l" ]` instead. – G-Man Says 'Reinstate Monica' Jul 06 '16 at 07:03
  • Sorry, I didn't think basic `typeset` features needed explaining; they've only been around for 25+ years, so aren't new! A couple of comments added. Why is `-a` discouraged? – Stephen Harris Jul 06 '16 at 10:38
  • This is great. How can I make it recursive? – dras Jul 06 '16 at 16:09
  • Change the `for` loop to something like `find . -type f | while read -r f` - have the find` statement determine the scope of your checks; the rest of the loop will compare to files in the same directory as the upper case file was found in. – Stephen Harris Jul 06 '16 at 16:11