Skip to content

Instantly share code, notes, and snippets.

@baptx
Created October 12, 2014 14:05
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 baptx/4306aa7e4fbc19417243 to your computer and use it in GitHub Desktop.
Save baptx/4306aa7e4fbc19417243 to your computer and use it in GitHub Desktop.
Copy folder by filename
IFS=$'\n'
if [ $# == 2 ]; then
if [ ! -d $1 ]; then
echo "Error: directory $1 does not exist"
else
dir=$1
len=$((${#dir} + 1))
if [ ! -d $2 ]; then
mkdir $2
fi
for i in `ls -R $1`
do
src=`echo $i | cut -d : -f 1`
if [ -d $src ]; then
dir=$src
fi
dest=$2/`echo $dir | cut -c $len-`
if [ ! -d $dest ]; then
mkdir $dest
fi
if [ -f $dir/$i ]; then
cp -v $dir/$i $dest
fi
done
fi
else
echo "On some MP3 player, the play order depends on wich files are copied first.
Folder files will be copied alphabetically by filename, subfolders included.
Usage: $0 source_directory destination_directory"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment