Skip to content
🎉 Welcome! Threatbear can now offer managed detection and response services for 24x7x365 coverage!
Renaming multiple files

Renaming multiple files

February 27, 2019

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 :

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

This uses a small bash function

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

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

245d2.json    5b8a9.json    7dcd6.json    a3401.json    cc96b.json    fb629.json
2614b.json    5ba41.json    7f4b2.json    a3a75.json    ce100.json    fb790.json
267a2.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