Blog / Linux/ How to Batch Remove the First N Characters from Filenames in Linux: Three Methods Explained

How to Batch Remove the First N Characters from Filenames in Linux: Three Methods Explained

Linux 批量删除文件名中的前 N 位字符:三种方法详解

Batch Remove the First N Characters from Filenames in Linux

In Linux, you may need to rename multiple files by removing a fixed number of characters from the beginning of each filename. This can be done efficiently using shell commands or scripts, avoiding manual work.

Method 1: Using a for Loop with sed

This command removes the first 9 characters from all filenames in the current directory:

for var in `ls`; do mv -f "$var" `echo "$var" | sed 's/^.........//'`; done

Explanation:

  • for var in `ls`; do ... done: Iterates over items listed by ls.
  • mv -f "$var" ...: Forcefully moves (renames) the file.
  • echo "$var" | sed 's/^.........//': Uses sed to delete the first 9 characters (each dot . matches any single character).

Note: This method uses ls output, which can cause issues with filenames containing spaces or special characters. A more robust approach uses shell globbing.

Method 2: Robust for Loop (Recommended)

Using the wildcard * handles filenames with spaces and special characters correctly:

for file in *; do
    if [ -f "$file" ]; then
        newname=$(echo "$file" | sed 's/^.........//')
        mv -f "$file" "$newname"
    fi
done

Improvements:

  • for file in *;: Uses shell globbing for safer iteration.
  • if [ -f "$file" ]; then: Checks if the item is a regular file, skipping directories.
  • newname=$(...): Stores the new filename in a variable for clarity.
  • Variables are quoted (e.g., "$file") to preserve spaces.

Method 3: Using the rename Command (If Available)

Many Linux distributions include a Perl-based rename command that uses Perl regular expressions. To delete the first 9 characters:

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

Explanation:

  • s/^.{9}//: Perl regex where ^ matches the start and .{9} matches any 9 characters, replacing them with nothing.
  • *: Operates on all files in the current directory.

Note: rename implementations vary. The above works for the Perl version. Test first with the -n (dry-run) flag: rename -n 's/^.{9}//' *.

Generalization: Removing Any N Characters

Replace the number 9 or .{9} with your desired N.

  • With sed: Use N dots .. E.g., to remove 5 characters: 's/^.....//'.
  • With Perl rename: Use the regex .{N}. E.g., to remove 5 characters: 's/^.{5}//'.

Safety Recommendations

  1. Backup or Test: Test commands in a sample directory or use dry-run modes (like rename -n).
  2. Subdirectories: The commands above only affect the current directory. Use find for recursive operations, but do so cautiously.
  3. Character Counting: These methods work on characters. For filenames with multi-byte characters (e.g., Chinese), test on a few files first.

Using these methods, you can safely and flexibly batch remove the first N characters from filenames in Linux.

Post a Comment

Your email will not be published. Required fields are marked with *.