bash shell 克隆文件的脚本
bash shell script to clone files
cd /home/foo/
for f in `seq -w 01 10`
do
cat $f > /home/foo/cloned_files/"$f".$i
done
情况:
我在 /home/foo 中只有 1 个文件。
我想将该文件克隆 10 次并将迭代编号添加到文件名中。最后,我应该有 10 个内容相同但后缀表示其序列号的文件。
示例:
/home/foo/xfile.txt
执行脚本后,我应该:
xfile.txt.01, xfile.txt.02, xfile.txt.03...xfile.txt.10 在 /home/foo/cloned_files/
如有任何帮助,我们将不胜感激。谢谢
试试这个,使用你的标签的规范方式:在纯 bash :
for i in {01..10}; do
cp "filename" "/path/to/xfile.txt.$i"
done
如果您需要动态的整数范围:
integer=10 # <= variable
for i in $(seq -w 1 $integer); do
cp "filename" "/path/to/xfile.txt.$i"
done
就像@Charles Duffy 在评论中所说的那样,一个更便携的解决方案(seq 与 bash 无关,它是一个外部命令):
for ((i=1; i<=10; i++)); do
printf -v num '%02d' "$i"
cp "filename" "/path/to/xfile.txt.$num"
done
更进一步:TestsAndConditionals
man cp
:
CP(1)
NAME
cp - copy files and directories
备注:
- 2018 年停止使用反引号
`
,我们建议改用 $( )
学习如何正确引用,这在 shell 中非常重要:
"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var"
, "$(command "$var")"
, "${array[@]}"
, "a & b"
. Use 'single quotes'
for code or literal $'s: 'Costs US'
, ssh host 'echo "$HOSTNAME"'
. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
另一个选项使用 GNU Parallel
parallel --dry-run cp file /elsewhere/file.{} ::: {01..10}
示例输出
cp file /elsewhere/file.07
cp file /elsewhere/file.08
cp file /elsewhere/file.09
cp file /elsewhere/file.06
cp file /elsewhere/file.05
cp file /elsewhere/file.04
cp file /elsewhere/file.03
cp file /elsewhere/file.02
cp file /elsewhere/file.01
cp file /elsewhere/file.10
如果输出看起来不错,请再次删除 --dry-run
和 运行 命令以实际执行此操作。
cd /home/foo/
for f in `seq -w 01 10`
do
cat $f > /home/foo/cloned_files/"$f".$i
done
情况: 我在 /home/foo 中只有 1 个文件。 我想将该文件克隆 10 次并将迭代编号添加到文件名中。最后,我应该有 10 个内容相同但后缀表示其序列号的文件。
示例: /home/foo/xfile.txt
执行脚本后,我应该: xfile.txt.01, xfile.txt.02, xfile.txt.03...xfile.txt.10 在 /home/foo/cloned_files/
如有任何帮助,我们将不胜感激。谢谢
试试这个,使用你的标签的规范方式:在纯 bash :
for i in {01..10}; do
cp "filename" "/path/to/xfile.txt.$i"
done
如果您需要动态的整数范围:
integer=10 # <= variable
for i in $(seq -w 1 $integer); do
cp "filename" "/path/to/xfile.txt.$i"
done
就像@Charles Duffy 在评论中所说的那样,一个更便携的解决方案(seq 与 bash 无关,它是一个外部命令):
for ((i=1; i<=10; i++)); do
printf -v num '%02d' "$i"
cp "filename" "/path/to/xfile.txt.$num"
done
更进一步:TestsAndConditionals
man cp
:
CP(1)
NAME
cp - copy files and directories
备注:
- 2018 年停止使用反引号
`
,我们建议改用$( )
学习如何正确引用,这在 shell 中非常重要:
"Double quote" every literal that contains spaces/metacharacters and every expansion:
"$var"
,"$(command "$var")"
,"${array[@]}"
,"a & b"
. Use'single quotes'
for code or literal$'s: 'Costs US'
,ssh host 'echo "$HOSTNAME"'
. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
另一个选项使用 GNU Parallel
parallel --dry-run cp file /elsewhere/file.{} ::: {01..10}
示例输出
cp file /elsewhere/file.07
cp file /elsewhere/file.08
cp file /elsewhere/file.09
cp file /elsewhere/file.06
cp file /elsewhere/file.05
cp file /elsewhere/file.04
cp file /elsewhere/file.03
cp file /elsewhere/file.02
cp file /elsewhere/file.01
cp file /elsewhere/file.10
如果输出看起来不错,请再次删除 --dry-run
和 运行 命令以实际执行此操作。