Find files, rename to unix bash place

This should be relatively trivial, but I tried for a while without much luck. I have a directory with many directories, each with its own structure and files.

I search all .java files in any directory under the working directory and rename them to a specific name. For example, I would like to name all java files "test.java".

If the directory structure:

./files/abc/src/abc.java ./files/eee/src/foo.java ./files/roo/src/jam.java 

I want to just rename to:

 ./files/abc/src/test.java ./files/eee/src/test.java ./files/roo/src/test.java 

Of course, part of my problem is that paths can have spaces. I don't need to worry about renaming classes or anything inside the file, just the file name in place.

If the directory contains more than 1.java file, I do not mind if it is overwritten or instructed to choose what to do (either this is normal, it is unlikely that it contains more than 1 each directory.

What I tried:

Peering at mv and find, but when I connect them, I seem to be doing it wrong, I want to be sure to keep them in my current location and rename, not move.

Thank you, I really appreciate any help.

+8
unix bash rename mv
source share
2 answers

The GNU find version has the -execdir action, which changes the directory wherever the file is located.

 find . -name '*.java' -execdir mv {} test.java \; 

If your version of find does not support -execdir , you can do this work with:

 find . -name '*.java' -exec bash -c 'mv "$1" "${1%/*}"/test.java' -- {} \; 
+21
source share

If your find (like mine) does not support -execdir , try the following:

 find . -name "*.java" -exec bash -c 'mv "{}" "$(dirname "{}")"/test.java' \; 
+5
source share

All Articles