I have a directory 'main' containing many sub-directories and in some of these sub-directories I have filenames that contain a colon. I uploaded the 'main' directory to a GitHub repository from Linux, but I ran into problem when I then tried to pull from this repository on a Windows machine, as Windows doesn't allow colons in filenames.

How can I recursively find all filenames with a colon in the 'main' directory using the Linux terminal, and then replace the colon with a, say, a dash (-) character?

find /path/to/directory -type f -name "*:*" -exec rename -n 's/:/-/g' {} +

(If this command doesn't work for you, see the Compatibility Note at the end of this answer.)

This is a basic safe version that only prints what would happen. To make it actually happen get rid of -n option.

It would work without -name "*:*" but it's good to let find pass only the filenames that should be changed because this makes the whole command faster.

It works like this:

  • find finds...
    • ...in the /path/to/directory dir...
    • ...all files (-type f)...
    • ...with matching names (-name "*:*"),...
    • ...then executes rename (multiple times if needed)...
    • ...with the pattern to replace : by -...
    • ...on all the files found, multiple files at once ({} +).


  • The command doesn't rename directories and this is by design.
  • Renaming may not work with some unusual characters in the filename (e.g. newline). Most certainly you have no such filenames there so this is rather a formal note.

Compatibility Note

The solution uses Perl script rename from rename package available on Debian, Ubuntu. Other distros may have rename from util-linux.

This manual suggests that the solution for non-Perl rename should be:

find /path/to/directory -type f -name "*:*" -execdir rename -n ':' '-' {} +

(Again, remove -n if it looks right.) I haven't tested this though. I think it will replace only the first colon in a filename. A workaround is to repeat the command (without -n, with -v) until there are no colons left.

Alternatively you can obtain the Perl script from an archive available on this site (currently File-Rename-0.20.tar.gz, the script is named rename.PL). See also this question.

  • ..hmmm..I'm getting an error when I enter the command - rename: not enough arguments. This is with and without the -n option. Any ideas why its not working? – eurocoder Aug 29 '17 at 15:17
  • I am using Fedora by the way if that makes a difference. – eurocoder Aug 29 '17 at 15:23
  • @eurocoder Please check the alternative command near the end of the edited answer, it may work for you. – Kamil Maciorowski Aug 29 '17 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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