Linux: Rename Expression To Remove First Character From a File Name

by on April 18, 2011 · 13 comments· LAST UPDATED April 19, 2011

in

I've over 1000's of files starting with _ character as follows:

_file1.txt
_foo1.txt
_2424.txt
_bar10000.txt

How do I remove the first character from a file name:

file1.txt
foo1.txt
2424.txt
bar10000.txt


You can use the rename command as described in our previous faq. The syntax is as follows:

 
rename "s/regex-rule/replace/g" *.files
 

To remove _ first character and rename all *.txt files, enter:

 
rename -v "s/^_//g" *.txt
 

Use the ls command to verify the same:
$ ls -l

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 break April 19, 2011 at 9:14 am

Hello Vivek,

in you example should be:

rename -v “s/^_//g” *.txt

without “^” rename will be replaced all occurs in argument (file name).

Alternatively we can use “sed”

Reply

2 nixCraft April 19, 2011 at 11:22 am

Thanks for the heads up!

Reply

3 Jorge June 27, 2013 at 8:17 pm

what if the first character has different values?

Reply

4 break July 2, 2013 at 9:42 am

@Jorge: “^_” means: “all lines with underscore at the beginning”

If first character will be different that “_” rename will just ignore such file.

Reply

5 Eric April 21, 2011 at 4:45 pm

I cant seem to get that to work. It just comes back instantly. im using

rename -v “s/^_//g” *.jpg

Reply

6 robf April 24, 2011 at 4:26 pm

That would mean that it didn’t match anything with both the *.jpg and regex. Are you sure it’s JPG files you want to rename, and are they definitely there!

Also, rename -vn is good – it’ll show you what it’s going to do without actually doing it, great for bulk jobs to make sure you’re doing the right thing.

Plus (!), in the example above the “g” modifier (multi-match) is wasteful, as you already say in the regex you want a “_” starting at the beginning of the line – this can only happen once, so lose the “g”. This modifier should be used sparingly, particularly if you’re not going to use the -n flag to simulate the regex, as that could lead to unwanted replacements.

HTH!

Reply

7 loomsen May 6, 2011 at 12:27 pm

Actually, there are two different rename programs, rename from util-linux and prename, which, on some systems, is the default rename command (/usr/bin/rename is a symlink to prename on those systems)

The util-linux rename will not work as shown at all. This tut only applies to perl rename, or prename.

Regards.

Reply

8 Vishal June 3, 2011 at 12:05 pm

Nice Info. thanks for it..

Reply

9 ekeko March 6, 2012 at 10:06 pm

hello, thanks for the info, but how to remove the first 8 characters?

I want this:
“[7db7eb48] L1E.mp4″ renamed as “L1E.mp4″

please help, I am stuck on it.

thanks in advance!

Reply

10 ekeko March 6, 2012 at 10:23 pm

oh I just found it, but it was challenging:
rename -v ‘s/\[\w+\]\s//’ *.mp4

thanks for your inspiring article!

Reply

11 stef April 13, 2012 at 8:28 pm

i was looking to do the same and finally managed to write a small loop that worked fine on my mac. I needed to delete the first 24 characters (including spaces) of several avi files so here is what i did:

for file in *avi; do mv “$file” “${file:24}”; done

file is the variable and can have any name – some use i or f etc etc.

you can obviously change the type of files (avi) and the number of characters you want to delete accordingly.

Hope this assists.

Reply

12 nixCraft April 13, 2012 at 8:55 pm
${file:24}

Nice bash hackery.

PS: It is called substring i.e. bash parameter substitution.

Reply

13 Liza April 2, 2014 at 7:14 pm

Hello!

I have file with names as:
A2002185.h23v04.005.2007177004246.hdf
A2002201.h23v04.005.2008288062542.hdf

I want to rename them by deleting everything in the name after first dot, how can I do that?

Thank you!

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: