bash 使用 shell 中的内容创建多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4140822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:51:38  来源:igfitidea点击:

Creating multiple files with content from shell

bashunixscripting

提问by Kevin

New to scripting. How can I write code to create multiple files (a.txt, b.txt, ... , z.txt)?

脚本新手。如何编写代码来创建多个文件(a.txt、b.txt、...、z.txt)?

Thanks.

谢谢。

回答by Paused until further notice.

One command to create 26 empty files:

创建 26 个空文件的一个命令:

touch {a..z}.txt

or 152:

或 152:

touch {{a..z},{A..Z},{0..99}}.txt

A small loop to create 152 files with some contents:

一个小循环来创建 152 个包含一些内容的文件:

for f in {a..z} {A..Z} {0..99}
do
    echo hello > "$f.txt"
done

You can do numbered files with leading zeros:

您可以使用前导零来编号文件:

for i in {0..100}
do
    echo hello > "File$(printf "%03d" "$i").txt"
done

or, in Bash 4:

或者,在 Bash 4 中:

for i in {000..100}
do
    echo hello > "File${i}.txt"
done

回答by cdhowie

echo Hello > a.txt
echo World > b.txt

for i in a b c d e f g; do
    echo $i > $i.txt
done

If you want more useful examples, ask a more useful question...

如果您想要更多有用的示例,请提出更有用的问题...

回答by vovan

To create files with names a.txtand b.txtsimple pass names to touch

创建具有名称a.txtb.txt简单传递名称的文件到touch

touch a.txt b.txt

回答by arush436

for i in {1..200}; do touch any_prefix_here${i}; done

where iis the count. So example files are employee1employee2etc... through to emplyee200

其中i是计数。所以示例文件是员工1员工2等...到员工200

回答by user3156262

to have some content in file use dd

要在文件中包含一些内容,请使用 dd

for i in {1..N};
do
dd if=/dev/urandom of=./path/file${i} bs=1M count=1;
done

回答by user472875

You can create a file using $ cat > a.txt. If you need to have a file with specific content, type $ echo content > filename.

您可以使用$ cat > a.txt. 如果您需要具有特定内容的文件,请键入$ echo content > filename.