0

I want to rename files in order of file-date; from example:

file_1.pdf
file_2.pdf

to:

file_1_ONE.pdf
file_2._TWO.pdf

The script must rename the first file to "ONE" ordered by file-date.

  • 1
    You need to rename the files to use word-based numbering? Or is that ONE a place-holder? (If so, please edit your question to show what you really want.) Why does the second file get ._TWO added (with a .), but the first file gets only _ONE? – roaima Jan 31 at 12:04
2

Using the zsh shell and the number utility found in /usr/games on some BSD systems (on e.g. Ubuntu systems, this would be available from the bsdgames package):

number=1
for file in ./file_*.pdf(.Om); do
    mv "$file" "${file%.pdf}_$(number -l "$number" | tr '[:lower:]' '[:upper:]').pdf"
    number=$(( number + 1 ))
done

The (.Om) is a modifier to the preceding pattern that is specific to the zsh shell. It makes the pattern expand to a list of filenames of regular files that is ordered by the mtime timestamp (last modified time) with the oldest file first.

The command number -l "$number" | tr '[:lower:]' '[:upper:]' would call number with the current value of $number and would return the English word corresponding to this in upper case, e.g. ONE, TWO, THREE etc. This would be prepended with the filename without its filename suffix and with an underscore, and the string .pdf would be added to the end.

Alternatively, to avoid tr:

number=1
for file in ./file_*.pdf(.Om); do
    en_number=$(number -l "$number")
    mv "$file" "${file%.pdf}_${en_number:u}.pdf"
    number=$(( number + 1 ))
done

${en_number:u} would convert $en_number (the output from number) to upper case. The :u modifier is zsh-specific and ${variable:u} corresponds to ${variable^^} in bash.

Testing:

$ for name in file_{1..10}.pdf; do touch "$name"; sleep 2; done
$ ls -lt
total 4
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_10.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_9.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_8.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_7.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_6.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_5.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_4.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_3.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_2.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_1.pdf
-rw-r--r--  1 kk  wheel  163 Jan 31 13:26 script.sh
$ zsh script.sh
$ ls -lt
total 4
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_10_TEN.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_9_NINE.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_8_EIGHT.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_7_SEVEN.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_6_SIX.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_5_FIVE.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_4_FOUR.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_3_THREE.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_2_TWO.pdf
-rw-r--r--  1 kk  wheel    0 Jan 31 13:28 file_1_ONE.pdf
-rw-r--r--  1 kk  wheel  163 Jan 31 13:26 script.sh
-2

This script lists the name of each file in the current folder in order of creation time along with an increasing counter. Change the echo line to do what you need :)

#!/bin/bash

((counter=1))

for f in $(ls -1ct); do
    echo $f-$counter
    ((counter+=1))
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.