I have a tree of folders. Some of them contain file named ".file.php" How do i rename all the files with this name recursively in the tree to ".file1.php"? I'm on debian server.
2 Answers
The full solution would look as follows
find -name '*.file.php' -exec rename 's/\.file\.php$/.file1.php/' {} \;

- 9,811
do you have rename
on debian? idk, but since it's available on ubuntu, I guess so.
NAME rename - renames multiple files
SYNOPSIS rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
DESCRIPTION "rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.
For example, to rename all files matching "*.bak" to strip the extension, you might say rename 's/\.bak$//' *.bak To translate uppercase names to lower, you'd use rename 'y/A-Z/a-z/' *
an example copied from a google result might be already quite near to what you want:
Here is an example of the rename command:
rename -n ’s/.htm$/.html/’ *.htm
The -n means that it's a test run and will not actually change any files. It will show you a list of files that would be renamed if you removed the -n. In the case above, it will convert all files in the current directory from a file extension of .htm to .html.
If the output of the above test run looked ok then you could run the final version:
rename -v ’s/.htm$/.html/’ *.htm
The -v is optional, but it's a good idea to include it because it is the only record you will have of changes that were made by the rename command....

- 3,108
-
1how do i do it recursively in a tree of folders? – die_humans Apr 05 '11 at 12:21
-
1Ubuntu is debian – Reuben Swartz Apr 05 '11 at 12:53
-
@die_humans: Look here http://ubuntuforums.org/showthread.php?p=6642616 for one way to do it. another way would be a shell script. – Christoph Apr 05 '11 at 13:23