2010-07-04 52 views
5

我有一個似乎沒有音樂隨機播放功能的「智能」手機,所以下一個最好的做法是編寫一個bash腳本來預先加載當前所有文件名目錄中有一個隨機數字。Bash腳本預先給所有文件添加一個隨機數

這難以做到嗎?

+0

可能希望通過'\'printf%05d $ RANDOM \' - 「$ i」'先運行以下建議 – 2010-07-04 16:44:25

回答

5

不,這並不難。然而它會弄亂你精心設計的文件名,並且可能很難撤銷。

您可以使用$RANDOM作爲bash中隨機數的簡單來源。爲您的 案件:

#!/bin/bash 
for file in *; do 
    mv "$file" $RANDOM-"$file" 
done 

我沒有測試這個。你可能想在一些小樣本上自己測試一下,以確保你知道它做了什麼。

+4

另外,請不要在任何給定的文件集上多次運行它,否則它會在另一個數字之前。沒有什麼比如「1413-426-234235-2-NeverGonnaGiveYouUp.mp3」。 – cHao 2010-07-04 16:29:10

+0

@cHao:的確如此。移動文件的整個方法被打破。如果他可以在某個特定的目錄中使用軟鏈接,並在完成後刪除它。不知道他的文件系統是否可以做到這一點。 – 2010-07-04 16:51:20

+0

不幸的是,我無法訪問鏈接等內容,因爲它只是一個FAT系統。幸運的是,文件名或多或少沒有意義,因爲重要的東西在ID3標籤中。可以使用例如 – Reinderien 2010-07-04 16:53:42

3

不是很難。例如:

for i in *; do mv "$i" $RANDOM-"$i"; done 
4

如果腳本已經被洗牌,該腳本將洗牌文件並重新洗牌。如果你傳遞了一個參數-u它將取消文件的刪除(刪除隨機前綴)。

#!/bin/bash 
for file in *.mp3 
do 
    if [[ -d $file ]] 
    then 
     continue # skip directories 
    fi 
    if [[ $file =~ ^1[0-9]{5}9-(.*).mp3$ ]] # get basename 
    then 
     name=${BASH_REMATCH[1]}    # of a previously shuffled file 
    else 
     name=${file%.mp3}      # of an unshuffled file 
    fi 
    if [[ $1 != -u ]] 
    then 
     mv "$file" "1$(printf "%05d" $RANDOM)9-$name.mp3" # shuffle 
    else 
     if [[ ! -e "$file.mp3" ]] 
     then 
      mv "$file" "$name.mp3"       # unshuffle 
     fi 
    fi 
done 

它使用一個固定寬度五位隨機數之後的「1」和隨後「9-」這樣的混洗的文件名是以下形式:1ddddd9-filename maybe with spaces - and other stuff.1983.mp3

如果您重新運行腳本,它將通過更改前綴中的隨機數來重新編排文件。

-u參數將刪除前綴1ddddd9-

該腳本需要Bash> =版本3.2。

0

通過此shell,您的音樂庫將隨機播放,無需重複播放任何歌曲,直到播放完所有歌曲。 播放的歌曲的歷史被記錄在文件「.Sh.his」中。 如果您向音樂庫中添加了歌曲,或者已經收聽了您的音樂庫中的所有歌曲,則會自動重置此歷史記錄,並生成一個新的隨機列表。每當你想要你可以重置的歷史是刪除文件「。Sh.his」。

#!/bin/bash 

#-----------------------------------INFO---------------------------------------------------------- 

#Through this shell, your music library will be played randomly, without repeating any songs until all have been played. 
#The history of songs played is recorded in the file "*. Sh.his". 
#This history is reset automatically if you added a song to the music library or have already heard all the songs of your library, 
#generating a new random list ever. Whenever you want you can reset the history is deleting the file "*. Sh.his". 

#Press "q" to skip song 
#Press "p" to pause song and resume song 

#------------------------------CONFIGURATION------------------------------------------------------ 

#mplayer package needed (For debian/Ubuntu/Mint: "$ apt-get install mplayer") 

#Select your music library path (all recursive folders will be included in the .mp3 files search): 
path="/media/Datos/Música/Music/" 

#------------------------------------------------------------------------------------------------- 

while true 
do 

cadena=$(find "$path" -iname '*.mp3')         #search media files 
nmedia=$(echo "$cadena" | wc -l) 

if [ -f "$0.his" ]           #file exist 
then 
    value=$(<"$0.his")          #read file 

    if [[ ($(echo "$value" | sed -n 1p) != $nmedia) || ($(echo "$value" | sed -n 2p) == 0) ]] #reset file conditions 
    then 
     listrand=$(seq 1 $nmedia | shuf) 
     index=$nmedia 
    else            #no reset file conditions 
     nmedia=$(echo "$value" | sed -n 1p) 
     index=$(echo "$value" | sed -n 2p) 
     listrand=$(echo "$value" | sed -n 3p) 
     listrand=$(echo "$listrand" | sed s/" "/\\n/g) 
    fi 

else             #file not exist 
    listrand=$(seq 1 $nmedia | shuf) 
    index=$nmedia 
fi 

nrand=$(echo "$listrand" | sed -n "$index"p)        #select random number 
cadena=$(echo "$cadena" | sed -n "$nrand"p)        #select song with random number 
index=$((index-1))           #write file 
echo $nmedia > "$0.his" 
echo $index >> "$0.his" 
echo $listrand >> "$0.his" 
mplayer "$cadena"           #play media file 

done 
exit 0 
+0

雖然這很有趣,但它與問題沒有關係 - 重命名文件,而不是播放音樂。 – Reinderien 2014-01-18 17:01:50

1

這是一個腳本,可以在我的博客上運行OS X和Linux。

#!/bin/bash 
# 
# FILE: 
# prepend_random_num.sh 
# ABOUT: 
# Prepends a random number between 1-65000 and an underscore to all files of specified type 
# Runs on Mac OSX & Linux 
# EXAMPLE: 
# $ ls 
# a.jpg b.jpg 
# $ sh prepend_random_num.sh jpg 
# $ ls 
# 138_b.jpg 8474_a.jpg  

for file in *.$1 
do 
    rand=$(jot -r 1 1 65000 || shuf -i 1-65000 -n 1) 
    mv "$file" "$rand"_"$file" 
done 
0

我知道這已經很老了,但我剛剛遇到類似的問題,也許這仍然是有用的。我剛剛買了一個便宜但防水的MP3播放器用於跑步,除了在洗牌模式下,相同的幾首歌似乎不斷重複。我發現LinuxQuestions.org一些指令,我可以修改我的需求,所以這裏就是我想出了一個小試驗和錯誤後:

我創建了一個名爲運行文件夾,把我所有的MP3從我的運行播放列表在那裏。 (我大寫的文件夾名稱,所以我不會意外刪除它。)

#!/bin/bash 
mkdir ../running_random 
for fname in *.mp3 
do 
     cp "$fname" ../running_random/$RANDOM."$fname".mp3 
done 

我打電話從運行目錄下的腳本,從新建running_random目錄中的內容複製到我的MP3播放器,然後我刪除running_random。

相關問題