As a follow up to the previous post, it turns out renaming files without perl rename can be a bit of a hazard. I needed to find some set of files in a directories This or That, and move the word "Foo" from the beginning of the file name to the end of the file name. Luckily, between Stu, some man pages, and some sed one-liners I was able to come up with the following one-liner that did exactly what I wanted:
find . -name "Foo*.php" | egrep "(This|That)" | while read line; do echo "mv $line $(sed 's/\(.*\)Foo\(.*\)\.php/\1\Foo\.php/' <<< $line)"; done | sh
- Find all files whose name starts with "Foo"
- Filter for only paths that contain "This" or "That" - note that this could possibly be done using a find -regex but it wasn't cooperating with the OR statement so I used egrep
- Pipe the list of file paths to an in-line shell script that reads each path one at a time
- Construct a mv command consisting of "mv", the initial path, and then use sed to manipulate the initial path into the destination path
- Echo the command
- Pipe to shell
Note: it's recommended to review the echo'd commands before piping to shell, although if you're in a version controlled environment like git this is obviously less of an issue :)