0

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.

5
  • Why not feed files to rename based on find? (e.g. somehting like find /path/to/files -type f -name "*thumb*" -exec -c bash rename {} \; (with the right options added for the rename command). – Hennes Aug 26 '14 at 14:05
  • It would be great to see a working example, if possible. – Pablo Aug 26 '14 at 14:06
  • 1
    I think example of those are already on the site. E.g. superuser.com/questions/213134/… – Hennes Aug 26 '14 at 14:08
  • accepted answer is using rename with regex, which is not supported in my rename. The others are using some pattern language which I am not familiar and not sure how to do back reference. – Pablo Aug 26 '14 at 14:11
  • @Pablo The pattern language would probably be documented in find's manpages. – Darth Android Aug 26 '14 at 14:31
3

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
-1

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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.