Score:-4

mv renaming files deleted it

tg flag

if i had a playlist filled with music, i would want it to shuffle and play because i wouldn't want to hear the same song order over and over again as it gets old real fast.. same thing here but with pictures

so i wanted my files randomized and i looked up a command to randomize file names and i got this

for file in *.jpg; do
mv -- "$file" "$RANDOM-$RANDOM-$RANDOM.jpeg"
done

it worked well but the problem was that it only changed jpeg so i did this

for file in *.jpg; do
mv -- "$file" "$RANDOM-$RANDOM-$RANDOM.jpeg"
done

for file in *.jpeg; do
mv -- "$file" "$RANDOM-$RANDOM-$RANDOM.jpg"
done

for file in *.JPG; do
mv -- "$file" "$RANDOM-$RANDOM-$RANDOM.png"
done

i ran this command as a shell script and it removed all files from the folder. And no where to be found. i also ran it in a removable disk

EDIT:

It just turned out to be a visual glitch and a power outage fixed it.

The reason why i took so long to edit this is cause my boot got corrupted and it took really long to fix it.

Thanks to anyone who tried to help.

David avatar
cn flag
From your question "i wanted to randomize file names on my folder so do go through it in the same order everytime i go through it." What does this mean.
bruh avatar
tg flag
if i have a playlist filled with music, i would want it to shuffle and play because i wouldn't want to hear the same song order over and over again as it gets old real fast.. same thing here but with pictures
David avatar
cn flag
Then that is what the question should have said. I suggest you edit and re word your question.
Artur Meinild avatar
vn flag
It seems you changed all your `*.JPG` files to `*.png`. So you'll want to look for `*.png` files and not `*.JPG`.
bruh avatar
tg flag
uhmm i had all three file types in my folder so i wanted to rename and randomize all three of those file types thats why the command is repeating with different file types. i dont think it all will change into one file type
bruh avatar
tg flag
it randomized the name of the file so i dont know the name of it to search for it
Bodo avatar
pt flag
Please [edit] your question and explain how exactly you ran these commands. Did you type the commands in a terminal window? Did you run a shell script? Did you copy&paste the commands exactly as you ran them to the question? Does "*it removed all files from the folder*" mean that the folder is now empty? How did you check this? The commands shown in the question should only rename files and would ask for confirmation if two files would get the same random name. I guess you also ran other commands not shown in the question. BTW: The third command will rename `*.JPG` files to **`.png`**.
cn flag
@bruh: it is `ls *.png`. And the error is in the "for' of the last one: `for file in *.JPG; do` should have been `for file in *.png; do`
hr flag
Although there is a *small* probability that `$RANDOM-$RANDOM-$RANDOM` generates non-unique names in bash, the only way I can see that your commands would have "deleted" *all* your files (well, technically *overwritten* all but 3 of them) would be if you ran the commands in a shell that does not provide the `RANDOM` builtin variable, so that `$RANDOM` returns the empty string. When run using `/bin/sh` for example, you'd end up with 3 files named `--.jpeg`, `--.jpg`, `--.png`. The `mv` command won't stop you doing that unless run with the `-n`/`--no-clobber` or `-i`/`--interactive` option.
bruh avatar
tg flag
@Bodo i ran it in a shell script. the command doesnt look like it deletes or remove files but it did.
bruh avatar
tg flag
@steeldriver the commands given above is exactly the way its written in the shell script. idk if RANDOM is a builtin variable in the shell script
hr flag
@bruh so what shebang did you use in the script? How exactly did you run it? Have you checked for files of the names I mentioned?
bruh avatar
tg flag
@steeldriver i pasted those commands in a txt file and saved it as .sh. i ran it and what i saw was names of files in the folder turning into a bunch of numbers and letters and slowly disappearing one by one. i ran this sricpt in a removable device so im not sure if its in my main disk or the removable one, that is if it moved to somewhere else
Score:1
in flag

You can utilize the Python programming language for this very easily:

import glob
import random

# reads in from the current folder the files with the pattern *.mp3 and assign it to a variable of type list.
music_files = glob.glob("*.mp3")

# is picking randomnized a file from the variable.
random_music_file = random.choice(music_files)

Python is installed up front on each Ubuntu installation.

Artur Meinild avatar
vn flag
Welcome! The OP asked about renaming image files. How does this even get close? Also, you should explain what the script does if it's universally applicable.
kladderradatsch avatar
in flag
@ArturMeinild, he talks of music files as well as of image files.
user535733 avatar
cn flag
+1: Looks like the OP asked an [XY Question](https://en.wikipedia.org/wiki/XY_problem). For the original, underlying problem, this is a good answer. How to answer an XY Question is always a challenge. The BEST option would be if the OP fixed their question, but we know that's rare.
Artur Meinild avatar
vn flag
As I read it, the underlying question/problem is that the OP lost his files - which have absolutely nothing to do with what he was originally trying to do. And yeah, we can't really do anything about that.
Score:0
ca flag

To possibly locate your files, with five digits and a dash in the name, with a name end with jpg:

First do
$ sudo updatedb - to freshen the database... then
$ locate --regex "[0-9]{5}-.*jpg$"

Oh, and prior to those you need e.g.
$ sudo apt install mlocate - if you haven't already done it.

One more thing: if PRUNEPATHS= in /etc/updatedb.conf happens to contain the path where the files are, they won't be shown.

bruh avatar
tg flag
im not sure what the output is supposed to be but i got nothing. i ran the command in terminal and there were no output
Hannu avatar
ca flag
The output (from `locate`) would be paths and names of your jpg files, if `sudo apt updatedb` includes them in the database. i.e. PRUNEPATHS in /etc/updatedb.conf tells which paths NOT to include - by default, one path among those is `/media/*`
I sit in a Tesla and translated this thread with Ai:

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.