Bash/batch multiple file, single folder, incrimental rename script; user provided filename prefix parameter

StackOverflow https://stackoverflow.com/questions/7023675

Frage

I have a folder of files which need to be renamed.

Instead of a simple incrimental numeric rename function I need to first provide a naming convention which will then incriment in order to ensure file name integrity within the folder.

say i have files:

wei12346.txt
wifr5678.txt
dkgj5678.txt

which need to be renamed to:

Eac-345-018.txt
Eac-345-019.txt
Eac-345-020.txt

Each time i run the script the naming could be different and the numeric incriment to go along with it may also be ddifferent:

Ebc-345-010.pdf
Ebc-345-011.pdf
Ebc-345-012.pdf

So i need to ask for a provided parameter from the user, i was thinking this might be useful as the previous file name in the list of files to be indexed eg: Eac-345-017.txt

The other thing I am unsure about with the incriment is how the script would deal with incrimenting 099 to 100 or 999 to 1000 as i am not aware of how this process is carried out.

I have been told that this is an easy script in perl however I am running cygwin on a windows machine in work and have access to only bash and windows shells in order to execute the script.

Any pointers to get me going would be greatly appreciated, i have some experience programming but scripting is almost entirely new.

Thanks, Craig

(i understand there are allot of posts on this type of thing already but none seem to offer any concise answer, hence my question)

War es hilfreich?

Lösung

#!/bin/bash

prefix="$1"
shift

base_n="$1"
shift

step="$1"
shift

n=$base_n

for file in "$@" ; do
    formatted_n=$(printf "%03d" $n)
    # re-use original file extension whilke we're at it.
    mv "$file" "${prefix}-${formatted_n}.${file##*.}"
    let n=n+$step
done

Save the file, invoke it like this:

bash fancy_rename.sh Ebc-345- 10 1 /path/to/files/*

Note: In your example you "renamed" a .txt to a .pdf, but above I presumed the extension would stay the same. If you really wanted to just change the extension then it would be a trivial change. If you wanted to actually convert the file format then it would be a little more complex.

Note also that I have formatted the incrementing number with %03d. This means that your number sequence will be e.g.

010
011
012
...
099
100
101
...
999
1000

Meaning that it will be zero padded to three places but will automatically overflow if the number is larger. If you prefer consistency (always 4 digits) you should change the padding to %04d.

Andere Tipps

OK, you can do the following. You can ask the user first the prefix and then the starting sequence number. Then, you can use the built-in printf from bash to do the correct formatting on the numbers, but you may have to decide to provide enough number width to hold all the sequence, because this will result in a more homogeneous names. You can use read to read user input:

echo -n "Insert the prefix: "
read prefix
echo -n "Insert the sequence number: "
read sn

for i in * ; do
  fp=`printf %04d $sn`
  mv "$i" "$prefix-$fp.txt"
  sn=`expr $sn + 1`
done

Note: You can extract the extension also. That wouldn't be a problem. Also, here I selected 4 numbers fot the sequence number, calculated into the variable $fp.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top