長い名前のファイルをコピー [閉じる]

長い名前のファイルをコピー [閉じる]

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

ベストアンサー1

元の(間違った)回答

素敵な人が言うと、rsync魅力のように動作します。

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

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

UPD:スクリプトを含む

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

SRCしたがって、ここにすべてのファイルをコピーしてファイルDST名を印刷してエラー(長い名前を含む)を引き起こす短いPythonスクリプトがあります(私が知る限り、UbuntuにはデフォルトでPython 2.7がインストールされています)。

  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])

おすすめ記事