Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

7 Answers

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.

share|improve this answer
    
Thanks for your answer.I tried and I have this problem: Substitution pattern not terminated at (eval 1) line 1. –  user236152 Jan 16 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 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 at 14:04
    
I did rename 's/:/-/g' *.png and it works, thanks !! –  user236152 Jan 17 at 11:18
    
Why put -v at the end ? –  user236152 Jan 17 at 11:18
show 1 more comment

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.


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.

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

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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