As an example, I have a directory with multiple files in this general format:
dir1/identifier1_desiredName1.m
dir1/identifier1_desiredName2
dir1/identifier1_desiredName3.m
dir1/identifier2_desiredName1.m
dir1/identifier2_desiredName2.m
dir1/identifier3_desiredName1.m
dir1/identifier3_desiredName2.m
dir1/identifier3_desiredName3
dir1/identifier4_desiredName1.m
dir1/identifier4_desiredName2.m
dir1/jabberwocky-mimsy-borogoves
dir1/other--should-not-be-moved
I'm trying to come up with a script that separates the files by the identifier by making a directory using that identifier, and then move files with the same identifier into that directory.
By the end of the moving, I would like to have something like:
dir1/identifier1/desiredName1.m
dir1/identifier1/desiredName2
dir1/identifier1/desiredName3.m
dir1/identifier2/desiredName1.m
dir1/identifier2/desiredName2.m
dir1/identifier3/desiredName1.m
dir1/identifier3/desiredName2.m
dir1/identifier3/desiredName3
dir1/identifier4/desiredName1.m
dir1/identifier4/desiredName2.m
dir1/jabberwocky-mimsy-borogoves
dir1/other--should-not-be-moved
As of right now, I think that I'm on the right track for the directory making:
awk _ {print $1} | uniq | mkdir
Syntax probably isn't quite correct, but the general idea is to print out the first column, separated by _
, omitting repeats, and then piping those names into mkdir
. But then I'm at a loss for moving the files into the directories. I was thinking about using grep
similarly (replacing mkdir
above and then piping into mv
), but I wasn't sure if it would work.
tree <your dir>
) – RomanPerekhrest Feb 06 '18 at 18:52identifier1
directory at the top level, or do you wantdir1/identifier1
anddir2/identifier1
? – G-Man Says 'Reinstate Monica' Feb 06 '18 at 20:16identifier_desiredName.m
is not a very clear way of saying that you wantidentifier_desiredName.m
to be renamed todesiredName.m
in theidentifier
directory. – G-Man Says 'Reinstate Monica' Feb 06 '18 at 20:22identifier_desiredName.m
instead ofidentifier_RestOfName.m
, they made francois P and me believe that they wanted the files renamed, and since they don’t show the result they want, they left it ambiguous. – G-Man Says 'Reinstate Monica' Feb 06 '18 at 20:36