2017-01-20 30 views
0

我有一個文本文件,其中列出了大量的文件路徑。我需要將源目錄中的所有這些文件(每行中的文件路徑中提到)複製到目標目錄。從一個目錄到另一個目錄複製文本文件中提到的大量文件路徑的最快方法

目前,我試着在命令行是

while read line; do cp $ line dest_dir; done < my_file.txt 

這似乎是有點慢。有沒有辦法平行整個事情或加快速度?

+0

*有沒有辦法來parallelise這件事*您必須能夠同時的讀取磁盤系統和寫入操作來自多個地點? –

+0

@HighPerformanceMark我在一臺unix服務器上,它連接到一個外部SSD磁盤。 –

+0

@HighPerformanceMark,因此我有一個文件,其中包含SSD上文件的一大組文件路徑。我需要將它們複製到服務器上的目錄中。即使SSD安裝在機器上,其目標是將其複製到服務器,然後執行scp命令將整個目錄複製到我的筆記本電腦。 –

回答

2

你可以嘗試GNU並行如下:

parallel --dry-run -a fileList.txt cp {} destinationDirectory 

如果你喜歡它說什麼,取出--dry-run

+0

並行磁盤I/O可能會也可能不會更快:https://oletange.wordpress.com/2015/07/04/parallel-disk-io-is-it-faster/ –

+0

@ OleTange是的,謝謝。我意識到這一點,我試圖暗示這僅僅是一個嘗試 - 就像你在文章中所說的那樣。鏈接到您的文章是非常合適的 - 謝謝。 –

0

你可以做一些以下(在你選擇的外殼)

 

    #!/bin/bash 

    BATCHSIZE=2 

    # **NOTE**: check exists with -f and points at the right place. you might not need this. depends on your own taste for risk. 
    ln -s `which cp` /tmp/myuniquecpname 

    # **NOTE**: this sort of thing can have limits in some shells 
    for i in `cat test.txt` 
    do 

    BASENAME="`basename $i`" 

    echo doing /tmp/myuniquecpname $i test2/$BASENAME & 

    /tmp/myuniquecpname $i test2/$BASENAME & 

    COUNT=`ps -ef | grep /tmp/myuniquecpname | grep -v grep | wc -l` 

    # **NOTE**: maybe need to put a timeout on this loop 
    until [ $COUNT -lt $BATCHSIZE ]; do 

    COUNT=`ps -ef | grep /tmp/myuniquecpname | grep -v grep | wc -l` 
    echo waiting... 
    sleep 1 

    done 

    done 

相關問題