Search code examples
bashsedfat32

Truncate files names over 255 characters


I am trying to copy some files with long file names onto an old Windows XP 32bit FAT32 system and I am getting errors of having too long file names. How could I recursively search through a directory for file names greater than or equal to 255 chars and truncate them as appropriate for a FAT32 filesystem?


Solution

  • I'm sure find can do the whole job, I couldn't quite get the last step so resorted to using some bash foo:

    #/bin/bash
    
    find . -maxdepth 1 -type f -regextype posix-extended -regex ".{255,}" |
    while read filename
    do 
        mv -n "$filename" "${filename:0:50}"
    done
    

    Using find to get all the files with filename greater than or equal to 255 characters:

    find . -maxdepth 1 -type f -regextype posix-extended -regex ".{255,}"

    Truncate those filenames to 50 characters, -n do not overwrite an existing file.

    mv -n "$filename" "${filename:0:50}"

    Note: Can this be with the -exec option anyone?