Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/56648862739d597cd0b1 to your computer and use it in GitHub Desktop.
Save ormaaj/56648862739d597cd0b1 to your computer and use it in GitHub Desktop.
Directory merge / rename example
#!/usr/bin/env ksh
# Example for merging directories of files with possibly
# overlapping names into a single directory.
mkdir a b c new
touch a/{0..5} b/{3..7} c/{4..10}
echo before: */*
typeset -A files
for x in ~(N)./*/*; do
files[${x##*/}]+=("${x}")
done
for x in "${!files[@]}"; do
n=0
for y in "${files[$x][@]}"; do
mv -f -- "$y" "new/${y##*/}.$((n++))"
done
done
echo after: new/*
before: a/0 a/1 a/2 a/3 a/4 a/5 b/3 b/4 b/5 b/6 b/7 c/10 c/4 c/5 c/6 c/7 c/8 c/9
after: new/0.0 new/1.0 new/10.0 new/2.0 new/3.0 new/3.1 new/4.0 new/4.1 new/4.2 new/5.0 new/5.1 new/5.2 new/6.0 new/6.1 new/7.0 new/7.1 new/8.0 new/9.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment