Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
6 replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
source | link

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found herehere

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e
  • (\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e
  • (\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e
  • (\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

5 added 2 characters in body
source | link

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e

(\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

    (\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e

(\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e
  • (\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

4 deleted 472 characters in body
source | link

Using Perl's rename utility:

prename 's/(\d+)\./000$1./;sprintf s/0+(\d{4})\"%04d./", $1./'e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./000$1./;sprintf s/0+(\d{4})\"%04d./", $1./'e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

We use twoA single substitute commandscommand is used:

  1. s/(\d+)\./000$1./

    This puts enough zeros in front of the number that precedes the decimal point. (\d+)\. matches one or more digits if they are followed by a period. The digits are saved in group 1. The replacement text is 000$1. which is three zeros, followed by group 1, followed by a period.

  2. s/0+(\d{4})\./$1./

    This removes excess leading zeros so that only the number that remains only has four digits.

s/(\d+)\./sprintf "%04d.", $1/e

(\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.

Using Perl's rename utility:

prename 's/(\d+)\./000$1./; s/0+(\d{4})\./$1./' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

Example

Consider a directory with these files:

$ ls
A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./000$1./; s/0+(\d{4})\./$1./' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt

How it works

We use two substitute commands:

  1. s/(\d+)\./000$1./

    This puts enough zeros in front of the number that precedes the decimal point. (\d+)\. matches one or more digits if they are followed by a period. The digits are saved in group 1. The replacement text is 000$1. which is three zeros, followed by group 1, followed by a period.

  2. s/0+(\d{4})\./$1./

    This removes excess leading zeros so that only the number that remains only has four digits.

Using Perl's rename utility:

prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt

prename is sometimes available under the name rename. There is, however, another unrelated and incompatible utility called rename that is installed by default on some distributions.

Debian-like distributions, among others, have Perl's rename/prename installed by default. If your distribution does not, instructions for installing it can be found here

(Hat tip to Steeldriver for improved version of the command.)

Example

Consider a directory with these files:

$ ls
A12345.txt  A123.txt  A12.txt  A1.txt

Now, let's run prename:

$ prename 's/(\d+)\./sprintf "%04d.", $1/e' A*.txt
$ ls
A0001.txt  A0012.txt  A0123.txt  A12345.txt

How it works

A single substitute command is used:

s/(\d+)\./sprintf "%04d.", $1/e

(\d+)\. matches one or more digits followed by a period. The digits are saved in group 1.

  • The final e tells perl to evaluate the command sprintf "%04d.", $1. This command returns the digits of group 1 in format %04d meaning zero-filled four digits, followed by a period.
3 added 289 characters in body
source | link
2 added 289 characters in body
source | link
1
source | link