Skip to content
Advertisement

Using find to rename files recursively with random chars

I have an IP camera that takes snapshots and nests those snapshots into multiple directories. The sub directories look something like this.

/cam_folder
   |--Date
   |----Hour
   |------Minute
   |-------->file1
   |-------->file2...etc
   |------Minute
   |-------->file1...etc

There is a ton of sub directories because of the way it stores files since it places those snapshots within a Minute directory of the Date/Hour directories.

At any rate, there are other types of files mixed in, but I know how to use find to find all the .jpgs I need:

find /cam_folder/ -type f -name '*.jpg'

But what I need to do is rename all the .jpg files to random characters. I was able to find this which works from a single directory in a bash script:

for file in *.jpg; do
    new_file="$(mktemp XXXXXXXX.jpg)"
    mv -f -- "$file" "$new_file"
done

My problem is how to tie these together? I need to use find to feed these into a bash script I guess?

Is there an easier way to just walk a directory recursively renaming as I go?

Advertisement

Answer

find /cam_folder/ -type f -name '*.jpg' -exec sh -c '
for f; do
  mv -f -- "$f" "${f%/*}/$(mktemp -u XXXXXXXX.jpg)"
done' _ {} +

find
Shell Command Language § The for Loop
Shell Command Language § Parameter Expansions

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement