I have 1000s of files in a single directory that I want to sort into subdirectories based on their filenames. They're all consistently named with a set structure of p-[number]_n-[number]_a-[number].[ext].
Here's a small sample...
What I'm after is a folder structure like this:
p-12345 ⊢ n-987 ⊢ p-12345_n-987_a-1254.jpg ⊢ p-12345_n-987_a-9856.pdf ⊢ p-12345_n-987_a-926.docx ⊢ n-384 ⊢ p-12345_n-384_a-583.pdf ⊢ p-12345_n-384_a-987.pdf p-2089 ⊢ n-2983 ⊢ p-2089_n-2983_a-2348.gif ⊢ n-1982 ⊢ p-2089_n-1982_a-403.jpeg p-38422 ⊢ n-2311 ⊢ p-38422_n-2311_a-126.pdf ⊢ p-38422_n-2311_a-5231.docx
I hope that makes sense.
Is it possible to write a script to organise the file in this way?
EDIT: To clarify: Yes, my question should be how can I write a script to organise the files? :) I'm very new to Unix and the command line in general. So far I've only written/used basic shell scripts. I have a hunch that the answer will probably involve regular expressions but beyond that I'm not really sure where to start.
The best idea I've come up with is to
- Export the file list to a text file
- Find and replace "_n" and "_a" with "/n" and "/a"
- Create a series of mv commands from that
- Save it as a shell script
I'm sure that's far more long-winded than it needs to be though. I'd also like to have something repeatable in case I need to do it for more files in future.