Search code examples
bashshellscriptingcygwinsh

how to change filenames in folder cygwin?


I have a folder contains meany files with the flowing convention:

#1_name.apk
#2_name2.apk
#3_name_name_name.apk

I want to change the names of all files and remove only the starting #1_ I tried to do this:

for file in *; do echo $file | mv ./$file $(awk -F '[/_]' '{print $2}'); done

The problem is when I have more than one _ in the filename like #3_name_name.apk.

My question is, how do I split only the start tags?

Thanks.....


Solution

  • Using Shell Parameter Expansion:

    for file in *; do echo $file; mv "$file" "${file:3}"; done
    

    This assumes that 3 leading characters from the filename need to be removed.