Artificial intelligent assistant

rename all files in a folder deleting duplicate string-parts I have a folder with lots of subfolders, that contain two times the same but slightly different: Movie1 {Action}{Adventure}{Sci-Fi}{Thriller}{Science Fiction}/ Movie2 {Action}{Adventure}{Thriller}{Science Fiction}/ Movie3 {Action}{Adventure}{Thriller}{Sci-Fi}}/ Movie4 {Action}{Adventure}{Thriller}/ How do I unify these by deleting the part "{Science Fiction}" where "{Sci-Fi}" already exists, renaming the fodlers that dont't contain "{Sci-Fi}" but only "{Science Fiction}"? I would go for a for loop: for f in *; do if [ *"{Science Fiction}"* == "$f" ] && [ *"{Sci-Fi}"* == "$f" ]; then #delete the "{Science Fiction}" part else ... fi done But that doesn't seem very elegant. is there a cleaner solution?

You can use sed to remove the duplicates from string:


for f in *; do
r=$(echo $f | sed -r "s/(.*)(\{Sci-Fi\}|\{Science Fiction\})(.*)(\{Sci-Fi\}|\{Science Fiction\})(.*)/\1\2\3\5/g");
echo $r;
done


Replace `echo $f` with `mv "$f" "$r"` if you like the output.

The above `sed` line will take the first matching word and remove the second, if you want to always priorize `Sci-Fi` over `Science Fiction`, even when only `Science Fiction` exists, you can do it in two steps:


for f in *; do
r=$(echo $f | sed "s/{Science Fiction}/{Sci-Fi}/");
s=$(echo $r | sed -r "s/(.*)(\{Sci-Fi\})(.*)(\{Sci-Fi\})(.*)/\1\2\3\5/g");
if [ "$f" != "$s" ]; then
echo "moving " $f " to " $s
fi
done

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy e99e2533a0c80028927d0831fea0290e