我有这个脚本
rename_files() {
title="${1##*${2} - }"
for filename in "$1/"*.*; do
case "${filename##*.}" in
ext1|ext2|ext3)
mkdir "/User/Downloaded/${title}"
new_path="/User/Downloaded/${title}/${title}.${filename##*.}"
echo "moving $filename -> $new_path"
mv "$filename" "$new_path"
;;
esac
done
}
rename_category() {
for path in "/User/Downloads/${1}"*; do
rename_files "$path" "$1"
done
}
rename_category CAT1
rename_category CAT2
这样做
在
.
├── Downloads
│ ├── CAT1 - Something ── File.ext1
│ └── CAT2 - SomethingElse ── OtherFile.ext1
├── Downloaded
├── CAT1
├── CAT2
在
.
├── Downloads
│ ├── CAT1 - Something
│ └── CAT2 - SomethingElse
├── Downloaded
├── CAT1 ── Something ── Something.ext1
├── CAT2 ── SomethingElse ── SomethingElse.ext1
我要做的是,如果只有一个文件具有相同的扩展名,那么这些文件将被重命名为_1
等。
在
.
├── Downloads
│ ├── CAT1 - Something ├── File.ext1
├── ExampleFile.ext1
│ └── CAT2 - SomethingElse ── OtherFile.ext1
├── Downloaded
├── CAT1
├── CAT2
在
.
├── Downloads
│ ├── CAT1 - Something
│ └── CAT2 - SomethingElse
├── Downloaded
├── CAT1 ── Something ── Something_1.ext1
── Something_2.ext1
├── CAT2 ── SomethingElse ── SomethingElse.ext1
修改:
#!/bin/bash
rename_files() {
title="${1##*${2} - }"
for filename in "$1/"*.*; do
case "${filename##*.}" in
doc|ext|ext)
mkdir "/User/Downloaded/${title}"
new_path="/User/Downloaded/${title}/${title}.${filename##*.}"
let "iters=1"
while [ -f $new_path ] ; do
$new_path=$new_path"$iters"
let "iters++"
done
echo "moving $filename -> $new_path"
mv "$filename" "$new_path"
;;
esac
done
}
rename_category() {
for path in "/User/Downloads/${1}"*; do
rename_files "$path" "$1"
done
}
rename_category CAT1
EDIT2:
.doc
/User/Downloads/DOC - TEST
个文件
/User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc
/Sample-doc-file-200kb.doc
我想要的是
/User/Downloaded/TEST/TEST_1.doc
/TEST_2.doc
这是输出日志
chmod +x /User/TEST.sh
sh /User/TEST.sh
moving /User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc -> /User/Downloaded/TEST/TEST.doc
mkdir: /User/Downloaded/TEST: File exists
/User/TEST.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc1: No such file or directory
/User/TEST.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc2: No such file or directory
等
更新了输出日志
sh /User/Script.sh
moving /User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc -> /User/Downloaded/TEST/TEST.doc
mv: rename {/User/Downloads/DOC - TEST/Sample-doc-file-100kb.doc} to {/User/Downloaded/TEST/TEST.doc}: No such file or directory
mkdir: /User/Downloaded/TEST: File exists
moving /User/Downloads/DOC - TEST/Sample-doc-file-200kb.doc -> /User/Downloaded/TEST/TEST.doc
mv: rename {/User/Downloads/DOC - TEST/Sample-doc-file-200kb.doc} to {/User/Downloaded/TEST/TEST.doc}: No such file or directory
输出更新:
这是我最初的情况
User/Downloads/DOC - TEST/SampleDoc.doc
/OtherSampleDoc.doc
/OtherDoc.doc
User/Downloaded/
脚本之后我只获得
User/Downloads/DOC - TEST/OtherSampleDoc.doc
/OtherDoc.doc
User/Downloaded/TEST/TEST.doc
但我想要的是
User/Downloads/DOC - TEST
User/Downloaded/TEST/TEST_1.doc
/TEST_2.doc
/TEST_3.doc
这是输出
sh /User/Script.sh
moving /User/Downloads/DOC - TEST/OtherDoc.doc -> /User/Downloaded/TEST/TEST.doc
mkdir: /User/Downloaded/TEST: File exists
/User/Script.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc1: No such file or directory
/User/Script.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc2: No such file or directory
/User/Script.sh: line 12: /User/Downloaded/TEST/TEST.doc=/User/Downloaded/TEST/TEST.doc3: No such file or directory
etc.
答案 0 :(得分:1)
rename_files() {
title="${1##*${2} - }"
for filename in "$1/"*.*; do
case "${filename##*.}" in
ext1|ext2|ext3)
mkdir "/User/Downloaded/${title}"
new_path="/User/Downloaded/${title}/${title}.${filename##*.}"
let "iters=1"
while [ -f $new_path ] ; do
new_path=$new_path"$iters"
let "iters++"
done
echo "moving $filename -> $new_path"
mv "${filename}" "${new_path}"
;;
esac
done
}