Portrait of Michael Limberger

Michael Limberger

Need me? Email mike@limberger.ca

Regex

Installing The Tools

Three programs

We need ack, rename, and perl. Perl comes with your system. The other two need installing.

macOS with Homebrew

Homebrew is a package manager for Mac. If you do not have it yet:

Show me.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then install our tools:

brew install ack
brew install rename

That is the whole install. Both tools should now be on your PATH.

Debian and Ubuntu

Show me.

sudo apt update
sudo apt install ack rename

On some systems, ack is called ack-grep:

sudo apt install ack-grep

Then type ack-grep instead of ack in commands.

Fedora and RHEL

Show me.

sudo dnf install ack perl-rename

Fedora's rename might be different. Make sure you get the Perl-based one, not the util-linux one.

Windows

Install Windows Subsystem for Linux first, then follow the Ubuntu instructions above inside that Linux environment.

Did it work?

Show me.

ack --version
rename --version
perl -v

ack should show v3.x.x. rename should show File::Rename. perl should show 5.x.x.

Two programs named rename

There are two different programs called rename.

Perl rename is what we want. Uses regex patterns. Can do complex transformations. The one Homebrew and most Linux distros install.

util-linux rename is not what we want. Simple string replacement only. Limited functionality.

If your rename does not support regex, you might have the wrong one. On some systems the Perl version is called prename or perl-rename.

A practice folder

Make a folder with test files to play with:

Show me.

mkdir ~/regex-practice
cd ~/regex-practice

touch photo_001.jpg photo_002.jpg photo_003.jpg
touch IMG_2024_06_15.png IMG_2024_07_20.png
touch document.txt notes.txt readme.md
touch "file with spaces.txt"
touch log_error.txt log_warning.txt log_info.txt

echo "Hello World" > document.txt
echo "TODO: fix this later" > notes.txt
echo "FIXME: broken code here" >> notes.txt
echo "Error: connection failed" > log_error.txt
echo "Warning: low memory" > log_warning.txt
echo "Info: process started" > log_info.txt

You now have files to experiment with. We will use this folder throughout.