Como renomear em lote arquivos no Ubuntu?

0

Eu achei rename no Ubuntu 10 não suporta regex, infelizmente. Eu preciso renomear arquivos contendo _thumb para _t deixando o resto como está. Exemplo: renomeie SLN0097H_thumb@2x~ipad.JPG para SLN0097H_t@2x~ipad.JPG . Espero que seja possível fazer sem escrever um loop de script bash.

    
por Pablo 26.08.2014 / 16:02

2 respostas

3

Não sei por que você quer evitar um loop do bash, mas aqui está um:

for i in *_thumb*; do mv "$i" "${i/_thumb/_t}"; done
    
por 26.08.2014 / 17:04
-1

Eu tenho um script antigo que você pode experimentar - funciona como

splat "wid" "wod" "wid *" [fazer]

wid_popup.c - > wod_popup.c

wid_popup.h - > wod_popup.h

wid_splash.c - > wod_splash.c

wid_splash.h - > wod_splash.h

apenas adicione "do" ao args se a renomeação fizer sentido. Existem provavelmente maneiras mais curtas de fazer isso, mas hey, funciona para mim 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."
    
por 26.08.2014 / 16:11