Bash脚本:在所有文件名前添加随机数

6
我有一部“智能”手机,似乎没有音乐随机播放功能,所以最好的办法是编写一个 Bash 脚本,用一个随机数在当前目录中的所有文件名前添加。

这个难度大吗?


你可能想先通过\printf %05d $RANDOM`-"$i"`运行下面的建议。 - Matt Joiner
6个回答

9

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

您可以在bash中使用$RANDOM作为简单的随机数源。对于您的情况:

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

我没有测试过这个。你最好在一些小的样本上自己测试一下,以确保你知道它的作用。


4
不要在同一组文件上运行超过一次,否则它会添加另一个数字。最好避免出现像“1413-426-234235-2-NeverGonnaGiveYouUp.mp3”这样的情况。 - cHao
@cHao:没错。整个移动文件的方法都是有问题的。如果他可以在某个特定目录中使用软链接,完成后再删除它就好了。不确定他的文件系统是否支持这样做。 - Benjamin Bannier
不幸的是,由于这只是一个FAT系统,我无法访问诸如链接之类的东西。幸运的是,文件名或多或少没有意义,因为重要的东西在ID3标签中。 - Reinderien
2
可以使用例如 printf "%06d" $RANDOM 来保持随机部分的长度恒定。 - Dummy00001
2
@cHao:实际上,4-8-15-16-23-42-NeverGonnaGiveYouUp.mp3会更糟糕;P - ninjalj

6

这个脚本会将文件进行随机排序,并在已经排序过的情况下重新排序。如果你传入一个-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-文件名(可能有空格)-和其他东西.1983.mp3

如果重新运行脚本,它会通过更改前缀中的随机数来重新洗牌文件。

-u参数将删除1ddddd9-前缀。

该脚本要求Bash版本>= 3.2。


4

并不是很难。可以这样:

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

2
这是我博客上的一个脚本,可在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上找到了一些指导,可以根据我的需求进行修改,所以在试错一番后,我想出了以下解决方案:

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

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

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


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

虽然这很有趣,但与问题——重命名文件而不是播放音乐——关系不大。 - Reinderien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接