0

Some cloud storage services let one download chosen directories as ZIP archives. But it appears that, on one of such services, the archives contain different-cased versions of the same directory. E.g. if I have Books directory stored in the cloud, the archive could have two entries for it: Books and books, and both of these may have some data – several files in one and others in another, so that total number of files is the same as in the cloud. And this problem happens recursively in the whole directory tree.

All cases I've seen only split the directories in no more than two versions: 1) correctly-named, and 2) all-lowercase.

I'd like, after (or in the process of) extracting this ZIP archive, to merge the directories, so that for each pair of case-differing directories I got only the correctly-named directory with all its original data as well as the data from its all-lowercase sister.

What is an easy way to do this?

0

1 Answer 1

2

To merge all directories to lowercase, one approach is to recurse over all directories and merge them into their lowercase counterpart. The traversal needs to be done in depth first mode, so that when you merge two directories, their content is already normalized.

The following script might do the job:

find . -depth -name '*[[:upper:]]*' -type d -execdir sh -c '
  source=$0
  target=$(echo "$source" | tr "[:upper:]" "[:lower:]")
  if [ "$source" != "$target" ]; then  # need to rename or merge
    if [ -d "$target" ]; then
      # merge $source (mixed- or uppercase) into $target (lowercase)
      find "$source" -mindepth 1 -maxdepth 1 -exec mv -bt "$target" {} +
      rmdir "$source"
    else
      mv "$source" "$target"
    fi
  fi
' {} \;
4
  • So, this collapses the names to all-lowercase. But what about maintaining the mixed-case version (given, as noted in the OP, that there is at most one such version)?
    – Ruslan
    Commented Dec 26, 2019 at 11:58
  • 1
    Do you mean that you wish to do the opposite - merge the lower-case into the mixed-case?
    – harrymc
    Commented Dec 26, 2019 at 12:10
  • Yes, the aim is to preserve the mixed case, not to collapse all to lowercase.
    – Ruslan
    Commented Dec 26, 2019 at 12:18
  • 1
    In this case you need to invert $source and $target inside the outer if.
    – harrymc
    Commented Dec 26, 2019 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .