Hello, I'm trying to create a script that sorts documents by author (found in file name) and then create a directory for that author. For example,
Input:
John - Paper_1.txt
John - Paper_2.txt
Mark - Paper_1.txt
Jill - Paper_1.txt
Output:
dir/John/Paper_1.txt
dir/John/Paper_2.txt
dir/Mark/Paper_1.txt
dir/Jill/Paper_1.txt
I have the following code, but the directories are named very strangely (possibly because of the sort feature I used).
for file in *.txt
do
dir=`find *txt | cut -d ' ' -f 1 | sort -u`
mkdir -p "$dir"
mv "$file" "$dir"
done
Right now, the code creates directories with the following names:
dir/John Mark Jill/Paper_1.txt
dir/John Mark Jill/Paper_2.txt
dir/Mark Jill/Paper_1.txt
dir/Jill/Paper_1.txt
Can anyone help?
---------- Post updated at 07:33 PM ---------- Previous update was at 07:02 PM ----------
As another route, I know that I can create the directories first, by using
mkdir $(ls *txt | cut -d - -f1 | sort -u)
but then the issue of sorting the files arises.
Any suggestions?