Why blanks and leading dashes in filenames are both “Donts”

The generation of contour lines causes some unexpected troubles. After hitting the 60th latitude, it produces empty tiles only. I have no clue yet why.

Even worse, the resulting osm files are spread over several old hard drives. I wanted to copy them over to one single drive I just bought for this purpose.

Surprisingly, I was not able to copy the files from within the KDE filemanager. The main reason for this is the weird file naming, which reads something like -90 -180 -89 -179.osm and so forth. mv now has to cope with two issues: the blanks in the filenames and the leading dashes. This means that a simple mv *.osm ../SomeOtherDir won’t work. I then started an oddyssey through the bowels of the operating system, just for moving some (well, admittedly some thousands) files.

I’d like to encourage anyone to be reluctant with such filenames, so you never are in need of the tricks listed below:

  • find . -name *.osm -print0 | xargs -0 mv {} ../SomeDestinationDirectory
    This one would work if there were no leading dashes in the filenames. Unfortunately there are, and mv interprets them as switches and is reporting “To many parameters”
  • You can try to set a leading ./ to mask the leading dashes
  • You can try to use find with its exec option. It’s a bit inefficient, as it will create one process for each file found. Plus it cannot cope with the leading dashes anyway.
  • Even dirtier, there’s a environment variable called IFS (internal field separator). It can be redefined to use line ends but breaks: IFS=$' '. As other applications rely on this variable as well, do not forget to unset it correctly after you are finished.
  • I thought I’ll try it on the Mac instead. I plugged the drive in. I was able to move one single file successfully in the finder. That encouraged me to move all files to another folder. The Mac started to do the job and even moved some files. Suddenly, the drive disappeared from the system, triggering two alerts of the operating system. As it is late, I’ll try to recover it tomorrow.

Conclusion: If possible, avoid such characters in your file names. If you have to cope with them, do not struggle with the shell. Better use a scripting language such as Perl, Python or Ruby to move your files. Yes, exactly. You have to write a script to move some weirdly named files. That’s it.