Remove Prefix from Filenames Using Bash String Manipulation
📖 Scenario: You have a list of filenames that all start with the prefix temp_. You want to clean up these names by removing this prefix.
🎯 Goal: Build a bash script that removes the prefix temp_ from each filename using the ${var#pattern} string prefix removal syntax.
📋 What You'll Learn
Create a bash array called
files with the exact filenames: temp_report.txt, temp_data.csv, temp_notes.mdCreate a variable called
prefix and set it to temp_Use a
for loop with variable file to iterate over filesInside the loop, create a variable called
clean_name that removes the prefix temp_ from file using ${file#${prefix}}Print each
clean_name on its own line💡 Why This Matters
🌍 Real World
Removing prefixes from filenames is common when organizing files, cleaning up logs, or processing data exports.
💼 Career
Knowing bash string manipulation helps automate file management tasks efficiently in system administration and DevOps roles.
Progress0 / 4 steps