2012-12-09 92 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 
(...) 
相關問題