【发布时间】:2021-04-13 03:50:42
【问题描述】:
我想要完成的是将每个目录中最大文件大小的 .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
我整理的这个命令似乎只返回最大的 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' \;
【问题讨论】: