Skip to main content
2 of 3
added 8 characters in body
muru
  • 78.1k
  • 16
  • 213
  • 319

While the general problem of identifying extensions is hard, you can clean up the script a bit:

  1. Tell find to only consider files with an extension: -iname '*.*'
  2. Use awk instead of cutting yourself:
  3. Use a script, and then tell find to exec that script.

Thus: a script called, say, move.sh:

#! /bin/bash
for i
do
    ext=/some/where/else/$(awk -F. '{print $NF}' <<<"$i")
    mkdir -p "$ext"
    mv "$i" "$ext"
done

Then run find thus:

find . -name '*.*' -type f -exec move.sh {} +

This has the problem that you can't rearrange within the folder, so you could use xargs:

find . -name '*.*' -type f -print0 > /tmp/temp
xargs -0 move.sh < /tmp/tmp
muru
  • 78.1k
  • 16
  • 213
  • 319