Rename files to random filenames (but not to checksums)
3
votes
4
answers
1571
views
Sometimes I need to batch rename files on a macOS system to random or pseudo-random filenames, eight lowercase letters and digits only, e.g. from
mcmurdo-station.png
to something like a12bc0xy.png
. For this, I use CRC32:
sh
for f in *.png; do mv $f $(crc32 $f); done
But there is a drawback that if there are two identical files in the same folder, they have same checksums, and so there will be a filename collision. Of course, usually I don't store identical files in the same folder, but I nevertheless would prefer to avoid such over-automatization while batch renaming.
Another way I have found on the internet is to use base64:
sh
randomname() { head -c8 /dev/urandom | base64 | tr -dc a-z0-9; }
zmv '(*).(*)' 'randomname
.$2'
>2024-05-22 edit: Instead of backquotes, [it is better to use a dollar sign](https://stackoverflow.com/q/9405478) :
>
>>zmv '*(.*)' '$(randomname)$2'
>
But the resulting filenames are of different lenght: the first test file was renamed from foo.png
to 52rud.png
, whereas the second - from bar.png
to pxg.png
.
There is also a solution using shuf
from GNU Core Utils: https://unix.stackexchange.com/a/259761 , but for now I would prefer to find a way that don't require installing third-party packages.
What is a good and simple alternative?
Asked by jsx97
(1347 rep)
May 19, 2024, 04:34 PM
Last activity: May 24, 2024, 07:28 AM
Last activity: May 24, 2024, 07:28 AM