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


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


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • break Apr 19, 2011 @ 9:14

    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”

    • 🐧 nixCraft Apr 19, 2011 @ 11:22

      Thanks for the heads up!

    • Jorge Jun 27, 2013 @ 20:17

      what if the first character has different values?

      • break Jul 2, 2013 @ 9:42

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

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

  • Eric Apr 21, 2011 @ 16:45

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

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

    • robf Apr 24, 2011 @ 16:26

      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.


  • loomsen May 6, 2011 @ 12:27

    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.


  • Vishal Jun 3, 2011 @ 12:05

    Nice Info. thanks for it..

  • ekeko Mar 6, 2012 @ 22:06

    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!

    • ekeko Mar 6, 2012 @ 22:23

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

      thanks for your inspiring article!

      • stef Apr 13, 2012 @ 20:28

        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.

        • 🐧 nixCraft Apr 13, 2012 @ 20:55

          Nice bash hackery.

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

  • Liza Apr 2, 2014 @ 19:14


    I have file with names as:

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

    Thank you!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum