简体   繁体   中英

rename all files after extension

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

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, eg hello.txt-123abc and hello.txt-456xyz .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM