使用 find 命令递归重命名每个目录中的最大 txt 文件,包括测试用例代码
recursively rename largest txt file in each directory using find command, Test Case Code included
我想要完成的是将每个目录中最大文件大小的 .txt 文件重命名为 keep.txt
这是一段代码,您可以运行设置测试用例:
mkdir -p './file test/1 first'
mkdir -p './file test/2 second yay'
echo 'other file' > './file test/1 first/1other file.nfo'
echo 'smallest file' > './file test/1 first/1smallest file.txt'
echo 'this is a medium sized file' > './file test/1 first/1medium file.txt'
echo 'this is the very largest file of all the files' > './file test/1 first/1keep largest file.txt'
echo 'other file in the folder thats even larger than everything' > './file test/2 second yay/2other file.nfo'
echo 'smallest file' > './file test/2 second yay/2smallest file.txt'
echo 'this is the very largest file of all the files' > './file test/2 second yay/2keep largest file.txt'
cd 'file test'
输出应如下所示:
tree -L 9
.
├── 1 first
│ ├── 1keep largest file.txt
│ ├── 1medium file.txt
│ ├── 1other file.nfo
│ └── 1smallest file.txt
└── 2 second yay
├── 2keep largest file.txt
├── 2other file.nfo
└── 2smallest file.txt
我放在一起的这个命令似乎 return 只是最大的 txt 文件,但它执行了多次,这似乎是问题的一部分:
find . -type f -execdir sh -c 'ls -S1 -d "$PWD/"* | grep txt | head -n 1' \;
这是我想重命名的,但它没有达到我的预期,我不知道为什么:
find . -type f -execdir sh -c 'ls -S1 -d "$PWD/"* | grep txt | head -n 1 | mv "{}" keep.txt' \;
目前,您正在为找到的每个文件执行 execdir 命令。
另一种方法是使用 find 的 printf 标志并仅打印文件的大小和 path/name,在仅打印最大文件的移动命令之前对输出进行排序并将命令通过管道传递给 sh执行。
find . -name "*.txt" -printf '%s,%f,%h\n' | sort -n | awk -F, '{ fil=;dir= } END { print "mv \""dir"/"fil"\" \""dir"/folder.jpg\"" }'
我们通过管道对输出底部的最大文件进行排序,然后通过管道通过 awk 获取最后一条记录,构建实际的 mv 命令。
确认 mv 命令符合预期后,通过管道传递到 sh 执行它,因此:
find . -name "*.txt" -printf '%s,%f,%h\n' | sort -n | awk -F, '{ fil=;dir= } END { print "mv \""dir"/"fil"\" \""dir"/folder.jpg\"" }' | sh
要对每个目录执行解决方案,运行:
while read line;do find "$line" -name "*.txt" -printf '%s,%f,%h\n' | sort -n | awk -F, '{ fil=;dir= } END { print "mv \""dir"/"fil"\" \""dir"/folder.jpg\"" }' | sh;done <<< "$(find . -type d)"
我想要完成的是将每个目录中最大文件大小的 .txt 文件重命名为 keep.txt
这是一段代码,您可以运行设置测试用例:
mkdir -p './file test/1 first'
mkdir -p './file test/2 second yay'
echo 'other file' > './file test/1 first/1other file.nfo'
echo 'smallest file' > './file test/1 first/1smallest file.txt'
echo 'this is a medium sized file' > './file test/1 first/1medium file.txt'
echo 'this is the very largest file of all the files' > './file test/1 first/1keep largest file.txt'
echo 'other file in the folder thats even larger than everything' > './file test/2 second yay/2other file.nfo'
echo 'smallest file' > './file test/2 second yay/2smallest file.txt'
echo 'this is the very largest file of all the files' > './file test/2 second yay/2keep largest file.txt'
cd 'file test'
输出应如下所示:
tree -L 9
.
├── 1 first
│ ├── 1keep largest file.txt
│ ├── 1medium file.txt
│ ├── 1other file.nfo
│ └── 1smallest file.txt
└── 2 second yay
├── 2keep largest file.txt
├── 2other file.nfo
└── 2smallest file.txt
我放在一起的这个命令似乎 return 只是最大的 txt 文件,但它执行了多次,这似乎是问题的一部分:
find . -type f -execdir sh -c 'ls -S1 -d "$PWD/"* | grep txt | head -n 1' \;
这是我想重命名的,但它没有达到我的预期,我不知道为什么:
find . -type f -execdir sh -c 'ls -S1 -d "$PWD/"* | grep txt | head -n 1 | mv "{}" keep.txt' \;
目前,您正在为找到的每个文件执行 execdir 命令。
另一种方法是使用 find 的 printf 标志并仅打印文件的大小和 path/name,在仅打印最大文件的移动命令之前对输出进行排序并将命令通过管道传递给 sh执行。
find . -name "*.txt" -printf '%s,%f,%h\n' | sort -n | awk -F, '{ fil=;dir= } END { print "mv \""dir"/"fil"\" \""dir"/folder.jpg\"" }'
我们通过管道对输出底部的最大文件进行排序,然后通过管道通过 awk 获取最后一条记录,构建实际的 mv 命令。
确认 mv 命令符合预期后,通过管道传递到 sh 执行它,因此:
find . -name "*.txt" -printf '%s,%f,%h\n' | sort -n | awk -F, '{ fil=;dir= } END { print "mv \""dir"/"fil"\" \""dir"/folder.jpg\"" }' | sh
要对每个目录执行解决方案,运行:
while read line;do find "$line" -name "*.txt" -printf '%s,%f,%h\n' | sort -n | awk -F, '{ fil=;dir= } END { print "mv \""dir"/"fil"\" \""dir"/folder.jpg\"" }' | sh;done <<< "$(find . -type d)"