Generating Regular Files Sequentially with Bash seq Command
This article introduces how to regularly generate files with content using the seq command in Bash, the Linux command line language.
Procedure
When you execute the following command, files numbered from 1 to 3 (1.txt, 2.txt, 3.txt) are generated, and each file contains its corresponding number.
ASUS:/mnt/c/pg/$ seq 1 3 | xargs -I {} bash -c "echo {} > {}.txt"
ASUS:/mnt/c/pg/$ ls
1.txt 2.txt 3.txt
ASUS:/mnt/c/pg/$ cat 1.txt 2.txt 3.txt
1
2
3
Command Details
-
seq 1 3:- Outputs consecutive numbers from 1 to 3.
-
xargs -I {} bash -c "echo {} > {}.txt":- The
xargscommand receives the output from theseqcommand and executes theechocommand for each number. -I {}is a placeholder forxargs, where each number is inserted.bash -c "echo {} > {}.txt"is a Bash command that uses each number as a filename and writes that number to the file.
- The
Verifying the Results
After executing the command, files are generated as follows, with each file containing its corresponding number:
ASUS:/mnt/c/pg/$ ls
1.txt 2.txt 3.txt
ASUS:/mnt/c/pg/$ cat 1.txt 2.txt 3.txt
1
2
3
This confirms how to regularly generate files with content. For more detailed usage examples and applications, please refer to Bash documentation and related resources.