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?
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".
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.
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 '. ')
file=somefile.whatevs open -a Safari "${file%.*}.html"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With