Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I batch rename files to lowercase in Subversion?

Tags:

linux

svn

We are running subversion on a Linux server. Someone in our organization overwrote about 3k files with mixed case when they need to all be lowercase.

This works in the CLI

rename 'y/A-Z/a-z/' *

But obviously will screw up subversion.

svn rename 'y/A-Z/a-z/' * 

Doesn't work because subversion deals with renames differently I guess. So How can I do this as a batch job? I suck with CLI, so please explain it like I'm your parents. hand renaming all 3k files is not a task I wish to undertake.

Thanks

like image 914
invertedSpear Avatar asked Nov 29 '10 19:11

invertedSpear


People also ask

Is there a way to batch rename files to lowercase?

rename "%f" "%f" - rename the file with its own name, which is actually lowercased by the dir command and /l combination.

Is there a way to batch rename files to uppercase?

Under "Rename" in the Tools menu there are a number of extended rename operations that can be performed. These Rename commands are performed on all selected files and folders. Uppercase / Lowercase renames the selected files/folders to the same name but with all uppercase or all lowercase letters.

Which of the following is the script for renaming files in folder from uppercase to lowercase?

Rename files from uppercase to lowercase with mv command Renaming multiple files from uppercase to lowercase with the mv command is easier if you know a little bit of Bash scripting.


1 Answers

for src in *; do
    dst=$(echo "$src" | tr '[A-Z]' '[a-z]')
    [ "$src" = "$dst ] || svn rename "$src" "$dst"
done

How it works:

  1. Loop over source filenames.
  2. Destination filename is lowercased source filename. tr does character by character translations.
  3. If source and destination filenames are the same we're okay, otherwise, svn rename. You could use an if statement instead, but for one-liner conditionals using || is pretty common.

If you need recursion, you can create a little script svn-lowername.sh (a slight variant of the above):

#!/bin/bash
for src; do
    dst=$(echo "$src" | tr '[A-Z]' '[a-z]')
    [ "$src" = "$dst" ] || svn rename "$src" "$dst"
done

Make sure to chmod +x svn-lowername.sh. Then execute:

find -name .svn -prune -o -type f -print0 | xargs -0 ./svn-lowername.sh

If you think this problem might happen again, stash svn-lowername.sh in your path somewhere (~/bin/ might be a good spot) and you can then lose the ./ prefix when running it.

You can also use svn-lowername.sh on just a single dir like this:

./svn-lowername.sh *
like image 117
Laurence Gonsalves Avatar answered Oct 21 '22 21:10

Laurence Gonsalves