简体   繁体   中英

Linux rename multiple files with a bash script + adding a suffix

I mention that I've already checked similar questions about renaming files with bash script, but none has helped me with this problem.

I need to rename many files in a directory after the following rule: list the current files alphabetically, then replace their name with a given basic name, add a suffix which is the current file index and then add a given extension.

I encountered a problem at indexing the files. For instance, if I had 1000 files, after executing the script their names should be file0001.ext, file0002.ext ... file0010.ext ... file0099.ext ... file 0100.ext ... file0999.ext, file1000.ext .

I've computed the needed number of zeros for every file, but I do not know how to implement the rule with a regular expression within the mv commnad. Below is my script code:

#!/bin/bash

nod=0

i=1
nof=$(ls -v $3 | wc -l)
cpy=$nof
while [ $cpy -gt 0]
do
    nod=$((nod+1))
    cpy=$((cpy/10))
done
for a in $(ls -v $3)
do
    cpy_of_i=$i
    i_digits=0
    while [ $cpy_of_i -gt 0 ]
    do
        i_digits=$((i_digits+1))
        cpy_of_i=$((cpy_of_i/10))
    done
    nr_zeros=$((nod-i_digits))
    old=$3$a
    new=$(echo $3$1[0]\*{nr_zeros}$i.$2)
    mv $old $new
    i=$((i+1))
done

Note: first argument is the new file name, the second argument is the new extension and the third argument is the directory which contains the files which are to be renamed. So the script will be called from command line with a command similar to this: ./rename.sh new_file pdf /home/dir/something/

Just keep a counter in a variable, use arithmetic expression to increment it after processing each file. Use command substitution with printf to create the zero padded number.

new=$1
ext=$2
dir=$3

id=1
for file in "$dir"/* ; do
    mv "$file" "$dir/$new"$(printf '%04d' $id)."$ext"
    (( id++ ))
done

You might need to only rename "$dir"/*."$ext" if there are files you don't want to rename with different extensions.

Here is my script, which works only on files, sorts them and renames them with numbers of right length (beginning with zeroes)

#!/bin/bash
dots=`find -maxdepth 1 -type f |wc -l|sed "s/./\./g"`
zeroes=`find -maxdepth 1 -type f |wc -l|sed "s/./0/g"`
(
    echo "x=0;zeroes=${zeroes};"
    find  -maxdepth 1 -type f  |sort |sed 's/.*/echo "mv \0 file${zeroes}$((x++)).txt"/'
)|bash|sed "s/\(file\)0*\(${dots}\.txt\)/\1\2/"

The scripts just types some mv something file0012.txt commands, so you either pipe that to bash, or add |bash to the end of the last line

Be also aware, that running such script more times on the same directory (after some modifications - maybe if you add file000000.txt) may result to garbage, as it would try to move files file12.txt to file13.txt possibly overwriting some of them on the way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM