Linux tools to bulk rename files

Rnr

The first tool to cover is called rnr and is written in Rust. It can be downloaded on GitHub where also some good examples can be found on how to use the tool.

Let’s try it out on a directory that we have with Markdown files. Due to a conversion, the file names include a date. As this is no longer needed, we want to strip out the date and only get the bit after the third hyphen.

  • Original file name: 2014-10-25-how-to-create-custom-tests-in-lynis.md
  • Wanted file name: how-to-create-custom-tests-in-lynis.md
# rnr --dry-run '\d+-\d+-\d+-' '' ./*.md
This is a DRY-RUN
./2014-10-25-how-to-create-custom-tests-in-lynis.md -> ./how-to-create-custom-tests-in-lynis.md
./2016-01-05-how-to-determine-a-file-type-on-linux.md -> ./how-to-determine-a-file-type-on-linux.md
./2015-03-30-viewing-available-test-categories-in-lynis.md -> ./viewing-available-test-categories-in-lynis.md
./2014-11-24-auditing-systemd-solving-failed-units-with-systemctl.md -> ./auditing-systemd-solving-failed-units-with-systemctl.md
ParameterExplanation
--dry-runDon’t make changes right away, but show what will be done
'(\d+)-(\d+)-(\d+)-'Capture three occurrences of just numbers
''Replace with empty string, as we want to strip the date
./*.mdOnly apply this to the Markdown files in this current directory

The nice part of rnr is that it shows the rename it will do. As this looks good, we can tell it to perform the action. In that case replace the dry-run with --force to make the actual changes.

Rename tool

The rename tool with the similar name rename can be found on many Linux systems. It used a Perl syntax for the matching, so with some fiddlng we can do the same bulk action as before.

If the tool is not installed yet, here are some pointers on the right package

Linxux distributionPackage nameInstallation
Ubunturenameapt install rename

Usage

# rename --nono 's/\d{4}-\d{2}-\d{2}-//' ./*.md
rename(./2014-03-02-lynis-stuck-during-testing.md, ./lynis-stuck-during-testing.md)
rename(./2014-03-06-linux-audit-auditing-network-configuration.md, ./linux-audit-auditing-network-configuration.md)
rename(./2014-03-09-securing-linux-audit-lynis.md, ./securing-linux-audit-lynis.md)
ParameterExplanation
--nonoDo not make actual changes
's/\d{4}-\d+{2}-\d{2}-//'Do a search (s), first 4 digits, hyphen, 2 digits, hyphen, 2 digits, hyphen. No replacement text, as we want to strip text from the file name

regex-rename

Another great utility can be found in the Python Package Index (PyPi) and is named regex-rename. As the name implies, it also support regular expressions.

Installation

pip3 install regex-rename

Tip: do the installation in a virtual environment (python3-virtualenv)

Usage

The next step is forming the regular expression. This utility does a “dry-run” by default and requires the --rename option to make actual changes.

# regex-rename "(\d{4})-(\d{2})-(\d{2})-(.*)" "\4"
[2024-03-28 14:03:51] DEBUG matching regular expression pattern to files: pattern=(\d{4})-(\d{2})-(\d{2})-(.*) replacement=\4 dry_run=True full_match=False recursive=False padding=None
[2024-03-28 14:03:51] INFO  matched file: from=2014-03-02-lynis-stuck-during-testing.md to=lynis-stuck-during-testing.md group_1=2014 group_2=03 group_3=02 group_4=lynis-stuck-during-testing.md
[2024-03-28 14:03:51] INFO  matched file: from=2014-03-06-linux-audit-auditing-network-configuration.md to=linux-audit-auditing-network-configuration.md group_1=2014 group_2=03 group_3=06 group_4=linux-audit-auditing-network-configuration.md
[2024-03-28 14:03:51] INFO  matched file: from=2014-03-09-securing-linux-audit-lynis.md to=securing-linux-audit-lynis.md group_1=2014 group_2=03 group_3=09 group_4=securing-linux-audit-lynis.md
[2024-03-28 14:03:51] INFO  files matched the pattern: matched=3 mismatched=0

Let’s have a look at the regular expression first.

Regular expression partExplanation
"Open the regular expression
(\d{4})-Match 4 digits, followed by a hyphen
(\d{2})-(\d{2})-Match 2 digits, hyphen, 2 digits, hyphen
(.*)Match all characters, so that we can use this part as our replacement
"Close the regular expression

The output of the tool is colored and shows what it is trying to do. We see also the dry_run=True that tells us that it won’t make any changes. It shows what files match and what the new file name would be. Another great tool do do a batch rename action.

Got another tool that you really like to use for batch renaming? Let us know!

Take the next step!

Want to learn more about Linux security? Have a look at the open source tool Lynis and become a Linux security expert yourself.

Lynis is a battle-tested technical security audit tool. It is open source, freely available, and used by system administrators all over the world. Other users include IT auditors, security professionals, like pentesters.

Tool Information

Visit project page
Screenshot of Lynis security tool