Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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" \;
share|improve this answer
    
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. –  Dennis Williamson Jan 24 '11 at 16:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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