2009-07-23 93 views
1

我需要在一個目錄中按字母順序反向觸摸幾個文件,延遲時間爲1秒。這些文件的名稱中有空格。我已經試過這樣:
ls | sort -r | tr '\012' '\000' | xargs -0 touch從Z-> A延遲觸摸文件

這:

#!/bin/bash 

for i in $(ls -r); 
do 
    touch "$i" 
    sleep 1 
done 

而是先使它過於快速,沒有得到我想要的東西(的文件中,才能出現在我的設備) ,第二個不處理空間的權利。

任何想法?

編輯:對不起,忘了補充說,這樣做會更好,因爲如果我必須在文件之間等待1秒,並且我有60多個文件,我不想等待超過1分鐘。抱歉,添麻煩了。

+0

你`sleep`的版本可能支持小數秒。例如,如果您需要加快速度,請嘗試「sleep .5」。 – 2009-07-23 12:55:52

回答

0

最後我用這個:

#!/bin/bash 
(OFFSET_IN_SEC=0 
IFS=$'\n' 
# for each file in reverse alphabetic order 
for file in $(ls -r); do 
    # offset in seconds from current time        
    OFFSET_IN_SEC=$(($OFFSET_IN_SEC + 1)) 

    # current time + $OFFSET_IN_SEC in format used by touch command 
    TOUCH_TIMESTAMP=$(date -d "$OFFSET_IN_SEC sec" +%m%d%H%M.%S) 

    # touch me :) 
    # NOTE: quotes around $file are for handling spaces 
    touch -t $TOUCH_TIMESTAMP "$file" 
done) 

我已經包括IFS的設置,如約$文件中的報價不處理空間很好。

謝謝大家!

3

read將在一條線上一次讀:

ls -r | while read FILE; do 
    touch "$FILE" 
    sleep 1 
done 

或者,你可以用$IFS可變浪費時間,以便只有換行符在for i in list語法單獨項目,而不是空格或製表符:

(IFS=$'\n' 
for FILE in $(ls -r); do 
    touch "$FILE" 
    sleep 1 
done) 

(括號內添加這樣$IFS是繼恢復。Things'll可能去吃香蕉,如果你忘了,並把它設置爲非標準值。)

順便說一下,您也可以使用touch -t來跳過睡眠設置特定的時間戳。儘管如此,這看起來相當困難,所以我會把它留給一個更冒險的迴應者。 :-)

+0

有趣的是,我不知道IFS變量。 +1 – 2009-07-23 04:12:08

1

另一個bash的解決方案:

#!/bin/bash 
OFFSET_IN_SEC=0 

# for each file in reverse alphabetic order 
for file in (ls -r); do 
    # offset in seconds from current time        
    OFFSET_IN_SEC=$(($OFFSET_IN_SEC + 1)) 

    # current time + $OFFSET_IN_SEC in format used by touch command 
    TOUCH_TIMESTAMP=$(date -d "$OFFSET_IN_SEC sec" +%m%d%H%M.%S) 

    # touch me :) 
    # NOTE: quotes around $file are for handling spaces 
    touch -t $TOUCH_TIMESTAMP "$file" 
done 
+0

將循環外的初始時間戳記(日期-d)移動。如果這些文件位於軟盤上,或者跨緩慢的SAMBA共享,則觸摸所有60個文件可能需要1秒鐘以上的時間。 – 2009-07-24 04:51:39

0

這個工作對我來說:

while read ; do 
    [ -d "$REPLY" ] || touch "$REPLY" 
    sleep 1 
done < <(find . -maxdepth 1 | sort -r)