Domanda

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?

È stato utile?

Soluzione

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?

Altri suggerimenti

Here you go:

find /path/to/base/dir -type f | \
while read filename
do
    file="${filename%%.*}"
    ext="${filename##*.}"
    if [[ "${#file}" -gt 251 ]]; then
         truncated=$(expr $file 1 251)
         mv "${filename}" "${truncated}"."${ext}"
    fi     
done

I actually didn't know how to do this either, but some simple modifications to the first Google result to "unix truncate file name" was sufficient to yield the above solution. Try it yourself first ;)

You can do it with perl in a terminal :

cd /d \path\to\dir
find . -type f |
    perl -F'[/\\]' -lane '
        $f = $F[-1];
        ($ext) = $f =~ /(\.[^.]+)$/;
        delete $F[-1];
        $path = join "/", @F;

        if (length($f) > 255) {
            $c++;
            rename "$path/$f", "$path/truncated_$c$ext"
        }
    '

The renamed files will looks like :

truncated_1.ext
truncated_2.ext
(...)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top