vendredi 30 octobre 2015

Ubuntu 14.04 recursive file rename individual files appending directory name to filename

On Ubuntu 14.04 I have files that are in different sub-directories but have same filename. i.e.:

dirA/file01.jpg, dirA/file02.jpg, dirA/file03.jpg

dirB/file01.jpg, dirB/file02.jpg, dirB/file03.jpg

dirC/file01.jpg, dirC/file02.jpg, dirC/file03.jpg

I want to copy all the files (about 1000) contained in different sub-directories (about 200) recursively into a separate single directory (/home/user/temp/).

Since they have identical filenames I want to rename all 1000 individual files appending their respective directories to the filename, thus making them unique, like this:

/home/user/temp/dirA-01.jpg,
/home/user/temp/dirA-02.jpg,
/home/user/temp/dirA-03.jpg

/home/user/temp/dirB-01.jpg,
/home/user/temp/dirB-02.jpg,
/home/user/temp/dirB-03.jpg,

/home/user/temp/dirC-01.jpg
/home/user/temp/dirC-02.jpg
/home/user/temp/dirC-03.jpg

All (1000) files should be stored in the same final directory (home/user/temp).

I tried using ls, then find, then rename, then mv, then cp with too many problems to list. If my PERL was up to scratch I would have nailed this in a minute but it's been 15 years since I've used it... sob sob... Can anyone help with something snappy and not too complicated? Your help is very much appreciated.

Aucun commentaire:

Enregistrer un commentaire