長い名前のファイルをコピーする

長い名前のファイルをコピーする

私は Linux Ubuntu 16 を使っていますが、HDD から SSD に約 400 GB (約 100,000 ファイル) のデータをコピーする必要があります。約 1,000 個のファイルに「名前が長すぎる」ものがあり、それらを見つけるのに時間がかかるため、スキップすることができないために、これを行うことができません。長い名前のファイルをコピーするプログラムはありますか?

答え1

元の(間違った)答え

クールな人たちが言うには、それはrsync魔法のように効くそうです。

rsync -auv --exclude '.svn' --exclude '*.pyc' source destination

元の回答:https://superuser.com/a/29437/483428

UPD: スクリプト付き

よし、他のクールな男たちは、それはrsync解決策ではないと言ったが、ファイルシステム自体は長い名前をサポートしていない。これは神々が作った形而上学的な低レベルの超秘密ツールではないことに注意してくださいrsync(ちなみに、Windows にはそのようなツールがたくさんあります)。

そこで、ここに短いPythonスクリプトがあります(私の知る限り、UbuntuではPython 2.7がデフォルトでインストールされています)。これは、からのすべてのファイルをにコピーしSRCDSTエラーが発生したファイルの名前(長い名前を含む)を出力します。

  1. 保存するcopy.py
  2. 使用法:python copy.py SRC DEST
import os
import sys
import shutil

def error_on_dir(exc, dest_dir):
    print('Error when trying to create DIR:', dest_dir)
    print(exc)
    print()

def error_on_file(exc, src_path):
    print('Error when trying to copy FILE:', src_path)
    print(exc)
    print()

def copydir(source, dest, indent = 0):
    """Copy a directory structure overwriting existing files"""
    for root, dirs, files in os.walk(source):
        if not os.path.isdir(root):
            os.makedirs(root)
        for each_file in files:
            rel_path = root.replace(source, '').lstrip(os.sep)
            dest_dir = os.path.join(dest, rel_path)
            dest_path = os.path.join(dest_dir, each_file)

            try:
                os.makedirs(dest_dir)
            except OSError as exc:
                if 'file exists' not in str(exc).lower():
                    error_on_dir(exc, dest_dir)

            src_path = os.path.join(root, each_file)
            try:
                shutil.copyfile(src_path, dest_path)
            except Exception as exc:
                # here you could take an appropriate action
                # rename, or delete...
                # Currently, script PRINTS information about such files
                error_on_file(exc, src_path)


if __name__ == '__main__':
    arg = sys.argv
    if len(arg) != 3:
        print('USAGE: python copy.py SOURCE DESTINATION')
    copydir(arg[1], arg[2])

関連情報