0

We have an issue where a set of data that was migrated from an old DAS drive which was attached to a Mac Mini acting as a server contains many illegal folder names (namely colons :). Some of the users must have created these and although the NAS allowed me to migrate them and they show up ok from the Synology's in built file browser, when accessing them from a Windows or Mac based client the folder names have been replaced with a auto-generated unique folder name.

I'm no Linux expert by any stretch of the imagination so I wonder if somebody could please help me out with a script that will remove the : (colon) from these folders so their Mac based machines are able to see them?

1

2 Answers 2

0

If it's just the : that is causing trouble, you could use find with rename option to replace that character with something else.

find /path/to/share -name "*:*" -exec rename 's/\:/_/' {} \;

Note that I haven't tested this, so try it on sample data first.

2
  • This should use + instead of \;, so that it only executes rename once per (ARG_MAX - env size) characters worth of filenames, instead of once per filename. Alternatively, instead of -exec rename ..., use -print0 and pipe find's output into rename -0 's/:/_/g' (but note that some old versions of perl rename don't support the -0 option, read the man page or run rename --help).
    – cas
    Commented Sep 25, 2022 at 13:18
  • BTW, rename won't replace existing files unless you force it to with -f or --force ... so if rename outputs any old_filename not renamed: new_filename already exists warnings, you can just run it again with s/:/__/g (and again with s/:/___/g, and so on)
    – cas
    Commented Sep 25, 2022 at 13:28
0

If bash is available on that system:

LC_ALL=C find /path/to/share -depth -name '*:*' -type d -exec bash -c '
  for file do
    base=${file##*/}
    mv -i "$file" "${file%/*}${base//:}"
  done' bash {} +

Should remove the :s in the names of directories.

If bash is not available, but mksh (MirBSD shell, and also the shell of Android) or ksh93 is (that ${var//pattern/replacement} operator is actually from ksh93), that should also work if you replace bash with ksh above. Same with zsh.

Beware that if both /path/to/share/foo:bar and /path/to/share/foobar directories exist, mv -i /path/to/share/foo:bar /path/to/share/foobar will actually move foo:bar into foobar. With the GNU implementation of mv, that can be avoided by passing the -T option to mv in which case you will get (thanks to -i) a mv: overwrite '/path/to/share/foobar'? prompt instead, and if you answer yes and foobar was empty, foo:bar will be renamed to foobar (and the old foobar discarded).

You must log in to answer this question.

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