-1

Given that I intent to change files with extensive name of .md to .sh

$ ls
bath.md  breakfast.md  brush.md  test.sh

I wrote it as

$ cat test.sh
#! /usr/local/bin/bash

for f in *
do
    if $f endswith .md
         replace $f.md with $f.sh
done

How could I get the script done?

Wizard
  • 2,503

1 Answers1

1
for i in *.md; do mv -- "$i"  "${i%.md}.sh"; done
Romeo Ninov
  • 17,484
noob
  • 22
  • So… an almost verbatim quote of the linked dupe's accepted answer. (Although I'd recommend using rename myself.) – Sparhawk Oct 28 '18 at 06:23
  • As this script may answer the question some explanations can make the answer better – Romeo Ninov Oct 28 '18 at 06:36
  • yes, I also read answers, but find it very tricky to understand, could you please leave an answer or introduce basic material for ${i%.md} @RomeoNinov – Wizard Oct 28 '18 at 06:38
  • 2
    @avirate Your question is a duplicate of another question. If you need clarification of that other question, it would be best to ask a new question. (Or just Google "Linux variable percent" or similar.) – Sparhawk Oct 28 '18 at 07:05
  • search for bash strings manipulation || the% cuts the final! creates an example variable || var = "file.txt" || echo $ var file.txt ||

    now I'll cut the end ".txt" ||

    echo $ {var% .txt} file |||

    how can you see this and the result!

    now I'll concatenate with .sh echo $ {var $ .txt} .sh file.sh

    so it works

    – noob Oct 28 '18 at 12:02