Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 1116  / 3 Years ago, thu, june 10, 2021, 10:34:37

I am new to scripting, and I have a directory with all files named num.pdb.ostat. I would like to rename all num.ostat (that is deleting the .pdb in all). For a single file this works:



mv 2.pdb.ostat 2.ostat


but when I try to do it for all files in folder with this script



for num in ./*; do mv ${num}.pdb.ostat ${num}.ostat; done


nothing happens



Can anyone tell me, where I went wrong?


More From » bash

 Answers
4

${num} takes the whole file name. You need to get filename without extension and add your new extension. You can make a string formatting. Use the following command:



for num in ./*; do mv ${num} ${num%.*.*}.ostat ; done


% deletes shortest match of $substring from back of $string.


[#21030] Friday, June 11, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ticrew

Total Points: 190
Total Questions: 111
Total Answers: 99

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
ticrew questions
Mon, Jan 10, 22, 07:20, 2 Years ago
Wed, Dec 8, 21, 19:25, 3 Years ago
Wed, May 5, 21, 08:17, 3 Years ago
Fri, Mar 10, 23, 17:21, 1 Year ago
Fri, Nov 4, 22, 06:43, 2 Years ago
;