sábado, 26 de septiembre de 2009

Replace on file with another in other directory in linux

Story

It's a long story, the thing is that my documents filesystem became corrupted and there were a lot of song that were not able to be reproduced.
I didn't know which songs were until I played them so do it by hand was not practical.
I get to create to scripts that allow me to replace all my favorites songs in my music folder with a backup of the song in a different folder.
There were some restrictions
  • The songs had to have the same name
  • The song to be replaced and the copy can be in diferent folders
The scripts

the solution is composed of two parts, a command that I run in console and a script, the command is

find -type f -exec ./replaceFiles "{}" \;

the script with name replaceFiles is

cp "$(find /another/directory/ | grep $basename "$1")" "$1" -v

How they work

When you first execute the command, for every file in the directory it is runned in it calls the script replaceFiles passing as parameter a file in the directory, the script search for the file within another directory by its name (basename "$1"), if it founds it then it copies over the original file "$1".

No hay comentarios:

Datos personales