2

我需要将文件从 File1、File2、... 调用到 File99。

我尝试了以下失败

cat test > {File1 .. File99}

没有 File 字样的命令不起作用。

4

6 回答 6

5
$ for i in {1..100}; do touch "File$i"; done 
于 2009-03-11T18:41:00.077 回答
5

只有一个命令:

touch File{1..99}
于 2009-03-11T22:48:47.813 回答
4

这取决于您使用的外壳。我假设您正在使用 Bash。

根据http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion,Bash 将扩展数字和单个字符。所以你的大括号表达式应该是:

File{1..99}

但我不认为重定向运算符“>”可以输出到多个文件。您可能需要使用循环:

for output in File{1..99}
do
    cat test > $output
done

或作为单行:

for output in File{1..99}; do cat test > $output; done
于 2009-03-11T18:40:07.320 回答
4

如果您更喜欢非循环版本,那么您可以使用 tee

cat test | tee File{1..99} > /dev/null
于 2009-03-11T19:10:28.917 回答
3

使用zsh(及其mult_ios),您可以:)

% zsh -c 'print test > file{1..3}' 
% head file*                      
==> file1 <==
test

==> file2 <==
test

==> file3 <==
test
于 2009-03-11T20:24:19.440 回答
1

If you want the files to sort properly (file01, file02 ... file10, etc.), do this:

for i in {0..10}; do i="0"$i; touch file${i: -2}; done

Which is the same as:

for i in {0..10}
do
    i="0"$i
    touch file${i: -2} # or cat file > file${i: -2}
done

There must be be a space between the colon and the dash in the substring expansion clause. You can start the ranges above with 1 if you want to start with "file01".

Or, a much more succinct way to have leading zeros:

touch file{0..9}{0..9}

and to use phyrex1an's technique with this one:

cat test | tee File{0..9}{0..9} > /dev/null

This does make one extra file "file00". You can also do "{2..3}{0..9}" for "file20" through "file39", but changing the ones digit (the second range) would cause parts of the sequence to be skipped.

于 2009-04-20T21:54:31.010 回答