Score:0

How to shuffle files and rename sequentially

nz flag

I have a set of .png images, currently named in sequence from 1 to 1000. How can I take these images and reassign them to a random number between 1 & 1000 so I end up with the same range of sequential numbers, but the image under each number is no longer the same?

David avatar
cn flag
Looks a lot like a homework task.
pLumo avatar
in flag
Interesting task nevertheless ;-)
Score:0
in flag

Could be done like this:

names=({1..1000}.png)
new_names=($(shuf -e {1..1000}.png.tmp))
for n in "${!names[@]}"; do
    echo mv "${names[$n]}" "${new_names[$n]}"
done && rename -n 's/\.tmp$//' *.tmp
  • Create two arrays $names and $new_names
  • we need to name it .tmp or so to not overwrite existing files, we'll fix this with rename.
  • loop over array keys and run mv for each.

Note: To actually run the command, remove the echo and -n from rename.

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.