1

Maybe some one have tool script (perl or shell script) that rename files names or directory names?

For example

The tool needs to read from the file as the following:

   more file

   /var         Local      exportlocal
   /etc        data         Data
  1. The first field $1 – directory name to search under this directory

  2. Second field – the name of the file/dir that need to search

  3. Third field – the name of the new file/dir that need to rename

Real example (before running the tool)

    /var/tmp/Local

Real example (after we run the tool)

    /var/tmp/exportlocal
0

Assuming that the file is whitespace-delimited (tabs and/or spaces between the fields and newlines ending each record) and that the file and directory names don't contain those characters:

#!/bin/sh
while read -r branch oldname newname
do
    find "$branch" -depth -name "$oldname" -exec sh -c 'mv "$1" "${1%/*}/$2"' _  {} "$newname" \;
done < file-with-names

Edit:

Change the find command as follows to output a message for each file that is renamed:

    find "$branch" -depth -name "$oldname" -exec sh -c 'mv "$1" "${1%/*}/$2"; echo "$1 was renamed to $2 under $3"' _  {} "$newname" "$branch" \;
  • hi Dennis thanks for your great support , I wander if it possible to add message to the user each time that file/dir was update (example - by echo "$oldname was update under directory: /dir1/dir2/dir3...) – klod Jan 24 '11 at 16:02
  • @klod: Please see my edited answer. – Paused until further notice. Jan 24 '11 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.