Bash script to add random number to all files

I have a smart phone that doesn’t seem to have a music shuffle function, so it’s best to write a bash script to add all the file names in the current directory with a random number.

Is it hard to do this?

+5
source share
6 answers

No, it’s not difficult. This, however, will ruin your carefully crafted file names and can be difficult to undo.

You can use $RANDOMas a simple source of random numbers in bash. For you, the case:

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

I have not tested this. You probably want to test this out on some small ones to make sure you know what it does.

+5
source

script , . -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.

script, , .

-u 1ddddd9-.

script Bash >= 3.2.

+4

. - :

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

script, 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
+1

, , . "Sh.his". reset , , . , , reset, "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

, , , , , . , MP3- , , , , , . LinuxQuestions.org, , :

"" MP3 . ( , .)

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

script Running, running_random MP3-, run_random.

0

All Articles