参考:How to search for all the files starting with the name “ABC” in a directory?
参考:Terminal Commands: For loop with echo
-
使用变量的时候需要 ${i} 这样包围住
- {01..10}, {1..10} 是不一样,前者默认前面都加 0,真是方便,随意
基本语法:
for i in ...; do ...; done or for i in ... do ... done
输出连续数字举例:
for i in {0..10}; do echo ${i}; done 0 1 2 3 4 5 6 7 8 9 10
or
for i in {0..10} do echo ${i} done 0 1 2 3 4 5 6 7 8 9 10
批量创建空文件举例:
for i in {01..10}; do touch text_${i}.txt; done # 生成文件 text_01.txt text_02.txt text_03.txt text_04.txt text_05.txt text_06.txt text_07.txt text_08.txt text_09.txt text_10.txt
批量修改文件名称举例:
for f in $(ls); do mv ${f} alex_${f}; done # 生成文件 alex_text_01.txt alex_text_02.txt alex_text_03.txt alex_text_04.txt alex_text_05.txt alex_text_06.txt alex_text_07.txt alex_text_08.txt alex_text_09.txt alex_text_10.txt