Bash 脚本为所有文件添加一个随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3175302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script to prepend a random number to all files
提问by Reinderien
I have a "smart" phone that doesn't seem to have a music shuffle function, so the next best thing is to write a bash script to prepend all filenames in the current directory with a random number.
我有一部似乎没有音乐随机播放功能的“智能”手机,所以下一个最好的方法是编写一个 bash 脚本,在当前目录中的所有文件名前加上一个随机数。
Is this difficult to do?
这很难做到吗?
回答by Benjamin Bannier
No, this is not hard to do. It will however mess up your carefully crafted filenames, and might be hard to undo.
不,这并不难做到。然而,它会弄乱您精心制作的文件名,并且可能难以撤消。
You can use $RANDOMas a simple source of random numbers in bash. For your
case:
您可以$RANDOM在 bash 中用作随机数的简单来源。对于您的情况:
#!/bin/bash
for file in *; do
mv "$file" $RANDOM-"$file"
done
I didn't test this. You probably want to test this yourself on some small sample to make sure you know what it does.
我没有测试这个。您可能希望自己在一些小样本上进行测试,以确保您知道它的作用。
回答by Paused until further notice.
This script will shuffle files and reshuffle them if they've already been shuffled. If you pass it an argument of -uit will unshuffle the files (remove the random prefix).
如果文件已经被打乱,这个脚本将打乱文件并重新打乱它们。如果您向它传递一个参数,-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 [[ != -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
It uses a fixed-width five digit random number after a "1" and followed by "9-" so the shuffled filenames are of the form: 1ddddd9-filename maybe with spaces - and other stuff.1983.mp3.
它在“1”之后使用一个固定宽度的五位随机数,后跟“9-”,因此无序文件名的格式为:1ddddd9-filename maybe with spaces - and other stuff.1983.mp3.
If you re-run the script, it will reshuffle the files by changing the random number in the prefix.
如果您重新运行脚本,它将通过更改前缀中的随机数来重新排列文件。
The -uargument will remove the 1ddddd9-prefix.
该-u参数将删除1ddddd9-前缀。
The script requires Bash >= version 3.2.
该脚本需要 Bash >= 3.2 版。
回答by zaf
Not really difficult. Something like:
真的不难。就像是:
for i in *; do mv "$i" $RANDOM-"$i"; done
回答by CPrescott
Here's a script that will run on OS X and linux from my blog.
这是我博客中将在 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 *.
do
rand=$(jot -r 1 1 65000 || shuf -i 1-65000 -n 1)
mv "$file" "$rand"_"$file"
done
回答by CPrescott
I know this is pretty old, but I was just confronted with a similar problem, and maybe this'll still be useful. I just bought a cheap but waterproof MP3 player for running, which works fine except that in shuffling mode the same few songs seem to keep repeating. I found some instructions on LinuxQuestions.orgthat I could modify for my needs, so here's what I came up with after a little trial and error:
我知道这已经很老了,但我刚刚遇到了类似的问题,也许这仍然有用。我刚买了一个便宜但防水的 MP3 播放器来跑步,它工作得很好,除了在随机播放模式下,同样的几首歌曲似乎一直在重复。我在LinuxQuestions.org上找到了一些说明,我可以根据自己的需要修改这些说明,所以这是我经过一些反复试验后得出的结论:
I created a folder called Running and put all my MP3s from my running playlist in there. (I capitalized the folder name so I don't accidentally delete it.)
我创建了一个名为 Running 的文件夹,并将我正在运行的播放列表中的所有 MP3 放在那里。(我将文件夹名称大写,以免不小心将其删除。)
#!/bin/bash
mkdir ../running_random
for fname in *.mp3
do
cp "$fname" ../running_random/$RANDOM."$fname".mp3
done
I call the script from inside the Running directory, copy the contents from the newly created running_random directory to my MP3 player, then I delete running_random.
我从 Running 目录中调用脚本,将新创建的 running_random 目录中的内容复制到我的 MP3 播放器,然后删除 running_random。
回答by Felipe
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".
通过此外壳,您的音乐库将随机播放,在播放完所有歌曲之前不会重复任何歌曲。播放歌曲的历史记录在文件“ .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 "##代码##.his" ] #file exist
then
value=$(<"##代码##.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 > "##代码##.his"
echo $index >> "##代码##.his"
echo $listrand >> "##代码##.his"
mplayer "$cadena" #play media file
done
exit 0

