-1

Is it possible to write a script to rename all files after the extension?

Example in the folder, there are :

hello.txt-123ahr
bye.txt-56athe
test.txt-98hg12

I want the output:

hello.txt
bye.txt
test.txt
choroba
  • 231,213
  • 25
  • 204
  • 289

1 Answers1

3

If you just want to remove everything from the dash forwards, you can use Parameter expansion:

#!/bin/bash
for file in *.txt-* ; do
    mv "$file" "${file%-*}"
done

Where ${file%-*} means "remove from $file everytning from the last dash". If you want to start from the first dash, use %%.

Note that you might overwrite some files if their leading parts are equivalent, e.g. hello.txt-123abc and hello.txt-456xyz.

choroba
  • 231,213
  • 25
  • 204
  • 289