Issue
I want to rename files by removing the last N characters
For example I want to rename these files by removing the last 7 characters
From:
file.txt.123456
To:
file.txt
Is this doable in a single command?
Solution
You can remove a fixed number of characters using
mv "$file" "${file%???????}" # 7 question marks to match 7 characters
This will work in any POSIX-compliant shell.
To remove the last extension (which may be more or less than 7 characters), use
mv "$file" "${file%.*}"
To trim everything after a given extension, you can try
EXT=csv
mv "$file" "${file%.$EXT.*}".$EXT
which actually removes .$EXT
and everything after, but then reattaches .$EXT
.
Answered By - chepner Answer Checked By - Dawn Plyler (WPSolving Volunteer)