Skip to content

Instantly share code, notes, and snippets.

@nasirkhan
Last active October 7, 2021 15:17
Show Gist options
  • Save nasirkhan/6990046 to your computer and use it in GitHub Desktop.
Save nasirkhan/6990046 to your computer and use it in GitHub Desktop.
Remove file extension using the linux shell script
```
#!/bin/sh
for file in `ls *.html`
do
newname=`echo $file|sed 's/\.html$//g'`
mv $file $newname
done
```
@seder001
Copy link

nice, but if you extend it a little bit it might be better

!/bin/sh

extension = '.html'
for file in ls *${extension}
do
newname=echo $file|sed 's/\${extension}$//g'
mv $file $newname
done

this should work, not tested.

@Popolon
Copy link

Popolon commented Sep 17, 2021

newname="${file//.html}"

So shorter:

ls *.html | while read file
do
  mv "${file}" "${file//.html}"
done

This is a really good reference to learn more about bash : https://tldp.org/LDP/abs/html/

About string manipulation : https://tldp.org/LDP/abs/html/string-manipulation.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment