linux - 如何根据用户选择按编号顺序创建多个文件?

标签 linux bash ubuntu

到目前为止,我有这个,我在所选文件夹中创建了我想要的文件夹数量。创建的文件夹数量取决于我的选择。
如何在每个文件夹中制作文件?我希望它们根据我选择的文件数量从 1 开始编号。

read -p "Enter the number of folders to create: " FOLDER_X
read -p "Enter the number of files to create: " FILE_Y

for (( x = 1; x <= FOLDER_X; x++ )); do 
mkdir $DIRY/$x

done

for i in {"$FILE_Y"}
do
echo hello > "$i.txt"
done

最佳答案

#!/bin/bash

shopt -s extglob
DIR=/path/to/somewhere

while :; do
    read -p "Enter the number of folders to create: " folders
    read -p "Enter the number of files to create: " files
    [[ $folders,$files == +([[:digit:]]),+([[:digit:]]) ]] && break
    echo "Please provide numeric inputs."
done

for (( i = 0; i < folders; ++i )); do
    mkdir -p "$DIR/$i" || break

    for (( j = 0; j < files; ++j )); do
        echo hello > "$DIR/$i/$j.txt" || break 2
    done
done
阅读 Bash Manual .

关于linux - 如何根据用户选择按编号顺序创建多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70642630/

相关文章:

linux - 在 Git 中使用两个存储库和两个项目

c - 检测何时在 C 中连接了 USB 设备

bash - 验证参数数量

bash - 在特定目录中运行 `ng build`

node.js - Yarn 错误地报告不兼容的 Node 版本失败

reactjs - 我正在运行 nginx,但它不服务于我的 React 构建

java - 如何在 Linux (Centos 7) shell 上在后台正确运行 Spring Boot Batch 应用程序?

linux - 如何使用 nohup 在 linux 中将进程作为后台进程运行?

bash - 调用者 0 在陷阱处理程序中给出了错误的行号

bash - Foreman 和 PATH 环境变量