0

I have a bunch of files in bash named myfile1.sh, myfile2.sh, myfile3.sh... and so on. I would like to remove the sh part in each one of them, that is: rename to myfile1, myfile2, myfile3, ... Do you have a suggestion to do it all at once?

Thanks in advance.

David
  • 1,155
  • 1
  • 13
  • 35
  • Possible duplicate of [Add file extension to files with bash](https://stackoverflow.com/questions/6114004/add-file-extension-to-files-with-bash) – kvantour Jul 20 '18 at 09:10
  • 1
    The first line of [this answer](https://stackoverflow.com/a/6114049/8344060) is what you are interested in. – kvantour Jul 20 '18 at 09:13

2 Answers2

2
for i in *.sh; do mv "$i" "${i%.sh}"; done
Hielke Walinga
  • 2,677
  • 1
  • 17
  • 30
0

If you have the rename command, you can use it:

rename 's/\.sh$//' *.sh

A bash one-liner can be:

for f in *.sh; do mv "$f" "${f%.sh}"; done
P.P
  • 117,907
  • 20
  • 175
  • 238
  • 1
    Rather than removing the first instance of `.sh` from the file, it would make more sense to remove it from the end using `"${f%.sh}"` – Tom Fenech Jul 20 '18 at 09:40
  • Or you could add the `%` anchor to your substitution: `${file/%.sh}`. – PesaThe Jul 20 '18 at 10:02
  • @TomFenech That's indeed better. Thanks. – P.P Jul 20 '18 at 10:03
  • 2
    YIKES!!!! Don't do that!!!! The dot matches anything including `A`... try `touch GreyAshenTiles.txt` and running the command with the `--dry-run` option. At the very least use `s/.sh$//`, preferably `s/\.sh$//` and don't wildcard at the end, just use `rename ... *.sh` – Mark Setchell Jul 20 '18 at 10:07