Search code examples
renamebatch-processingsymlinkbatch-renameubuntu-17.10

Batch rename target of symlinks to name of symlink


I have a whole set of directory that is made of symlinks to other files (originally recorded in mythtv - symlinks created via mythlinks). My mythdatabase died but due to the mythlinks I can still find out which file is which.

I would like to (batch if possible) rename the target files of the symlinks to the name of the symlinks ie:

Mar 27 22:12 GreatFilm.mpg -> 123.mpg

Mar 27 22:12 GreaterFilm.mpg -> 456.mpg

so that the target files will be GreatFilm.mpg and GreaterFilm.mpg respectively.

can someone help with this?

thank you for your help

FYI on my ubuntu 17.10 the rename command does not have an option -s / --symlink


Solution

  • Using :

    find . -type l -name '*.mpg' -exec bash -c '
        d=$(readlink "$1")
        echo unlink "$1"
        echo mv "$d" "$1"
    ' -- {} \;
    

    Remove the 2 echo commands when the output looks good

    or using and :

    find . -type l -name '*.mpg' -exec bash -c '
        echo unlink "$1"
        rename -n 's/(.*)/readlink $1/e "$1"
    ' -- {} \;
    

    Remove the echo command and the -n when the output looks good