【问题标题】:renaming the files recursively in unix在 unix 中递归地重命名文件
【发布时间】:2018-11-28 17:20:15
【问题描述】:

我知道这会被标记为重复的,但我尝试搜索谷歌,但我正在尝试的方法对我不起作用。

我在一个目录中有一些.txt 文件,我需要递归地将所有*.txt 文件重命名为*_XYZ.txt。 XYZ 在变量X 中定义。

我试过下面的代码:

file=`find ./ -type f -name "*.txt"|sed "s,^./,,g" |awk -F '.' '{print $1}'`

for i in "$file"
do
mv "$i" "$i_${X}.txt"
done

任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: unix rename ksh mv hp-ux


    【解决方案1】:

    您的脚本破坏了变量file 中的原始文件名,这就是它无法重命名文件的原因。

    工作示例:

    X="_XYZ"
    for f in $(find . -type f ! -name "*$X.txt" -name "*.txt"); do 
        mv "$f" "${f%.txt}$X.txt"
    done
    

    输出:

    $ X="_XYZ"
    $ find . -type f -name "*.txt"
    ./c_XYZ.txt
    ./aa/c.txt
    ./a.txt
    ./b.txt
    $ for f in $(find . -type f ! -name "*$X.txt" -name "*.txt"); do mv "$f" "${f%.txt}$X.txt"; done
    $ find . -type f -name "*.txt"
    ./b_XYZ.txt
    ./c_XYZ.txt
    ./aa/c_XYZ.txt
    ./a_XYZ.txt
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2019-08-27
    • 2014-02-07
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多