Search code examples
bashsh

Remove part of filename for multiple files using bash


I have multiple files named 01 - a.txt, 02 - b.txt, 03 - c.txt etc. I want to remove the initial number and the dash to have all files named like a.txt, b.txt, c.txt. I'm not good with bash so I would be extremely thankful for some help.

Many thanks!


Solution

  • Try substring removal ##*-

    E.g.

    $ arr=("01 - a.txt" "02 - b.txt" "03 - c.txt")
    
    $ echo "${arr[@]##*- }"
    a.txt b.txt c.txt
    

    In a loop

    for i in *-*.txt;do echo "$i" "${i##*- }";done
    01 - a.txt a.txt
    02 - b.txt b.txt
    03 - c.txt c.txt
    

    Replace echo with mv if the output looks ok.