Reputation: 107
I have found examples of similar questions sorting files into folders based on the filename but don't really know where to start with this as i need to loop through multiple things and could do with some help please.
I have a folder of telephone recordings with the following file name format:
OUT260-20140828-125114-1409226674.42655.wav
it is split up into several parts
OUT260 = 260 is the name of the extension
20140828 = the date
125114 = the time
the rest im not sure I think it's some king of id because it increments each time.
what i want to end up with is the following folder stucture created by a cron job / bash script
top level
extension numbers eg
260, 261 ,262
then year then month then day then multiple files eg for the file name above 260-1251.wav
so if this can be achieved the example file would end up as below
/Recordings/260/2014/08/28/260-1251.wav
i have the following example which is a more basic example:
#!/bin/bash
for full_filename in *jpg; do
extension="${full_filename##*.}"
filename="${full_filename%.*}"
mkdir -p "$filename/picture"
mv "$full_filename" "$filename/picture"
done
Hope that makes sense any help much appreciated
Upvotes: 1
Views: 936
Reputation: 476594
A quick and dirty way to do this is using sed
to replace the name by the path:
#!/bin/bash
for f in OUT*.jpg
do
res=$(echo "$f" | sed -r 's#^OUT([0-9]+)\-([0-9]{4})([0-9]{2})([0-9]{2})\-([0-9]{4}).*\.(.*)$#/Recordings/\1/\2/\3/\4/\1-\5.\6#g')
mkdir -p `dirname "$res"`
mv "$f" "$res"
done
For example if $f
is OUT260-20140828-125114-1409226674.42655.wav
, $res
is /Recordings/260/2014/08/28/260-1251.wav
. For filenames that don't follow the correct pattern, $res
is the same as $f
so nothing happens.
sed
makes use of regular expression to find and replace (parts of) the filename. The syntax is somehow hard to explain but there are a lot of good tutorials on the internet.
Furthermore please note that it is in general not a good idea to use a root dir like `/Recordings``...
Please use cp
first for testing...
Upvotes: 1