V
V
Vadim2021-08-13 14:48:07
linux
Vadim, 2021-08-13 14:48:07

How to find and rename all files by removing a substring (-git)?

seems simple, but these two simple scripts don't work (don't want to use/set rename)

find . -type f -name "*.txt" -exec sh -c 'mv "$0" $(echo -n "${$0/-git/}")' '{}' \;


or such

find . -type f -name "*.txt" -print0 | xargs -0 -I{} fname={} && mv "$fname" $(echo -n "${$fname/-git/}")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
3
3vi1_0n3, 2021-08-13
@Viji

#!/bin/bash

find . -name "*-git*.txt" | (
IFS='
'
while read oldname
do
newname=${oldname//-git}
mv "$oldname" "$newname"
done
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question