-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?

AbstProcDo
  • 2,453
  • 4
  • 23
  • 55
  • Also dupe of [this](https://unix.stackexchange.com/questions/238856/find-all-filename-mp4-and-rename-filename-audio), [this](https://unix.stackexchange.com/questions/205466/how-to-change-suffix-of-the-files), [this](https://unix.stackexchange.com/questions/339697/change-only-the-extension-of-a-file)… etc. – Sparhawk Oct 28 '18 at 05:17

1 Answers1

1
for i in *.md; do mv -- "$i"  "${i%.md}.sh"; done
Romeo Ninov
  • 16,541
  • 5
  • 32
  • 44
noob
  • 22
  • 2
  • 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 – AbstProcDo 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