2012-12-09 94 views
1

我想将一些带有长文件名的文件复制到旧的Windows XP 32位FAT32系统上,并且出现文件名太长的错误。我如何递归搜索文件名大于或等于255个字符的目录,并将它们截断为适合FAT32文件系统?截断超过255个字符的文件名

回答

2

我敢肯定find可以做到全工作,我不能完全得到最后一步,所以采用了一些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 

使用find让所有与文件名大于或等于255个字符的文件:

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

截断这些文件名到50个字符,-n不覆盖现有文件。

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

注:这可以与-exec选项的人?

0

在这里你去:

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 

我却不知道如何可以做到这一点,但一些简单的修改到first Google result to "unix truncate file name"足以产生上述溶液。试一试第一;)

0

你可以在一个终端与perl做到这一点:

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" 
     } 
    ' 

重命名的文件将看起来像:

truncated_1.ext 
truncated_2.ext 
(...) 
相关问题