# Ubuntu – Change directory structure from /YEAR/MONTH/DAY/ to /YEAR-MONTH-DAY

bashbatch-renamecommand linedirectoryregex

I need to change my directory structure for my photos from /YEAR/MONTH/DAY/ to /YEAR-MONTH-DAY

I know this can sort of be done with exiftool, but it will only work on files containing EXIF-tags, and I have xmp-sidecar files corresponding to each image, and these need come along too. So I recon script of some kind would be the best way.

I sat down and tried to learn RegEx, sed, bash and what not, and given enough time, I guess I should be able to figure this one out, but right now I am in a hurry, so any help would be appreciated.

//Ola

The rename utility in Ubuntu can rename directory structures but it won't clean up after itself.

rename 's#(.+)/(.+)/(.+)#$1-$2-$3#' */*/*/  Stick -vn on the end if you just want it to tell you what it's going to do before it renames anything, but here's a little test harness that shows you what's possible: $ mkdir -p 2014/06/15
$touch 2014/06/15/photo_{001..003}.jpg$ tree
.
└── 2014
└── 06
└── 15
├── photo_001.jpg
├── photo_002.jpg
└── photo_003.jpg

$rename 's#(.+)/(.+)/(.+)#$1-$2-$3#' */*/*/
\$ tree
.
├── 2014
│   └── 06
└── 2014-06-15
├── photo_001.jpg
├── photo_002.jpg
└── photo_003.jpg


Simply put it's being fed the third-level directories and renaming is reading the earlier two segments and renaming it, sticking it in the current directory. As you can see there will be a load of year directories. Assuming they're empty you could clean up with something like (and I pray you check they're empty first):

find -maxdepth 1 -type d -regex '\./[0-9][0-9][0-9][0-9]' -exec rm -irf "{}" \;


I'm using the -i option to force it to ask you before deleting every file. Remove that at your own risk.