I've got a hard drive taken from a NAS drive where files had been put on from a Mac - However, some of the filenames had been, for example, 'backup 16/07/14' which has then been changed to 'backup 16:2f07:2f14' on the NAS.

This is proving an issue when copying files over to a new NAS. So what i'm looking to do is just switch the ':2f' to '-' by running a command on a folder that houses all these other folders.

I'm reasonably new to Linux, so i'm stuck. I've loaded up the hard drive on ubuntu on a virtual machine.

Any suggestions?

If you have perl-rename aka prename:

find . -depth -name "*:*" -exec perl-rename 's/:2f/-/g' {} +

Without:

find . -depth -name "*:*" |
while read -r name; do
    mv -nT "$name" "${name//:2f/-}"
fi
  • Might want a -print0 and -d '' there just for safety. – slhck Jul 16 '14 at 12:03

You need to use

sed

command. It can be used to replace characters in filenames. You first find files with

find

command and use "|" to pipe them to sed.

  • I tried running find . -type f | xargs sed -i " 's/:2f/-/g' But it doesn't seem to run? – Peter Jul 16 '14 at 8:49
  • Also, it's directory names if that makes a difference? – Peter Jul 16 '14 at 8:52
  • 1
    @Peter: It does run; it just doesn't do what you both are expecting. The main tool here, sed -i, does not rename files; it edits files. You're asking it to apply the regex to the files' contents. To rename, you need a different tool, e.g. perl-rename. /// And yes, directory names do make a difference: you added -type f, so you're asking for a list of files specifically, not directories. To list all directories, it would be -type d. – grawity Jul 16 '14 at 10:12

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.