Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2943  / 2 Years ago, sat, march 12, 2022, 3:49:55

I'm working on a system where I'd like the first letter of each filename to be random - not necessarily unique, just random. I'd like to be able to run this from a cron job.



For example, I would be able to point it at a list of files like



song.mp3    song1.mp3    song2.mp3
song3.mp3 song4.mp3 song5.mp3


And have it change the names of the files to something like this:



a song.mp3    g song1.mp3    k song2.mp3
r song3.mp3 l song4.mp3 e song5.mp3


Is there a way to do this?


More From » 12.04

 Answers
7

See this script. I tried the exactly what you wanted.



#!/bin/bash

for item in *.mp3
do
mv "$item" "$(cat /dev/urandom | tr -cd 'a-z' | head -c 1) $item"
done


How does random letters picked up



Here I am picking up strings from /dev/urandom and deleting all the characters which are not in between [a-z] and at last keeping only the first letter.



How to use



Copy the code to a file, say rename_song.sh, give it execution permission using terminal as follows,



chmod +x /path/to/rename_song.sh


Copy rename_song.sh to the directory where you have .mp3 files. run the script from terminal (first navigate to the directory where you have .mp3 files) as,



./rename_song.sh


If you have a list of files like



song.mp3    song1.mp3    song2.mp3
song3.mp3 song4.mp3 song5.mp3


They will be renamed like this:



a song.mp3    g song1.mp3    k song2.mp3
r song3.mp3 P song4.mp3 A song5.mp3


If you want letter as well as number then replace tr -cd 'a-z' by tr -cd 'a-z0-9', and it is always better to use a simple shell glob instead of parsing the output of ls.


[#28075] Monday, March 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rryble

Total Points: 489
Total Questions: 121
Total Answers: 119

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
;