Score:2

Creation of a simple script that renames a file picking randomly from a group of files

ag flag

The idea is to play a "greeting" sound file during startup in KDE.

Inside a folder, there would be, for example, 10 audio files. Let's say, named 1.ogg 2.ogg 3.ogg etc.

The file that actually plays is called greetings.ogg.

How do I randomly take one of these files and rename it as greetings.ogg without losing the 10 original files?

I am trying my best, but I'm failing miserably.

hr flag
It sounds like you actually want to randomly *copy* (not rename) one of the files
TylerW avatar
gb flag
Not an answer to this question, but for your example would it be simpler for a script to choose and play a random file using something like `file_to_play=$(ls greetings/*.ogg | shuf -n 1)` instead of renaming one at random?
Score:8
it flag

You cannot both "rename" a file and "not lose the original", you'll have to copy (man cp) the randomly selected original file to greetings.ogg, something like:

# select among 0.ogg .. 9.ogg
cp $(( $RANDOM % 10 )).ogg greetings.ogg

Read man bash, you might want to initialize $RANDOM.

Erniemenendez avatar
ag flag
Thank you, your answer helped me out, made it so simple!!! THANKS
Score:5
hr flag

If your files are not necessarily named with a simple arithmetic scheme amenable to the use of the shell's $RANDOM variable, then another option is to use shuf:

shopt -s extglob

cp -- "$(printf '%s\n' !(greeting).ogg | shuf -n 1)" greeting.ogg

The ksh-style extended glob !(greeting).ogg avoids copying the existing file to itself - you could avoid that by copying the file to a different directory.

Ginnungagap avatar
cn flag
I think a simple symlink would do nicely instead of needlessly copying on every invocation. Still a nice and concise answer, +1
Score:4
us flag

A more elegant solution (as already suggested in comments by both Ginnungagap and James S.), would be to use symlinks, like so:

#!/bin/bash

# Select among ten files at random and make a renamed copy inside /home/$user

ln -sf $(( $RANDOM % 10)).mp3 /home/x/greetings.mp3

Note: ln is both faster and more efficient, in terms of disk usage, than cp.

Note the use of the -f option to overwrite any link to a previous greeting sound - see Create symlink - overwrite if one exists.


Alternatively, avoiding the use of $RANDOM (and bash) altogether, and using shuf instead:

#!/bin/sh

rand=`shuf -i 1-10 -n 1`
ln -sf ${rand}.mp3 /home/x/greetings.mp3

The use of shuf is taken from this answer to Generate random numbers in specific range).

Score:1
ag flag

After a long search, I figured it out. It is so simple!!!!

I created a folder named greetingsTest inside my /home/$user/ directory so it obviously looks like; /home/$user/greetingsTest

Inside there I have 10 .mp3 files (maybe one day I will expand it to 20) and I also have a file called randomizer.sh that looks like this;

#!/bin/bash

###///// select among ten files at random and make a renamed copy inside /home/$user

cp $(( $RANDOM % 10)).mp3 /home/x/greetings.mp3

So simple! Thank all you for the help!!!

James S. avatar
de flag
Instead of using `cp`, which makes an actual copy on the disk and is therefore relatively slow, why not use `ln -s` which creates a symbolic link and is *much* faster and does not use any additional disk space?
Erniemenendez avatar
ag flag
Thank you very much!!! id did a man ln and I went with ln-sf THANK YOU!!!
James S. avatar
de flag
Awww yiss. :) linux/unix filesystems do this thing and it's really cool; when folks move over from Windows they usually don't know about it but it's a great way to do a lot of different things.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.