I found rename
on Ubuntu 10 doesn't support regex unfortunately. I need to rename files containing _thumb
to _t
leaving the rest as is. Example: rename SLN0097H_thumb@2x~ipad.JPG
to SLN0097H_t@2x~ipad.JPG
. Hopefully it's possible to do without writing bash script loop.
2 Answers
Not sure why you want to avoid a bash loop, but here's one:
for i in *_thumb*; do mv "$i" "${i/_thumb/_t}"; done
I have an old script you can try - works like
splat "wid" "wod" "wid*" [do]
wid_popup.c -> wod_popup.c
wid_popup.h -> wod_popup.h
wid_splash.c -> wod_splash.c
wid_splash.h -> wod_splash.h
just add "do" to the args if the rename makes sense. There are probably shorter ways to do this, but hey, it works for me 8)
#!/bin/sh
#
# splat-name "pattern" "replacement" "files" [do]
#
# Searches for <pattern> in the matching filenames and uses sed to replace
# with <replacement> in the same filename. i.e. it renames parts of files.
#
# Nothing is done unless [do] is added, so you can test out that it works.
#
if test $# -lt 3
then
echo
echo "Performs recursive file name changes"
echo
echo "usage: splat-name \"<pattern>\" \"<replacement>\" \"<files>\" [do]"
echo
echo "e.g:"
echo " splat \"rm\" \"remove\" \"Makefile\""
echo "Then when sure it works:"
echo " splat \"rm\" \"remove\" \"Makefile\" do"
echo
echo "splat $*"
exit
fi
TOP=/tmp
mkdir -p $TOP 2>/dev/null
pattern=$1
replacement=$2
doit=$4
svn=$5
echo "### Building pattern..."
echo "### replacement= " $replacement
echo "### pattern = " $pattern
SPLATFILE=${TOP}/.splatfile$$
SPLATFILE_TMP=${TOP}/.splatfile_tmp$$
SPLATLIST=${TOP}/.splatlist$$
SPL=${TOP}/.spl$$
###############################################################################
# Build our complex sed line
###############################################################################
echo "s/$pattern/$replacement/g" > ${SPLATFILE}
###############################################################################
# Need a newline for sed
###############################################################################
echo "" >> ${SPLATFILE}
sed "
s/NEWLINE/\\\\012/g
s/#/\\\\#/g" ${SPLATFILE} >${SPLATFILE_TMP}
mv ${SPLATFILE_TMP} ${SPLATFILE}
echo "### Finding relevant files..."
find . -follow -name "$3" -type d -not -wholename "*.svn*" -print | sed 's/^\.\///' > ${SPLATLIST}
for i in `cat ${SPLATLIST}`
do
file=$i
newfile=`echo $file | sed -f $SPLATFILE`
if [ $newfile = $file ]
then
continue
fi
echo "### mv $i -> $newfile"
if [ "$doit" = "do" ]
then
mv $i $newfile
if [ "$svn" = "svn" ]
then
svn rm $i
svn add $newfile
fi
fi
done
find . -follow -name "$3" -type f -not -wholename "*.svn*" -print | sed 's/^\.\///' > ${SPLATLIST}
#echo "### Searching for diffs..."
for i in `cat ${SPLATLIST}`
do
file=$i
newfile=`echo $file | sed -f $SPLATFILE`
if [ $newfile = $file ]
then
continue
fi
echo "### $i -> $newfile"
if [ "$doit" = "do" ]
then
cp $i $newfile
if [ $? -eq 0 ]
then
rm $i
fi
if [ "$svn" = "svn" ]
then
svn rm $i
svn add $newfile
fi
fi
done
if [ -f ${SPL} ]
then
rm ${SPL}
fi
if [ -f ${SPLATFILE} ]
then
rm ${SPLATFILE}
fi
#echo "### Done."
find /path/to/files -type f -name "*thumb*" -exec -c bash rename {} \;
(with the right options added for the rename command).rename
. The others are using some pattern language which I am not familiar and not sure how to do back reference.find
's manpages.