Skip to content

Instantly share code, notes, and snippets.

@arielallon
Created August 25, 2014 21:04
Show Gist options
  • Select an option

  • Save arielallon/760c86ee40ad6f0fe1db to your computer and use it in GitHub Desktop.

Select an option

Save arielallon/760c86ee40ad6f0fe1db to your computer and use it in GitHub Desktop.
Move files into folders by extension
#!/bin/bash
recup_dir="${1%/}"
echo "$recup_dir";
[ -d "$recup_dir" ] || {
echo "Usage: $0 recup_dir";
echo "Mirror files from recup_dir into recup_dir.by_ext, organized by extension";
exit 1
};
find "$recup_dir" -type f | while read k; do
ext="${k##*.}";
ext_dir="$ext";
fn=`basename "$k" "$ext"`;
pre=${fn:0:3};
[ -d "$ext_dir" ] || mkdir -p "$ext_dir";
[ -d "$ext_dir/$pre" ] || mkdir -p "$ext_dir/$pre";
echo "${k%/*}"
mv "$k" "$ext_dir/$pre/";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment