If I understand the .bat syntax correctly, this should be basically equivalent:
#!/bin/sh
for a in *
do
[ ! -f "$a" ] && continue # skip anything that is not a regular file
dirname="${a%.*}" # trim off the last suffix
mkdir -p "$dirname" && mv -i "$a" "$dirname/"
done
Like the original, this script will process the files within the current directory only. If you need to process the current directory and all its pre-existing sub-directories (that's how I would understand "recursively") it gets a bit more complex, as the script would have to proceed in a depth-first manner to avoid trying to push files into infinitely deep sub-sub-sub... directories.
In that situation, a different solution would need to be developed, most likely using the find
command to gather the filenames. The find
command is recursive by default: it will automatically process the specified directory and all its sub-directories, unless you specifically tell it not to. So here's a recursive version that can handle an arbitrary number of files in any directory:
#!/bin/sh
# FIXME: needs more protection against unusual filename characters
find . -type f -depth | while read a
do
dirname="${a%.*}" # trim off the last suffix
mkdir -p "$dirname" && mv -i "$a" "$dirname/"
done
It has a slight problem, though: if a filename includes a line-feed character, it might not process that filename correctly. But if your files only have well-behaved characters in their names, this should be enough.