34

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?

Oli
  • 299,380

8 Answers8

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

Oli
  • 299,380
16

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.

terdon
  • 104,119
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.

Sadi
  • 11,074
1

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.

Dan Hulme
  • 169
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
Sadi
  • 11,074
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