[clug] renaming files to make windows happy

steve jenkin sjenkin at canb.auug.org.au
Thu Apr 23 13:34:18 GMT 2009

Hal Ashburner wrote on 23/4/09 6:22 PM:

> Anyone got any other tricks for checking large scripted operations where
> discovering the mistake late, would be pretty painful?

Not sure how those scripts handle symlinks and device files/pipes/sockets.

Use hard links and retain the original files in all their
glory/goriness. Only uses a small amount of space per filename.

Means you can rerun & tune your algorithm as you desire.
Even keep multiple copies of the tree and see how Samba etc deals with them.
I get nervous making irreversible changes :-/

'ln' will return an error if it didn't work
[so does 'mkdir' if you are doing the same work with dirs]


if [ ln $old $newpath/$new ]; then :
else echo Houston, we have a problem

A note on some other techniques.
You probably have a whole directory tree to process.
Which says you'll be wanting 'find orig-dir | while read file...'

Useful Options for 'find':
-print0 => handle white space etc. But not sure how 'while read' goes
-depth  => descend tree first, needed if you are creating directories
-type d => select only directories
-type f => select only plain files.

A simple way to package things is:
find orig-dir -type f -print0 |xargs -0 do-stuff-script

And you have to write your script...
Remember in bash to use "$@" not "$*" to save original white space
as you iterate over the command line params.

It's much nicer to do this, whitespace names permitting (modulo dirs):

find orig-dir -type f|\
 awk '{f=$1; gsub(/[:,$]/, "_", f); print $1, "newdir/" f}'|\
 xargs -L 1 ln

[[substitute sed, perl etc as you prefer for awk]]

When I have just a few (<100) files, I use 'find' to create a list and
capture it in a file. Then edit the file into a shell script.
duplicate the file names, make the destination look how I want, add 'mv'
or 'cp' or 'ln' to the start of the file.

Then 'sh file' or some such to run it... Not nice, but I'm more
comfortable with my text editor than most other tools.
And I can make arbitrary changes :-)


Steve Jenkin, Info Tech, Systems and Design Specialist.
0412 786 915 (+61 412 786 915)
PO Box 48, Kippax ACT 2615, AUSTRALIA

sjenkin at canb.auug.org.au http://members.tip.net.au/~sjenkin

More information about the linux mailing list