Search code examples
command-linerenamemv

Batch rename with command line


I have some files: file1.txt, file2.txt and I would like to rename them like this: file1.something.txt and file2.something.txt I looked for some similar questions and I come up with this:

for i in file*.txt; do echo  mv $i file*.something.txt; done

but unfortunately the output is:

mv file1.txt file*.something.txt

mv file2.txt file*.something.txt

and therefore only 1 file is created. Could please somebody help? (I am using a macbook air, I am not sure if this is relevant) Thank you very much


Solution

  • Try this :

    rename -n 's/\.txt/something.txt' *
    

    (remove -n switch when your tests are OK)

    warning There are other tools with the same name which may or may not be able to do this, so be careful.

    If you run the following command (GNU)

    $ file "$(readlink -f "$(type -p rename)")"
    

    and you have a result like

    .../rename: Perl script, ASCII text executable
    

    and not containing:

    ELF
    

    then this seems to be the right tool =)

    If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

    $ sudo update-alternatives --set rename /path/to/rename
    

    (replace /path/to/rename to the path of your perl's rename command.


    If you don't have this command, search your package manager to install it or do it manually


    Last but not least, this tool was originally written by Larry Wall, the Perl's dad.