Threatbear Logo
engineering

Renaming multiple files

Date Published

When trying to rename multiple files in a directory tree that have the same name — such as Azure signin logs that are written to Azure storage accounts — if you are like me then your first thought would be to use the ```find . -name *.json -exec mv {};``` pattern.


The problem is that find executes only one mv here and not one for each file as you would assume. To achieve this we need the help of our venerable bash shell :

1for file in $(find /olddata -name "*.json")
2do
3cp $file /data/`get_random`.json
4done
5

This uses a small bash function

1function get_random() {
2 cat /dev/urandom | dd count=1 bs=4 2> /dev/null | openssl dgst | cut -c 1-5
3}

After this you will have a single directory containing all the files from the initial directory tree, neatly and individually renamed


1245d2.json 5b8a9.json 7dcd6.json a3401.json cc96b.json fb629.json
22614b.json 5ba41.json 7f4b2.json a3a75.json ce100.json fb790.json
3267a2.json 5bad8.json 7f617.json a3b8a.json ce77b.json fba4d.json

Next step is how to merge these files into a single JSON file using jq