Skip to content

Instantly share code, notes, and snippets.

@thedod
Last active December 27, 2015 21:59
Show Gist options
  • Save thedod/7395513 to your computer and use it in GitHub Desktop.
Save thedod/7395513 to your computer and use it in GitHub Desktop.
Weird folder renamer (it's for a friend :) )

Put renamer.sh and renamer-aux.sh in a folder on your executable path (note that they should both be in the same folder).

Now cd somewhere and run renamer.sh, you'll get a series of mv commands to rename each folder from SOMEFOLDER/ to somefile-AT-somefolder/ where somefile is the name of the first file under somefolder/ (according to filename sort order).

Why would you need this? Beats me. A friend asked for it.

Note: Order does matter. The output you get from renamer.sh may contain (e.g.):

mv "/a/b/c/" "/a/b/something-AT-c"
mv /a/b/" "/a/aomething-AT-b"
mv "/a/" "/something-AT-a"

Doing them out of sequence would break - of course.

#!/bin/sh
cd "$1"
echo "mv \"$1/\" \"$1/../$(find . -type f -not -iname '.*' | sed -e 's/^.*\///' -e '/^\./d' |head -1 )-AT-$(basename "$1")\""
#!/bin/sh
find -type d -not -iname '.*' -exec "$(dirname $0)/renamer-aux.sh" '{}' \;|LC_ALL=C sort -k5 -t\" -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment