Tuesday, January 15, 2008

Mass Copy and On-The-Fly Rename

I call this: Mass CPR. It's not the one that involves frenching chicks. This is about copying files & directories from one location to another, and renaming them on the fly, so that they reach the destination with a different name, applying the renaming according to a pattern.A co-worker had an external hard disk, that she put on her Mac to put 20GB of pictures. Later on, she wanted to view the pictures from her Windows laptop, which didn't see the HFS+ filesystem. Luckily, I found another HFS+ formatted disk in the Marketing department, where I copied the files & directories.The problem was that she had put colons in the file & directory names, as separators, instead of hyphens. FAT filesystems do not allow colons in file or directory names. To further complicate things, most of the files & directories were named in Japanese, and there was no temporary space to dump the data (other than the marketing disk). Since my laptop, is the only one with Linux installed, I was the only one capable of seeing the FAT & HFS+ partitions.
I started by writing a small PHP script (in CLI), which only worked on a small segment, and wasn't capable of doing recursions. The hunt for a program to do the dirty work began, and let me tell you this: It was NOT easy!!!After struggling for 4 days, I decided to ask the guys at #linux (on DALnet) and one of the OPs helped and gave me the following line:
pax -rw -s ',:,-,g' src dst
This magnificent line will copy, and replace any colon with a hyphen, all files & directories recursively, from the original directory, named src, to the target directory, named dst.
There was another thought of creating symbolic links to the original files, rename the links, and copy the links to the target disk, with the option to follow the symlinks. I'm not positive if that would've worked as intended, as pax did its job wonderfully. If you want to poke around with this idea, take a look at cpio.

1 comment:

Fares Al-Osaimi said...

good to know the pax command, much better than cp :)