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. )

7 Answers 7

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 ':' '_' {} +
6
  • +1 for the preview. A solution to rename the file by simply removing the colon would be great. Jul 27, 2009 at 11:25
  • For the rename command I get syntax error at (eval 1) line 1, near ":" Jul 27, 2009 at 12:25
  • What version of rename do you have? It was tested from util-linux-ng 2.14.1.
    – Dan Carley
    Jul 27, 2009 at 12:41
  • It appears to be 1.5 (according to the comments in the file). I think the version number you're quoting is for the package, not the rename file itself. Jul 27, 2009 at 22:34
  • 2
    There's a different rename that takes a perl regex. so you'd want -exec rename 's/:/_/g' {} I think Redhat based distributions used one, and debian the other. Nov 12, 2009 at 16:17
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.

0
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

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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .