#!/bin/bash # # Syntax: mass_move [-h] part1 part2 [file [...]] # # Purpose: Replaces part1 in the names of the given files by part2. # The default value for file is *${part1}* # The -h flag shows the help info for this command. # Created: Sep 28, 1999 by Bart De Schutter # Last revised: Jul 31, 2007 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: files # # Process input arguments. # if [ $# -ge 1 -a "$1" = "-h" ]; then sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi if [ $# -lt 2 ]; then echo >&2 echo >&2 "ERROR: ${0##*/} requires at least 2 input arguments." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi part1="$1" part2="$2" shift 2 if [ $# -eq 0 ]; then # Recursively call mass_move. This is the easiest way to # deal with the list="$@" problem in this case. $0 "$part1" "$part2" *${part1}* fi # # Move the files. # for fname in "$@" do if [ -f "$fname" ]; then new_name=$(echo "$fname" | sed -e "s/${part1}/${part2}/g") if [ "$fname" != "$new_name" ]; then # # Note: If you use cp -i and answer no, then $? still is 0, but the # file is not overwritten. Hence, we use extra tests to prevent # a mv statement if the cp -i answer is no. # if [ -e "$new_name" ]; then read -p "cp: overwrite $new_name (y/n)? [n] " answer if [ "$answer" == 'y' -o "$answer" == 'Y' ]; then \cp "$fname" "$new_name" \mv -i "$fname" "${fname}.bak" fi else \cp "$fname" "$new_name" \mv -i "$fname" "${fname}.bak" fi fi fi done