Few bash scripts

Perl rename (copied out from ubuntu dist)

#!/usr/bin/perl -w
use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
        'v|verbose' => \$verbose,
        'n|no-act'  => \$no_act,
        'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
    chop(@ARGV);
}

for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
    {
        warn  "$was not renamed: $_ already exists\n";
    }
    elsif ($no_act or rename $was, $_)
    {
        print "$was renamed as $_\n" if $verbose;
    }
    else
    {
        warn  "Can't rename $was $_: $!\n";
    }
}

Capitalize all filenames in current folder (depend on perl rename)

rename 'y/A-Z/a-z/' *
rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' *

Turn everything to lowercase (depend on perl rename)

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

Turn everything to uppercase (depend on perl rename)

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

SVN Synchorize (auto add and delete all files recursively, also propset Id Revision keywords)

find . -name "._*" | xargs rm -Rf
svn st | grep "^?" | awk '{ print $2}' | while read f; do svn add "$f"; svn propset svn:keywords "Id Revision" "$f"; done;
svn st | grep "^!" | awk '{ print $2}' | while read f; do svn delete "$f"; done;

SVN deploy (create a “deploy” folder with all the modified and added files from a specified revision to HEAD, retaining folder structure)
Example: deploy 815 (will copy everything to ./deploy folder from revision 815 onwards)

svn diff -r $1:HEAD --summarize | grep "^A\|^M" | awk '{ print $2}' | while read f; do mkdir -p `dirname deploy/"$f"`; cp "$f" deploy/"$f"; done

Alias for dir, list just the directories and symbolic links in a directory.

alias dir='ls -Lla|grep ^d'

Svn log by revision and username
example: svn 1000:head vincent

#!/bin/bash
svn log -r $1 | sed -n "/"$2"/,/-----$/ p"

Leave a Reply