I'm trying to move files like this:
...
DLG023-00-01-sprite.TextGrid.json
DLG023-00-01.prosody.json
DLG023-01-01-sprite.ogg
DLG023-00-02-sprite.TextGrid.json
DLG023-00-02.prosody.json
DLG023-01-02-sprite.ogg
...
DLG028-00-01-sprite.TextGrid.json
DLG028-00-01.prosody.json
DLG028-01-01-sprite.ogg
...
into their respective folders, e.g.: move the file to dialogue023/
when it starts with DLG023
; to dialogue024/
when it starts with DLG024
; and so on.
All of the files and directories are at the same level.
In other words, I'm trying to produce commands like this one:
mv DLG023-00-01-sprite.ogg dialogue023/
How can I extract the first number from the filename?
So far I've got this:
for i in DLG*-*-*-sprite.ogg; do echo "${i//[^0-9]}";done
But this outputs all of the digits (e.g.: 230001) when I only want the first one (e.g.: 23).
for num in $(seq -f '%03g' 1 999); do test -d dialogue${num}||mkdir dialogue${num}; mv -t dialogue${num} DLG${num}-*; done
followed byrmdir dialogue*
-- the-f '%03g'
means all numbers are zero-padded to at least three digits, and since 999 fits into three digits all numbers will become three digits long in the directory names. Plainrmdir
fails if the directory is not empty, so is safe. Brace for unimportant error output frommv
about nonexistent files.