Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用bash脚本更改多个文件的扩展名_Bash_Recursion_Rename - Fatal编程技术网

如何使用bash脚本更改多个文件的扩展名

如何使用bash脚本更改多个文件的扩展名,bash,recursion,rename,Bash,Recursion,Rename,我需要一个bash脚本来递归地重命名带有空白扩展名的文件,以便在末尾追加.txt。我找到了以下脚本,但我不知道如何使其递归: #!/bin/sh for file in *; do test "${file%.*}" = "$file" && mv "$file" "$file".txt; done 谢谢 谢谢。您可以将繁重的工作委托给find $ find . -type f ! -name "*.*" -print0 | xargs -0 -I file mv file f

我需要一个bash脚本来递归地重命名带有空白扩展名的文件,以便在末尾追加.txt。我找到了以下脚本,但我不知道如何使其递归:

#!/bin/sh
for file in *; do
test "${file%.*}" = "$file" && mv "$file" "$file".txt;
done
谢谢


谢谢。

您可以将繁重的工作委托给
find

$ find . -type f ! -name "*.*" -print0 | xargs -0 -I file mv file file.txt

假设没有扩展意味着名称中没有句点。

如果您不介意使用递归函数,那么您可以在较旧的Bash版本中使用:

shopt -s nullglob

function add_extension
{
    local -r dir=$1

    local path base
    for path in "$dir"/* ; do
        base=${path##*/}
        if [[ -f $path && $base != *.* ]] ; then
            mv -- "$path" "$path.txt"
        elif [[ -d $path && ! -L $path ]] ; then
            add_extension "$path"
        fi
    done

    return 0
}

add_extension .

mv--
用于防止以连字符开头的路径。

可以避免使用
xargs
find-f型-名称'*.'-exec mv{}{}.txt\。是的,对,它会消除一些噪音。第一个想法可能不是最好的。可能是重复的