Omit Second Word in Filename

Ever had a bunch of files, where you wanted to omit the second word in the file name?

Now, I believe there’s a much smaller way using zmv and it’s regular expression powers (and if there is and you know, please feel free to add it in the comments).

I for my part grew up with the bash and therefore feel much more comfortable with using sed and awk, though I haven’t used it in a while. But somehow – at least to my memory – I never needed to use substr, so this one was new to me. Here’s how I’ve done it:

for f in *; do; t=$(awk '{print $1 " " substr($0, index($0,$3))}' <<< $f); mv "$f" "$t"; done

My initial idea, which I found much more appealing was to just override the second variable:

for f in *; do; t=$(awk '{$2=""; print}'; mv "$f" "$t"; done

But that still treats $2 as a word, and therefore, before and after it there’s a space, leaving me with two spaces. Using substr() however, the space is removed, which is why i had to manually insert it.

Now if you want a more readable script file read on.

Continue reading