Friday, May 3, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 8916  / 2 Years ago, mon, september 26, 2022, 9:58:33

In terminal using for loop we can add a part before or after the file name. But if we want to remove it how do we do this?
For example we can add .old after all .txt files name using the command bellow



$for i in *.txt
>do
> mv $i $i.old
> done


But my question is how do we remove this .old just using the same technique?


More From » command-line

 Answers
5
for i in *.txt; do mv $i ${i%%.txt}; done


It's a simple for cycle, just like the one you wrote. Note I've used ; instead of newlines so that I can type it into a single line. The only construct that is new to you is the ${i%%.txt}, which simply means $i, with whatever following the %% signs cut off from the end of $i.



A good tip: if unsure what would happen, try adding echo in front of the actual command, so that you would see the exact commands that are to be executed. E.g.:



for i in *.txt; do echo mv $i ${i%%.txt}; done 

[#29331] Tuesday, September 27, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
suspengn

Total Points: 477
Total Questions: 104
Total Answers: 100

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
suspengn questions
;