3

I have a bunch (about 10 gigs worth) of files that I need to copy from an external linux disk to a Windows machine. Unfortunately some file-names that contain the ":" character have got into this collection.

None of these colon files needs to be on the windows machine so I need a quick solution to zap or rename them.

What would you all recommend? (I'm assuming something like a bash / perl / python script in Linux. We don't have powershell on the Windows machine. )

Brad Gilbert
  • 2,473
  • 2
  • 21
  • 19
interstar
  • 1,231
  • 4
  • 18
  • 23

7 Answers7

16

Review the offending files.

find /path/to/files -name '*:*' -print

Delete the offending files.

find /path/to/files -name '*:*' -exec rm {} +

Rename the offending files with an underscore.

find /path/to/files -name '*:*' -exec rename ':' '_' {} +
Dan Carley
  • 25,189
  • 5
  • 52
  • 70
5

For a more efficient version of Dan C's delete some UNIX variants support:

find /path/to/files -name '*:*' -delete

this avoids the need to fork and exec /bin/rm for every single matching file.

This -delete option is present on MacOS X and on my FC11 system (with findutils-4.4.0). I don't know how long ago it was added to findutils.

Alnitak
  • 20,901
  • 3
  • 48
  • 81
0

You can delete all the files by doing

rm *:*

in the directory on the Linux box.

That should delete all files containing the colon.

Or you can rename them using the rename command

rename 'y/(.*):(.*)/$1$2/' *

That will replace test:something to be testsomething

Mark Davidson
  • 395
  • 4
  • 11
0

The easiest way may be to zip the whole structure, and unzip the file to the Windows disk. Unzip knows how to remap illegal characters.

Requires you have the disk space though :(

0

If you want to change the filenames in flight (not modifying the source directory), GNU tar has a --transform option.

tar cf - -C $SOURCE_DIR . --transform=s/:/_/g | tar xf - -C $DEST_DIR

You could also use the --exclude option to avoid those files.

tar cf - -C $SOURCE_DIR . --exclude='*:*' | tar xf - -C $DEST_DIR

I don't see the --transform option on the RHEL5 machines where I work, but I think it's pretty common otherwise.

0

Also,

mmv ";*:*" "#1#2#3"

or a similar command should do the trick as well. Run it with -nv first, just to see what will be moved to what, without actually modifying anything

dmityugov
  • 756
  • 4
  • 5
0

This should really be a comment on the most-chosen answer above, but I can't (yet) apply a comment.

I have this version of rename on a Debian jessie system.

$ rename -V
/usr/bin/rename using File::Rename version 0.20

I was successful at renaming by using:

find . -name '*:*' -exec rename 's/:/_/g' {} \;

However, there is a side effect. The timestamps on all the files changed.

Alan Carwile
  • 111
  • 2