gpt4 book ai didi

linux - 使用目录名称作为模式递归重命名数百个文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:02 24 4
gpt4 key购买 nike

当前结构:

Cat and wolf
----- Volume 1.zip
----- Volume 2.zip
Cat and fox
---- Volume 01.rar
Rat and eagle
---- Rat and eagle 01.7x

如您所见,该结构并不遵循简单的模式。我希望它成为文件夹名称+编号。例如:

Cat and wolf 01.zip
Cat and wolf 02.zip
Cat and fox 01.rar
Rat and eagle 01.7z

有什么方法可以达到这个结果?

最佳答案

这是一个相当具有挑战性的问题。最棘手的方面是递归的要求。但是,正如您所指出的,命名模式并不简单。为了构建目标文件名,我们必须从原始文件名词干中解析出尾随数字,并将其连接到递归构建的目录名前缀上。

下面是我的解决方案,用 bash 实现。它遍历给定基目录中的所有对象。对于子文件,它会将它们移动到所需文件名下的目标目录中,而对于子目录,它会递归,构建一个连接在分隔符字符串上的目录名前缀。初始基目录、目标目录和分隔符字符串都被参数化为函数参数。我也为方便起见,在处理完基本目录后将其删除,但前提是当时发现它是空的。

function renameSubFilesToDest {

local base;
local dest;
local sep;
local prefix;

local file;
local fileBase;
local -i num;
local ext;
local new;
local -i rc;

## parse arguments
if [[ $# -lt 1 ]]; then echo 'too few arguments.' >&2; return 1; fi;
if [[ $# -gt 4 ]]; then echo 'too many arguments.' >&2; return 1; fi;
base="$1"; ## unconditionally take base dir as 1st arg
## take destination dir as optional 2nd arg, default to base if not given
if [[ $# -ge 2 ]]; then dest="$2"; else dest="$base"; fi;
## take name separator as optional 3rd arg, default to space if not given
if [[ $# -ge 3 ]]; then sep="$3"; else sep=' '; fi;
## take new name prefix as optional 4th arg, default to empty string if not given
if [[ $# -ge 4 ]]; then prefix="$4"; else prefix=''; fi;

## iterate over all objects in the base dir
for file in "$base"/*; do
fileBase="$(basename -- "$file";)";
if [[ -d "$file" ]]; then
## recurse on dir, appending file basename and trailing sep to prefix
renameSubFilesToDest "$file" "$dest" "$sep" "$prefix$fileBase$sep";
rc=$?; if [[ $rc -ne 0 ]]; then return 1; fi;
else
## don't process files at the first depth level
if [[ -z "$prefix" ]]; then continue; fi;
## parse num and ext from file name using bash extended regular expressions
if [[ ! "$fileBase" =~ ([1-9][0-9]*)\.([^.]+)$ ]]; then
echo "warning: file \"$fileBase\" does not match expected pattern." >&2;
continue;
fi;
num=${BASH_REMATCH[1]};
ext=${BASH_REMATCH[2]};
## derive the final file name in dest
new="$dest/$prefix$(printf %02d $num).$ext";
printf '%s -> %s\n' "$file" "$new"; ## print status messages at run-time
mv -i -- "$file" "$new";
rc=$?; if [[ $rc -ne 0 ]]; then echo "error: mv [$rc]." >&2; return 1; fi;
fi;
done;

## for convenience, remove the dir if it is now empty
find "$base" -maxdepth 0 -empty -delete;

return 0;

} ## end renameSubFilesToDest()

这是即将到来的演示的一个小帮助函数,它只是在当前目录中设置您的输入文件结构:

function setupDemo {
mkdir Cat\ and\ wolf Cat\ and\ fox Rat\ and\ eagle;
touch Cat\ and\ wolf/Volume\ {1,2}.zip;
touch Cat\ and\ fox/Volume\ 01.rar
touch Rat\ and\ eagle/Rat\ and\ eagle\ 01.7z
} ## end setupDemo()

这是输入文件结构上的函数演示:

setupDemo; ## set up the file structure

find *; ## show it
## Cat and fox
## Cat and fox/Volume 01.rar
## Cat and wolf
## Cat and wolf/Volume 1.zip
## Cat and wolf/Volume 2.zip
## Rat and eagle
## Rat and eagle/Rat and eagle 01.7z

renameSubFilesToDest .; ## run the solution
## ./Cat and fox/Volume 01.rar -> ./Cat and fox 01.rar
## ./Cat and wolf/Volume 1.zip -> ./Cat and wolf 01.zip
## ./Cat and wolf/Volume 2.zip -> ./Cat and wolf 02.zip
## ./Rat and eagle/Rat and eagle 01.7z -> ./Rat and eagle 01.7z

find *; ## show the result
## Cat and fox 01.rar
## Cat and wolf 01.zip
## Cat and wolf 02.zip
## Rat and eagle 01.7z

正如 GhostCat 在他的评论中指出的那样,该测试用例在技术上并未涵盖真正的递归文件结构,这至少需要两级子目录。我将我的解决方案写成完全递归的,因为您在标题中暗示递归是目标,并且无论如何,完全递归的解决方案将更通用并且可能更有用。这是第二个级别的演示,我们如何更改此演示的分隔符:

mkdir -p level\ one/level\ two;
touch level\ one/level\ two/some\ file\ 7.txt;

find *;
## level one
## level one/level two
## level one/level two/some file 7.txt

renameSubFilesToDest . . _;
./level one/level two/some file 7.txt -> ./level one_level two_07.txt

find *;
## level one_level two_07.txt

关于linux - 使用目录名称作为模式递归重命名数百个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com