如何在无限循环中通过复制文件来更改文件名

时间:2019-01-11 12:50:31

标签: bash

所以我有一个循环,该循环将在循环中复制一个名为file1的文件,怎么可能永远复制一个名为file2,file3等等的文件?

2 个答案:

答案 0 :(得分:0)

假设(我不推荐(我真的不推荐(使用后果自负))):

#!/bin/bash

# define 'i' as the variable to contain a number
i=0
# open while loop to cycle condition true (infinitely until you quit the application)
while true; do
# Iterate i = i + 1  (which works because you've defined it as a number previously)
((i++))
# The copy operation which will copy file to file1, file2 file3 ...
cp file file$i
done

答案 1 :(得分:0)

如果我正确理解您的X文件名为myfile1,myfile2,...,myfileX。因此,不是无限循环:)也许是这样的:

for f in $(ls myfile*) 
do
  cp $f $f.copy
done