回答
不,這並不難。然而它會弄亂你精心設計的文件名,並且可能很難撤銷。
您可以使用$RANDOM
作爲bash中隨機數的簡單來源。爲您的 案件:
#!/bin/bash
for file in *; do
mv "$file" $RANDOM-"$file"
done
我沒有測試這個。你可能想在一些小樣本上自己測試一下,以確保你知道它做了什麼。
另外,請不要在任何給定的文件集上多次運行它,否則它會在另一個數字之前。沒有什麼比如「1413-426-234235-2-NeverGonnaGiveYouUp.mp3」。 – cHao 2010-07-04 16:29:10
@cHao:的確如此。移動文件的整個方法被打破。如果他可以在某個特定的目錄中使用軟鏈接,並在完成後刪除它。不知道他的文件系統是否可以做到這一點。 – 2010-07-04 16:51:20
不幸的是,我無法訪問鏈接等內容,因爲它只是一個FAT系統。幸運的是,文件名或多或少沒有意義,因爲重要的東西在ID3標籤中。可以使用例如 – Reinderien 2010-07-04 16:53:42
不是很難。例如:
for i in *; do mv "$i" $RANDOM-"$i"; done
如果腳本已經被洗牌,該腳本將洗牌文件並重新洗牌。如果你傳遞了一個參數-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。
通過此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
雖然這很有趣,但它與問題沒有關係 - 重命名文件,而不是播放音樂。 – Reinderien 2014-01-18 17:01:50
這是一個腳本,可以在我的博客上運行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
我知道這已經很老了,但我剛剛遇到類似的問題,也許這仍然是有用的。我剛剛買了一個便宜但防水的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。
- 1. 腳本預先在一個目錄內的所有文件名
- 2. 預先添加到文本文件
- 3. Unix:預先添加一個沒有虛擬文件的文件?
- 4. 一個bash腳本來計算所有文件的數量
- 5. Bash或Python:在所有.tex文件中遞歸地添加和預先加載一個字符串
- 6. 從文件有一個bash腳本
- 7. 試圖從一個隨機行從一個文本文件中添加文本
- 8. bash腳本 - 添加/從文件
- 9. Bash:在文件末尾添加一個隨機字母數字字符串
- 10. 在沒有SED的Bash腳本文件中添加文本
- 11. 添加一個標誌,bash腳本
- 12. 如何給我的bash腳本添加一個「nice」開關
- 13. 需要添加鏈接到一個PHP隨機圖像腳本
- 14. 加密多個文件中bash腳本
- 15. bash腳本從隨機文件列表中創建菜單
- 16. Bash shell腳本隨機運行不同的文件
- 17. 如何在bash腳本中有條件地添加一個額外的參數?
- 18. Bash Echo傳遞給另一個腳本,未按預期工作
- 19. 給一個數組一個隨機數
- 20. 向所有bash數組項添加尾隨換行符,但最後一個
- 21. bash腳本,創建一個目錄中的所有文件的數組
- 22. bash腳本,修改一個文件
- 23. 記錄一個bash腳本文件
- 24. 從bash文件中獲取一個隨機數的字節?
- 25. 使用bash腳本將文本添加到xls文件
- 26. 從bash腳本的文本文件中添加新用戶
- 27. 使用bash添加一個文本到多個文件
- 28. Applescript預先添加和附加文本到現有的textedit文件
- 29. 給一個bash腳本suid權限
- 30. 在隨機數發生器中添加所有隨機數的總和
可能希望通過'\'printf%05d $ RANDOM \' - 「$ i」'先運行以下建議 – 2010-07-04 16:44:25