複製具有長名稱的檔案

複製具有長名稱的檔案

我使用的是 linux ubuntu 16,我需要將大約 400GB(約 100.000 個檔案)的資料從 HDD 複製到 SSD。我不能這樣做,因為其中大約 1000 個文件的名稱“太長”,而且我不能跳過它們,因為找到它們需要很長時間。有沒有複製長名稱檔案的程式?

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

相關內容