我有数千个名为“DOCUMENT.PDF”的文件,我想根据路径中的数字标识符重命名它们。不幸的是,我似乎无法访问重命名命令。
三个例子:
/000/000/002/605/950/ÐÐ-02605950-00001/DOCUMENT.PDF
/000/000/002/591/945/ÐÐ-02591945-00002/DOCUMENT.PDF
/000/000/002/573/780/ÐÐ-02573780-00002/DOCUMENT.PDF
要重命名为,而不更改其父目录:
2605950.pdf
2591945.pdf
2573780.pdf
答案 0 :(得分:0)
使用for循环,然后使用mv
命令
for file in *
do
num=$(awk -F "/" '{print $(NF-1)}' file.txt | cut -d "-" -f2);
mv "$file" "$num.pdf"
done
答案 1 :(得分:0)
您可以在Bash 4.0 +中使用globstar
执行此操作:
cd _your_base_dir_
shopt -s globstar
for file in **/DOCUMENT.PDF; do # loop picks only DOCUMENT.PDF files
# here, we assume that the serial number is extracted from the 7th component in the directory path - change it according to your need
# and we don't strip out the leading zero in the serial number
new_name=$(dirname "$file")/$(cut -f7 -d/ <<< "$file" | cut -f2 -d-).pdf
echo "Renaming $file to $new_name"
# mv "$file" "$new_name" # uncomment after verifying
done
请参阅此相关帖子,讨论类似问题:How to recursively traverse a directory tree and find only files?