2

I should do a script with sed command to replace any extensions of a file with the extension .bak.

hello.txt -> hello.bak

How can I do? I'm trying from command line with

echo hello.txt | sed -e s/\.[0-9a-b]+$/\.bak

But it doesn't change anything.

Andrea
  • 21
  • 2
  • 1
    `sed` changes the contents of a file, not the file itself AFAIK. Look into the `rename` command. – LawrenceC Feb 22 '17 at 12:20
  • You technically *can* do this with `sed` and some shell magic, but it will be needlessly complicated. `rename` is the way to go. If you [edit] your question to ask *how to accomplish the goal of replacing the file extension* as opposed to *how to use `sed` to do it*, then we can provide answers with examples of how to do it; that's wasted effort though if you are dead-set on using `sed` specifically. – user Feb 22 '17 at 12:23
  • Under Linux try to read `man rename`... something like `rename txt bak` to rename all the files `*.txt` in `*.bak`. I think it comes with perl installed. BTW come on, join to SuperUser `;-)`. – Hastur Feb 22 '17 at 12:25
  • just show the shell magic. for x in ( hello.txt ) ; do mv $x $( echo $x | sed -e s/\.[0-9a-z]+$/\.bak ) ; done # (The word "done" is literal; use it. The # is also valid shell, and indicates the rest is comment.) I'm not posting as an answer since I'm too time-crunched to test the reg-ex part right now, but that should show the shell pieces. Note that the "hello.txt" in that example could be multiple space-separated filenames, or a shell wildcard (like simply using the * wildcard). I can put this into a proper answer later, but wanted to give an answer right now. – TOOGAM Feb 22 '17 at 13:11
  • Possible duplicate of [Recursively rename files (change extension) in Linux](https://superuser.com/questions/213134/recursively-rename-files-change-extension-in-linux) – kenorb Feb 22 '19 at 15:04

1 Answers1

0

For bash I have found some answers that might intrest you. This following question looks a lot like your question.

https://stackoverflow.com/questions/1224766/how-do-i-rename-the-extension-for-a-batch-of-files

In windows you can use the following: e.x

ren *.txt *.bat

this line changes the file extension of the file hello to .bat the * makes sure it selects every file with the extension .txt in the folder the cmd is openend and changes is to .bat

RamonRobben
  • 949
  • 6
  • 17
  • 1
    I strongly suspect that you are living in a Microsoft world. `ren` isn't available on most common \*nix systems (the basic command for renaming a file on a Unix-like OS is `mv`, shorthand for *move*), which I'm pretty sure OP wants answers for due to the use of `sed` (the *stream editor*) in the first place, and even if there was a `ren`, the shell would expand the asterisks so the command would likely not do what was intended anyway. – user Feb 22 '17 at 12:25
  • @MichaelKjörling ah okay. I'm sorry I didn't know sed was a linux command. I suspected it was a command in windows for very advanced users. I've added a link to a discussion that probably has the right answer. – RamonRobben Feb 22 '17 at 13:08