33

I have multiple files named like that : screenshot 13:25.png
Windows struggle to open these files probably because of the ":".

How can I replace it?

1
  • 2
    Can you maybe accept an answer to mark the question as resolved? This will greatly help other users.
    – MERose
    Nov 5, 2015 at 16:01

8 Answers 8

57

In a terminal, cd into the right directory and then run this.

rename 's/\:/-/g' *.png -vn

This will preview the renaming. It should replace : with -.

If that looks right, remove the n from the end and then it will do the actual renaming.

Note: Ubuntu versions above 17.04 don't ship with rename, but it's still available in the default repositories, so use sudo apt install rename to obtain it

10
  • Thanks for your answer.I tried and I have this problem: Substitution pattern not terminated at (eval 1) line 1.
    – user236152
    Jan 16, 2014 at 11:22
  • @user236152 Sorry, I went a bit mad with the escaping backslashes (was on autopilot!). Removed a couple and it should be valid now.
    – Oli
    Jan 16, 2014 at 11:39
  • Perhaps a simple Nautilus Script that replaces all such characters that might cause problems in other platforms (running on all selected files) could be useful for "multiplatform" people.
    – Sadi
    Jan 16, 2014 at 14:04
  • 2
    Why use it? It shows you what it's doing (makes it easier to reverse if you make a mistake). Why at the end? Just so you can remove it (or the n) easily.
    – Oli
    Jan 17, 2014 at 11:19
  • 1
    @nightcrawler You should be able to chain completely separate replacements: rename 's/\:/-/g; s/%28/ /g; s/%29//g' ...
    – Oli
    Nov 10, 2016 at 12:14
15

Here's a pure bash solution:

for i in *:*; do
    mv -- "$i" "${i//:/_}"
done

The ${var//pattern/replacement} format will replace all occurrences of pattern with replacement in the variable $var. For more information on bash's string maipulation capabilities, see here.

If you want to do this for multiple characters, you could simply place them in a character class. So, for example, to replace all of ;,:,=,+,%,, with underscores, you could do:

$ ls 
1foo:bar  2foo:bar:baz  3foo;bar  4foo=bar  5foo%bar  6foo,bar  7foo+bar
$ for i in *; do mv -- "$i" "${i//[:;=%,+]/_}"; done
$ ls
1foo_bar  2foo_bar_baz  3foo_bar  4foo_bar  5foo_bar  6foo_bar  7foo_bar

Basically, the idea is that [ ] means any of the characters listed. So, by placing all of the characters you want to replace in the character class, all of them are dealt with at once.

The -- after mv signifies the end of options and is needed so that this will work even for file names beginning with - which otherwise would have been treated as options passed to mv.


For the specific characters you asked for, things are a bit more complex because some of them need to be escaped (I am ignoring the / since *nix doesn't allow it in file names any more than Windows does so that won't be an issue):

$ ls
1foo<bar  2foo>bar  3foo:bar  4foo\bar  5foo|bar  6foo*bar  7foo?bar  8foo"bar  9foo'bar
$ for i in *; do mv -- "$i" "${i//[<>:\\|*\'\"?]/_}"; done
$ ls
1foo_bar  2foo_bar  3foo_bar  4foo_bar  5foo_bar  6foo_bar  7foo_bar  8foo_bar  9foo_bar

Note that I escaped the \,' and " by adding a \ in front of each.

5
  • Can this be enhanced to replace several individual characters (e.g. all those that are not acceptable by Windows) with others?
    – Sadi
    Jan 16, 2014 at 14:18
  • 1
    @Sadi see updated answer.
    – terdon
    Jan 16, 2014 at 14:29
  • Basically there are nine: "< > : " / \ | ? *"
    – Sadi
    Jan 16, 2014 at 14:36
  • @Sadi see updated answer.
    – terdon
    Jan 16, 2014 at 14:50
  • Thanks, this could help create a Nautilus Script that makes names of selected file(s) compatible with Windows.
    – Sadi
    Jan 16, 2014 at 15:12
2

If you prefer a GUI, install pyrenamer:

sudo apt-get install pyrenamer

Then run it:

pyrenamer

It has dozens of options for patterns and renaming formats.

1

I prefer GUI but as a Nautilus Extension, i.e. Nautilus Actions Extra:

sudo add-apt-repository ppa:nae-team/ppa
sudo apt-get update
sudo apt-get install nautilus-actions-extra
nautilus -q

(See www.webupd8.org/2011/12/nautilus-actions-extra-pack-of-useful.html)

Then when you select files to be renamed and click Rename from the context menu, you are offered many options for renaming files.

0

The renameutils package has a handy program called qmv which provides interactive batch renaming. You run it on a directory or a list of files, and it generates a temporary text file containing their file names, and starts your favourite editor. When you exit the editor, any file names you changed are renamed accordingly. qmv turns a batch rename problem into a text editing problem.

In this case, you can use qmv -f do *:* to edit the names of all the files containing a colon, and then the :%s/:/_/g command in Vim (or a corresponding search-and-replace in whatever editor you use).

The -f do option sets the format of the text file to have the destination file-name only, which makes the search-and-replace simpler. The default format has two copies of the file name on each line, and you edit only the second one. That allows it to do the right thing if you've deleted or reordered lines in the file, but it's not always the easiest. There are other formats too.

With qmv, you can instead choose to pipe the generated file through a command of your choice, such as sed, if you don't want the interactive capability.

0

And here is a Nautilus Script that replaces (all?) Windows-incompatible characters with a hyphen in all selected file names (based on the answer by terdon ;-)

#!/bin/bash
filesall=""
while [ $# -gt 0 ]
    do
        files=`echo "$1" | sed 's/ /\?/g'`
        filesall="$files $filesall"
        shift
    done
for i in $filesall; do mv "$i" "${i//[<>:\\|*\'\"?]/-}"; done
0

Thunar (the XFCE file-manager) has a nice build in feature for bulk renaming of files. It has options for removing/replacing characters, search and replace, numbering files and more.

Install thunar:

sudo apt-get install thunar

Open thunar, browse to your files, select them and and choose 'rename' (via context menu or F2)

0
@echo off
echo.
echo. How many Characters Do You Want To Remove
echo.
echo. From The End Of The FileName ?
echo.
echo.
set /p variable=" > "
setlocal enabledelayedexpansion
for /f "delims=" %%a in (' dir /b /a-d *.mp3') do (
set oldName=%%a
Set newName=!oldName:~0,-%variable%!.mp3
Ren "!oldName!" "!newName!"
)
exit
3
  • This works well and has a variable prompt ... Name the file something like _ Truncate Last Characters.cmd
    – TekWiz2010
    Dec 21, 2015 at 5:16
  • 3
    While this may be correct, this uses a Windows batch scripting language. We're on AskUbuntu, a site for Ubuntu related questions, and hence Ubuntu-related tools. My view is that this answer may not necessarily be appropriate here. Dec 21, 2015 at 5:29
  • You'd need to run it through Wine but it's probably possible to run this on Ubuntu... It's just a weird answer, tis all.
    – Oli
    Dec 21, 2015 at 10:09

You must log in to answer this question.

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