Moving multiple files

One common requirement on *nix systems is to rename (or move-and-rename) more than one file, matching a wildcard. However, if you try to do this:

mv ACK*.xml /code/xml/ACK*.xml.done

it wont work for more than one file. By the way, this is one of those odd places where Windows command line works better!

How to work around this? As below:

ls ACK*.xml | sed -e 's/.*/mv & \/code\/xml\/&.done/' | sh

I have already talked about sed, sh and this style of scripting: it can work also on Windows. In the target path all slashes need to be escaped like this: \/ since its being used inside a sed script. Of course, this was easy because we had to suffix something to the filename – had we needed to add something in the middle, it would have been more difficult (and a bigger sed script would be needed).

Here is a more ‘capable’ script: it takes two parameters, first one the path to the source directory, and next one the path to the target directory. It moves ACK*.xml files from source to target/.processed (.processed folder under target) renaming them with the current datetime stamp.

Customise it to suit your needs.


Licensing and information about the blog available here.