Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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/awk或类似工具将整个目录树转换为大写文件名和目录名?_Bash_Unix_Awk_Filenames - Fatal编程技术网

如何使用bash/awk或类似工具将整个目录树转换为大写文件名和目录名?

如何使用bash/awk或类似工具将整个目录树转换为大写文件名和目录名?,bash,unix,awk,filenames,Bash,Unix,Awk,Filenames,为了匹配,我需要将整个大文件系统(文件名和目录名)转换为大写字母。如果我们可以创建指向orig文件名的链接,其中链接名应该与orig文件名相同,但为大写,那就太好了。此外,链接的dir.-树也应该相同,但还是大写的 这里有人知道怎么处理吗? 非常感谢 谢谢你下面的建议。我现在可以这样做,但它仍然是马车。 我不能很快找到它。知道臭虫在哪里吗 #! /bin/bash walk () { local dir=$1 cd "$dir" || retur

为了匹配,我需要将整个大文件系统(文件名和目录名)转换为大写字母。如果我们可以创建指向orig文件名的链接,其中链接名应该与orig文件名相同,但为大写,那就太好了。此外,链接的dir.-树也应该相同,但还是大写的

这里有人知道怎么处理吗? 非常感谢

谢谢你下面的建议。我现在可以这样做,但它仍然是马车。 我不能很快找到它。知道臭虫在哪里吗

    #! /bin/bash

    walk () {
        local dir=$1
        cd "$dir" || return
        local f
        for f in .* * ; do
            if [[ $f == . || $f == .. ]] ; then
                continue  # Skip the special ones
            fi

            if [[ $f == *[[:lower:]]* ]] ; then  # Skip if no lowercase
                mkdir -p "$2""/""$dir" && ln -s "$(pwd)""/""$f" "$2""/""$dir""/""${f^^}"
            fi
            if [[ -d "$f" ]] ; then
                walk "$f" "$2"
            fi
        done
        cd ..
    }

    walk "$1" "$2"

扩展我的评论,类似这样的内容将使它们以数组的形式组合在一起:

original_pathnames=()
uppercase_pathnames=()

pathname="/path/to/my/file.ext"

#I suppose you could put this into a loop where you pass a giant list of files (find?)
########################################################################################

original_pathnames+=("$pathname")
uppercase_pathnames+=("$(echo "$pathname" | tr "[:lower:]" "[:upper:]")")

########################################################################################

乔博达说得很好。如果有两个文件进行不区分大小写的匹配,则在匹配过程中会发生冲突。你也必须处理好这个案子,但这是一个好的开始。我也不确定你从哪里得到目录树列表。我会使用
find
获取文件列表,然后使用
IFS=$'\n'
循环,但是如果您使用
find
,则
-iname
标志将为您执行不区分大小写的搜索。

您可以使用递归函数

我使用了^^参数扩展,因此我不必向外抛出到
tr
,它应该更快

#! /bin/bash

walk () {
    local dir=$1
    cd "$dir" || return
    local f
    for f in .* * ; do
        if [[ $f == . || $f == .. ]] ; then
            continue  # Skip the special ones
        fi

        if [[ $f == *[[:lower:]]* ]] ; then  # Skip if no lowercase
            ln -s "$f" "${f^^}"
        fi
        if [[ -d "$f" ]] ; then
            walk "$f"
        fi
    done
    cd ..
}

walk "$1"

您可能会遇到一些错误,例如当
文件
文件
存在于同一目录中时,或者如果无法输入该目录。

echo“$pathname”| tr”[:lower:][:upper:][
如果文件
fileA
fileA
存在于同一目录中,该怎么办?也许可以考虑改进匹配的代码…非常感谢,我知道了,我可以改进和学习。谢谢,还有一个问题。我想将新的大写链接写入一个单独的目录。您的解决方案将链接写入同一位置。如何调整您的解决方案以再次满足我的要求?可能在
${f^^^}
之前预先添加
$前缀
,或者替换已知的初始部分?非常感谢,我需要一些时间来解释您的智能代码!