Artificial intelligent assistant

Copying Files in a Folder To Individual Folders This might sound like something quite odd but for my HTPC, I need to rearrange how my files are stored such that each of the individual files in a folder needs to be moved to a subdirectory of its own (with the same name as the file). I.E. - Currently the directory is: directory/ - a.file - b.file - c.file and I want to change it to: directory/ - a/ -- a.file - b/ -- b.file - c/ -- c.file I don't think this is terribly tricky but it's just odd and I haven't found anybody doing it before: I think it can be done with find and exec but I'm a bit of a battler and can't quite get it. Thank you very much in advance (or even just for taking the time to read this).

Write a shell script which gets the files as parameters:


#!/bin/bash

for filepath; do
parentpath="${filepath%/*}"
if [ "$parentpath" != "$filepath" ]; then # $file contains a /
file="${filepath#"$parentpath"/}"
else
file="$filepath"
parentpath=""
fi
filename="${file%.*}"
if [ -z "$parentpath" ]; then
mkdir -p "$filename" && mv "$file" "$filename"
else
mkdir -p "${parentpath}/$filename" &&
mv "$filepath" "${parentpath}/$filename"
fi
done


You can call this script as `./script *.ext1 *.ext2 *.ext3` for not too huge numbers of files or as `find ... -exec ./script {} +` for huge numbers.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 03e80d87803f1a403a6eadfaf7693ec2