Linux Bash脚本为所有文件预先添加一个随机数

Linux Bash脚本为所有文件预先添加一个随机数,linux,bash,Linux,Bash,我有一个智能手机,它似乎没有音乐洗牌功能,所以下一个最好的方法是编写一个bash脚本,在当前目录中的所有文件名前面加上一个随机数 这很难吗?不是很难。比如: for i in *; do mv "$i" $RANDOM-"$i"; done 不,这不难做到。但是,它会弄乱您精心制作的文件名,并且可能很难撤消 您可以在bash中使用$RANDOM作为简单的随机数源。为了你的 案例: 我没有测试这个。你可能想自己在一些小的机器上测试一下 示例以确保您知道它的作用。此脚本将洗牌文件,如果文件已被洗牌

我有一个智能手机,它似乎没有音乐洗牌功能,所以下一个最好的方法是编写一个bash脚本,在当前目录中的所有文件名前面加上一个随机数


这很难吗?

不是很难。比如:

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

不,这不难做到。但是,它会弄乱您精心制作的文件名,并且可能很难撤消

您可以在bash中使用$RANDOM作为简单的随机数源。为了你的 案例:

我没有测试这个。你可能想自己在一些小的机器上测试一下
示例以确保您知道它的作用。

此脚本将洗牌文件,如果文件已被洗牌,则重新洗牌。如果将参数-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,所以乱序文件名的格式是:1dddd9 filename可能带有空格,以及其他内容。1983.mp3

如果重新运行脚本,它将通过更改前缀中的随机数来重新排列文件

-u参数将删除1dd9前缀


脚本需要Bash>=3.2版。

通过此shell,您的音乐库将随机播放,在播放完所有歌曲之前不会重复任何歌曲。 播放歌曲的历史记录记录在文件中。嘘,他的。 如果您向音乐库中添加了一首歌曲,或者已经听到了库中的所有歌曲,并生成了一个新的随机列表,则会自动重置此历史记录。只要需要,您可以在删除文件时重置历史记录。嘘,他的

#!/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

下面是一个脚本,它将在我的博客中的OSX和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播放器,它可以很好的运行,只是在洗牌模式下,同样的几首歌似乎会不断重复。我找到了一些可以根据需要修改的说明,因此,经过一番尝试和错误后,我得出了以下结论:

我创建了一个名为Running的文件夹,并将我跑步播放列表中的所有MP3放在其中。我将文件夹名称大写,以免意外删除它

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

我从Running目录中调用脚本,将新创建的Running_random目录中的内容复制到我的MP3播放器,然后删除Running_random。

此外,不要在任何给定的文件集上多次运行该脚本,否则它会预加另一个数字。没有什么能比得上1413-426-234235-2-NeverGonnaGiveYouUp.mp3了。@cHao:没错。移动文件的整个方法都被破坏了。如果他可以在某个特定目录中使用软链接,并在完成后将其删除。不确定他的文件系统是否能做到这一点。不幸的是,我无法访问像链接这样的东西,因为它只是一个胖系统。幸运的是,文件名或多或少是没有意义的,因为重要的东西在ID3标记中。可以使用例如printf%06d$RANDOM来保持随机部分a的长度不变。@cHao:实际上,4-8-15-16-23-42-NeverGonnaGiveYouUp.mp3会更糟糕;Pmight希望通过`printf%05d$RANDOM`-$i first运行下面的建议虽然这很有趣,但与问题无关——重命名文件,而不是播放音乐。
 #!/bin/bash
 mkdir ../running_random
 for fname in *.mp3
 do
         cp "$fname" ../running_random/$RANDOM."$fname".mp3
 done