Command Line File Mastery: Rename and Delete in Bulk

Alright, let’s get this done. You’ve got a mess of files with some weird numbering and you want to clean it up. Specifically, you’re looking to nuke 040-the-rm-command.md and then streamline the rest of your files, turning 014-the-mkdir-command.md into a clean mkdir.md, and so on.

Here’s how you’ll tackle that.


Cleaning Up Your File Names: A No-Nonsense Guide

You’ve got files named like 014-the-mkdir-command.md and a specific one you want gone: 040-the-rm-command.md. This isn’t about just moving things around; it’s about making your file structure logical and easy to navigate.

We’re going to break this down into two clear actions:

  1. Eliminate the Specific File: Get rid of that 040-the-rm-command.md file.
  2. Standardize Your Naming: Transform those clunky NNN-the-command-command.md names into simple command.md.

Let’s dive in.


Step 1: Nuke the Unwanted File

First things first, let’s trash 040-the-rm-command.md. This is straightforward.

Open your terminal, navigate to the directory where these files live, and run this command:

rm 040-the-rm-command.md

That’s it. No theatrics. The file is gone.


Step 2: Streamline Your File Names

Now for the main event: cleaning up the rest of those files. You’ve got options here, depending on what you’re comfortable with and what tools you have available. I’ll give you two solid ways to do this.


Option A: The Loop Method (Bash/Zsh)

This method uses a simple loop in your terminal to process each file individually. It’s robust and works pretty much everywhere.

for file in [0-9][0-9][0-9]-the-*-command.md; do
  # Pull out the core command name, e.g., "mkdir" from "014-the-mkdir-command.md"
  name_part="${file#*-the-}"
  command_name="${name_part%-command.md}"

  # Build the new, clean filename
  new_name="${command_name}.md"

  # Make the move
  mv "$file" "$new_name"
done

How it works:

  • for file in [0-9][0-9][0-9]-the-*-command.md; do: This tells your shell to grab every file that starts with three digits, has “-the-“, something in the middle, and ends with “-command.md”.
  • name_part="${file#*-the-}": This strips off the “###-the-” part from the beginning.
  • command_name="${name_part%-command.md}": This then strips off the “-command.md” part from the end, leaving you with just the command, like “mkdir” or “mv”.
  • new_name="${command_name}.md": This reassembles it into the clean format, e.g., mkdir.md.
  • mv "$file" "$new_name": This is the command that actually renames the file.

Option B: The rename Command (Advanced, but Powerful)

If you’re on a Linux system, you might have a powerful rename command (often called perl-rename or prename). This is a one-liner for some serious file manipulation.

First, check if you even have it:

which rename

If that spits out a path like /usr/bin/rename, you’re in business.

If you have it, here’s the command:

rename 's/^\d{3}-the-(.*)-command\.md$/$1\.md/' *.md

How it works:

  • rename: The command itself.
  • 's/^\d{3}-the-(.*)-command\.md$/$1\.md/': This is a regular expression, essentially a pattern matcher.
    • s/.../.../: Means “substitute this for that.”
    • ^\d{3}-the-: Matches the start of the filename, three digits, and “-the-“.
    • (.*): This is the crucial part. It captures anything in between, which is your command name (e.g., “mkdir”).
    • -command\.md$: Matches “-command.md” at the very end of the filename. The backslashes before the periods are important; they tell the regex to treat . as a literal period, not a wildcard.
    • $1\.md: This is what you’re replacing it with. $1 refers to whatever was captured by (.*). So, if (.*) caught “mkdir”, this becomes mkdir.md.
  • *.md: Tells rename to only look at files ending in .md.

Before You Hit Enter: The Golden Rule

Always, always test destructive commands first. You don’t want to accidentally rename or delete the wrong files.

For the Loop Method:

Before running the mv command, replace it with echo to see what would happen:

for file in [0-9][0-9][0-9]-the-*-command.md; do
  name_part="${file#*-the-}"
  command_name="${name_part%-command.md}"
  new_name="${command_name}.md"
  echo "Would rename '$file' to '$new_name'"
done

For the rename Command:

Use the dry-run option (-n):

rename -n 's/^\d{3}-the-(.*)-command\.md$/$1\.md/' *.md

This will show you a list of planned changes without actually executing them. Once you’re confident, remove the -n.


There you have it. Pick your poison, execute with precision, and get those files looking like they belong in a well-organized system.