简体   繁体   English

如何查找多个文件,检查重复项并替换为 bash、sed 脚本

[英]How to find multiple files, check for duplicates and replace with bash,sed script

I'm looking to have a script, that help me to search the files with help of bash and sed command, that has extension (.bak.alt) and then change the name of the file, if its not already existent.我正在寻找一个脚本,它可以帮助我在 bash 和 sed 命令的帮助下搜索文件,该文件具有扩展名 (.bak.alt),然后更改文件名(如果文件名尚不存在)。 I mean search for (*.bak.alt *.BAK.alt *.bak) and replace it with (*.BAK) if its not already existed and should compare the new one after changing it with the old one and then react, if its the same content and name, do nothing, if not then change it.我的意思是搜索(*.bak.alt *.BAK.alt *.bak)并将其替换为 (*.BAK) 如果它不存在并且应该在将其与旧的更改后比较新的然后做出反应,如果它的内容和名称相同,则什么都不做,如果不是,则更改它。

i have figure something with help of a friend but its not complete.我在朋友的帮助下想出了一些东西,但它并不完整。

#!/bin/bash

        echo " Finds something.."

        find . -type f \( -name "*.bak.alt" \) > F1.txt
        cat F1.txt
        find . -type f \( -name "*.BAK.alt" \)> F2.txt
        cat F2.txt

        if diff F1.txt F2.txt
                then echo "everythings looks good":
        else
              sed -E 's/^\(.*)\.(bak|BAK)\.alt$/mv "&" "\1.BAK"/;e'| wc -l    echo " Files changed "
fi

Something like this (untested) is what you need:你需要的是这样的(未经测试):

#!/usr/bin/env bash
shopt -s nocasematch

while IFS= read -r old; do
    new="${old/%.alt}"
    new="${new/%.bak}"
    [[ ! -e "$new" ]] && echo mv -- "$old" "$new"
done < <( find . -type f \( -name '*.bak.alt' -o -name '*.BAK.alt' -o -name '*.bak' \) )

Remove the echo when you're done testing and want it to actually do the mv .完成测试并希望它实际执行mv删除echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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