Bash Scripting: How to Bulk Rename Files by Trimming First N Characters in Linux


9 views



When working with batches of files in Linux, we often encounter situations where we need to systematically modify filenames. One common task is removing a fixed number of leading characters from multiple files - exactly the problem we're addressing here.



The most straightforward approach uses bash's built-in parameter expansion:

for file in *; do
    mv "$file" "${file:5}"
done

This script:
1. Loops through all files in current directory
2. For each file, moves it to a new name starting from the 6th character (index 5)



A more robust version would:

for file in *; do
    if [ -f "$file" ]; then
        newname="${file:5}"
        if [ "$newname" != "$file" ]; then
            mv -i "$file" "$newname"
        fi
    fi
done

Key improvements:
- Checks if the item is a regular file (-f)
- Prevents empty new filenames
- Uses -i flag for interactive confirmation on overwrites



For systems with Perl's rename (sometimes called prename):

rename 's/^.{5}//' *

This regular expression removes exactly 5 characters from the start (^) of each filename.



Save this as trim_filenames.sh:

#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: $0 [directory] [chars_to_trim]"
    exit 1
fi

dir="$1"
count="$2"

cd "$dir" || exit 1

for file in *; do
    if [ -f "$file" ]; then
        newname="${file:$count}"
        if [ -n "$newname" ] && [ "$newname" != "$file" ]; then
            mv -i "$file" "$newname"
        fi
    fi
done

Usage example:
./trim_filenames.sh /path/to/files 5



Always test with echo first:

for file in *; do
    echo mv "$file" "${file:5}"
done

Other safety measures:
- Make backups before bulk operations
- Consider using -n (no clobber) instead of -i for scripts
- Test with a copy of your files first



For directories with thousands of files:
- Native bash: ~0.1ms per file
- rename utility: ~0.05ms per file
- Parallel processing could help for very large batches

Remember that filename manipulation in Linux is case-sensitive and treats spaces specially - always quote your variables!

When working with large sets of files in Linux, manually renaming each one is tedious. Bash provides powerful tools for batch file operations. Let's explore how to remove the first 5 characters from multiple filenames.

The simplest method uses a for loop with parameter expansion:

for file in *; do
    mv "$file" "${file:5}"
done

This script:

  • Iterates through all files in current directory
  • Uses bash substring expansion ${file:5} to remove first 5 chars
  • Renames each file with mv command

For filenames containing spaces or special characters, we need proper quoting:

for file in *; do
    mv -- "$file" "${file:5}"
done

The '--' option prevents interpretation of filenames starting with '-' as options.

The rename utility (Perl-based) offers more flexibility:

rename 's/^.{5}//' *

This Perl expression removes exactly 5 characters from the start (^) of each filename.

Always test with echo before actual renaming:

for file in *; do
    echo mv "$file" "${file:5}"
done

To only rename .txt files while preserving extensions:

for file in *.txt; do
    mv "$file" "${file:5}"
done

For safety, create backups before mass renaming:

mkdir backup
cp * backup/

Remember to adjust the numeric value (5) based on how many characters you need to remove. These techniques work on Ubuntu and most Linux distributions.