如何在 Ubuntu 上批量重命名文件?

如何在 Ubuntu 上批量重命名文件?

不幸的是,我发现renameUbuntu 10 不支持正则表达式。我需要重命名包含的文件,其余部分保持不变。示例:重命名_thumb为。希望可以不编写 bash 脚本循环就做到这一点。_tSLN0097H_thumb@2x~ipad.JPGSLN0097H_t@2x~ipad.JPG

答案1

不确定为什么要避免 bash 循环,但是这里有一个:

for i in *_thumb*; do mv "$i" "${i/_thumb/_t}"; done

答案2

我有一个你可以尝试的旧脚本 - 工作原理如下

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

如果重命名有意义,只需将“do”添加到参数中。可能有更短的方法来做到这一点,但嘿,这对我来说很有效 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."

相关内容