Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

hostmaster

2 total

rename many files at once

This is where the bash variable handling makes it even more interesting. Instead of just doing something like "mv $var", we can replace text in the filename using this syntax:

${var/originaltext/replacetext}


So now, if we run this command on our directory:

for f in *;do mv $f ${f/test/prod};done

backup files

// create backup copy

#!/bin/bash

filename=$1
date=`date +%Y%m%d`

usage () {
        echo "Usage: `basename $0` filename"
}

if [ -z "$filename" -a ! -f "$filename" ]; then
        usage
        exit 1
fi

rev=0
backup="$filename.$date.$rev"

while [ -f $backup ]; do
        let rev+=1
        backup="$filename.$date.$rev"
done

cp $filename $backup
exit $?

2 total