Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the extension name in a string with bash?

Tags:

bash

I want to translate the following python code to bash. The code changes the extension name to .html and runs Safari to open it.

#!/usr/bin/env python import os.path import os  oldName = $TM_FILEPATH (name, ext) = os.path.splitext(oldName) rename = name + ".html" os.system("open -a Safari %s" % rename) 

How can I change the file extension with bash?

like image 645
prosseek Avatar asked Dec 10 '10 16:12

prosseek


People also ask

How do you rename a file extension in Linux?

Renaming in Linux In the Linux command line, you can rename a file and file extension using the mv (move) command as shown. In the example above, the "hope. txt" text file would be renamed to "hope. html".

How do you change a string in bash?

The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.

How do you remove a filename extension in Unix?

You should be using the command substitution syntax $(command) when you want to execute a command in script/command. name=$(echo "$filename" | cut -f 1 -d '. ')


2 Answers

file=somefile.whatevs open -a Safari "${file%.*}.html" 
like image 112
Ignacio Vazquez-Abrams Avatar answered Sep 25 '22 22:09

Ignacio Vazquez-Abrams


If you happen to know the extension you can switch it like this:

$ MY_FILE=file.html $ NEW_EXT=${MY_FILE/html/php} $ echo ${NEW_EXT} file.php 
like image 42
dinigo Avatar answered Sep 24 '22 22:09

dinigo