Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename file by removing last n characters

Tags:

shell

unix

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?

like image 740
heinistic Avatar asked Oct 23 '13 00:10

heinistic


People also ask

How do I rename multiple files or delete multiple characters?

Batch Rename with File Explorer Head to the folder containing the files you wish to rename. Order the files how you wish to rename them. Press CTRL + A to select all the files in the folder, then right-click and select Rename. Input your new file name, and press Enter.

How do I remove the last character from a file?

The $ s/. $// function call executes on the endmost line of the file. It replaces the last character (s) with an empty string. In this way, it completely takes off the last character of the file.

How do you change a lot of file names quickly?

To rename multiple files from File Explorer, select all the files you wish to rename, then press the F2 key. The name of the last file will become highlighted. Type the new name you wish to give to every file, then press Enter.


1 Answers

Are you using bash?

file="file.txt.123456"
mv $file ${file::(-7)}
like image 104
Adam Liss Avatar answered Sep 19 '22 16:09

Adam Liss