如何将整个目录树转换为大写的文件名和带有 bash/awk 或类似名称的目录名?
How can I convert a whole directory tree into uppercase filenames and directory names with bash/awk or similar?
出于匹配目的,我需要将整个大文件系统(文件名和目录名)转换为大写字母。如果我们可以创建指向原始文件名的链接,其中链接名应与原始文件名相同但为大写,那就太好了。此外,链接的目录树也应该相同,但又是大写。
这里有人知道如何处理吗?
非常感谢。
感谢您提供以下建议。我现在可以这样做,但它仍然存在问题。
而且我无法快速找到它。知道错误在哪里吗?
#! /bin/bash
walk () {
local dir=
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 """/""$dir" && ln -s "$(pwd)""/""$f" """/""$dir""/""${f^^}"
fi
if [[ -d "$f" ]] ; then
walk "$f" ""
fi
done
cd ..
}
walk "" ""
扩展我的评论,像这样的东西将把它们放在数组中:
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:]")")
########################################################################################
chorboda 提出了一个很好的观点。如果您有 2 个文件进行不区分大小写的匹配,这将在您的匹配过程中发生冲突。您也必须处理这种情况,但这是一个好的开始。我不确定您从哪里获取目录树列表。我会使用 find
获取文件列表,然后使用 IFS=$'\n'
循环,但如果您使用 find
,则 -iname
标志将不区分大小写地搜索你.
您可以使用递归函数。
我使用 ^^ 参数扩展,所以我不必 shell 输出到 tr
,它应该更快。
#! /bin/bash
walk () {
local dir=
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 ""
您可能会遇到一些错误,例如,当 FILE
和 file
存在于同一目录中,或者无法进入该目录时。
出于匹配目的,我需要将整个大文件系统(文件名和目录名)转换为大写字母。如果我们可以创建指向原始文件名的链接,其中链接名应与原始文件名相同但为大写,那就太好了。此外,链接的目录树也应该相同,但又是大写。
这里有人知道如何处理吗? 非常感谢。
感谢您提供以下建议。我现在可以这样做,但它仍然存在问题。 而且我无法快速找到它。知道错误在哪里吗?
#! /bin/bash
walk () {
local dir=
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 """/""$dir" && ln -s "$(pwd)""/""$f" """/""$dir""/""${f^^}"
fi
if [[ -d "$f" ]] ; then
walk "$f" ""
fi
done
cd ..
}
walk "" ""
扩展我的评论,像这样的东西将把它们放在数组中:
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:]")")
########################################################################################
chorboda 提出了一个很好的观点。如果您有 2 个文件进行不区分大小写的匹配,这将在您的匹配过程中发生冲突。您也必须处理这种情况,但这是一个好的开始。我不确定您从哪里获取目录树列表。我会使用 find
获取文件列表,然后使用 IFS=$'\n'
循环,但如果您使用 find
,则 -iname
标志将不区分大小写地搜索你.
您可以使用递归函数。
我使用 ^^ 参数扩展,所以我不必 shell 输出到 tr
,它应该更快。
#! /bin/bash
walk () {
local dir=
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 ""
您可能会遇到一些错误,例如,当 FILE
和 file
存在于同一目录中,或者无法进入该目录时。