Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a file in linux by removing certain characters from the file name?

Tags:

linux

bash

shell

How can I rename a file in linux to strip out certain characters from the file name?

For example,

My123File.txt to be renamed to My123.txt

like image 259
SSS Avatar asked Dec 10 '22 02:12

SSS


1 Answers

If you're okay with just wildcards (not full regexes), then you might try something like

f='My123File.txt'
mv $f ${f/File/}

This type of shell expansion is documented here.

If you really need regexes, try

f='My123File.txt'
mv $f $(echo $f | sed -e 's/File//')
like image 135
jjlin Avatar answered Mar 08 '23 23:03

jjlin