Filenames

從 USB 設備複製到硬碟的文件名稱全部變為大寫。如何解決?

  • August 12, 2014

I am currently using OpenBSD 5.5-release.

Whenever I copy files or directories from my USB device to the local HDD, the names of the copied files have all become uppercase.

What causes it?

How do I fix it?

You can fix it (each time it happens) with this command:

find *local_directory_name* -depth -exec sh -c 'dir="$(dirname "$0")"; FILE="$(basename "$0")"; lowfile="$(echo "$FILE" | tr "A-Z" "a-z")"; if [ "$lowfile" != "$FILE" ]; then mv "$0" "$dir/$lowfile"; fi' {} ";"

Type this all as one line (replacing *local_directory_name* with the name of the directory to which you copied the files).  You can break it into multiple lines by inserting backslashes.  Or you can put the part after sh -c into a script file.

This enumerates all the files in the directory (including subdirectories, recursively) and executes the given commands on each one.  -depth makes it work “bottom-up”, so it processes all the entries in a directory before it processes (renames) the directory itself.  Each filename (relative path starting from local_directory_name) is broken down into a directory portion and a plain filename (just the bottom component).  Then the filename is converted from upper case to lower case.  If this is different from the existing filename, it renames the file to the lower-case name.  I added this check to prevent the diagnostic messages you would otherwise get from trying to rename a file to itself, which would happen if you had a file whose name contained no letters (i.e., was numerals and special characters only).  Or, for that matter, if you had a file whose name contained no capital letters.

Afterthought: another way to avoid mv 123 123 errors is to add -name "*[A-Z]*" after -depth, which tells find to process only names that contain at least one capital letter.

You mentioned you are using the FAT32 file system. FAT, as well as NTFS are case insensitive FSes. I am assuming the driver does uppercase only to make sure there is not >1 file in the dir with the same name (though with different casing).

The problem you describe has been around for a while. See this thread from 2008 for example.

I suggest using a different FS or using TAR archives on your FAT/NFTS drives to retain casing.

引用自:https://unix.stackexchange.com/questions/149185