简体   繁体   English

Linux shell:LOOP用于在每个文件夹中创建文件

[英]Linux shell: LOOP for create file in each folder

experts 专家

I want to create file in each folder . 我想在每个文件夹中创建文件。 Here is my command 这是我的命令

for i in `ls`;do cd $i;touch test.txt;done

-bash: cd: 10/: No such file or directory
-bash: cd: 2/: No such file or directory
-bash: cd: 3/: No such file or directory
-bash: cd: 4/: No such file or directory
-bash: cd: 5/: No such file or directory
-bash: cd: 6/: No such file or directory
-bash: cd: 7/: No such file or directory
-bash: cd: 8/: No such file or directory
-bash: cd: 9/: No such file or directory

It only generate test.txt in folder 1/ , the rest folder are blank. 它只在文件夹1/生成test.txt ,其余文件夹为空。 I think the reason is my command lack of { } to clarify the scale of LOOP for . 我认为原因是我的命令缺乏{ }来澄清LOOP的规模。

could you please help me update my command? 你能帮我更新一下我的命令吗?

for dir in */; do
   touch "$dir/test.txt"
done
  1. There's no need to cd into a directory to create a file there. 无需cd进入目录即可在其中创建文件。
  2. Don't parse the output of ls . 不要解析ls的输出。 The output of ls is only for looking at. ls的输出仅供查看。 Parsing it will break if your files or directories have names containing literal newlines or spaces. 如果您的文件或目录包含包含文字换行符或空格的名称,则解析它将会中断。
  3. The pattern */ will match any directory in the current directory. 模式*/将匹配当前目录中的任何目录。
  4. Quote your variable expansions. 引用你的变量扩展。 Your code would break if IFS is set to a digit. 如果IFS设置为数字,您的代码将会中断。

If you really need to do a cd into the directory, do it in a subshell. 如果你真的需要在目录中执行cd ,请在子shell中执行。 The changed working directory only affects the subshell and there is no need to cd back. 更改的工作目录仅影响子shell,无需cd返回。

for dir in */; do
   ( cd "$dir" && touch test.txt )
done

Let's consider that you have the following 10 folders in your current working directory: 我们假设您当前的工作目录中有以下10个文件夹:

tree .
.
├── 1
├── 10
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
└── 9

you can run the following command, to create your files: 您可以运行以下命令来创建文件:

for d in `find . -mindepth 1 -maxdepth 1 -type d`; do touch "$d"/test.txt; done

OUTPUT: OUTPUT:

tree .
.
├── 1
│   └── test.txt
├── 10
│   └── test.txt
├── 2
│   └── test.txt
├── 3
│   └── test.txt
├── 4
│   └── test.txt
├── 5
│   └── test.txt
├── 6
│   └── test.txt
├── 7
│   └── test.txt
├── 8
│   └── test.txt
└── 9
    └── test.txt

10 directories, 10 files

Explanations: 说明:

find . -mindepth 1 -maxdepth 1 -type d find . -mindepth 1 -maxdepth 1 -type d will get all the folders that are exactly 1 level under your current working folder if you omit -mindepth 1 then you will have a file created in your current working directory since . find . -mindepth 1 -maxdepth 1 -type d将获取当前工作文件夹下正好为1级的所有文件夹,如果省略-mindepth 1那么您将在当前工作目录中创建一个文件. will be selected, if you omit -maxdepth 1 then file will be created recursively at any depth level, also -type d will allow to filter only on directories. 将被选中,如果省略-maxdepth 1则将在任何深度级别递归创建文件,同时-type d将仅允许对目录进行过滤。

You can then use a loop to create the files or even xargs command would be enough 然后,您可以使用循环来创建文件,甚至xargs命令就足够了

You cd into a directory, but you don't cd back out. cd到一个目录,但你不cd退了出去。 Assuming that the first directory in the list is 1 , your script first changes into 1 , and next tries to change into 1/10 , which does not exist. 假设列表中的第一个目录为1 ,则您的脚本首先更改为1 ,然后尝试更改为1/10 ,这不存在。

You could do a cd - after touching the file. 你可以在触摸文件后做一张cd -

Even better, you avoid the cd altogether and do a touch $i/test.txt instead. 更好的是,你完全避免使用cd ,而是touch $i/test.txt

Of course, the script as written is not very robust: It breaks, if the current directory contains plain files, and it breaks if it contains entries which have spaces in their names - but this is a different issue. 当然,编写的脚本不是很健壮:如果当前目录包含普通文件,它会中断,如果它包含名称中包含空格的条目,它会中断 - 但这是一个不同的问题。

稍微改变你的命令,

for i in `ls -1`;do touch "$i"/test.txt;done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux Shell-在每个文件夹中创建文件夹和编号的文本文件 - Linux shell - Creating Folder and Numbered text files in each folder 在循环中使用两个变量来访问同一文件夹shell脚本linux中的两个diff文件类型 - Using two variables in a loop to access two diff file types in the same folder shell scripting linux 如何获取每个create table的第n个字段引用linux shell下的另一个文件? - How to get nth field of each create table refering to another file under linux shell? 通过php / shell脚本在linux中创建一个文件? - Create a file in linux via php/shell script? linux shell脚本在当前文件夹中运行可执行文件 - linux shell script to run an executable file in current folder Linux机器上每个键盘控制台外壳的设备文件保存在哪里? - Where are the device file kept for each keyboard console shell on a Linux machine? 目录中每个文件的Linux Shell脚本获取文件名并执行程序 - Linux Shell Script For Each File in a Directory Grab the filename and execute a program 如何在每个文件夹中创建一个文件? - How can I create a file in each folder? 如何为文件的每一行创建文件夹? - How to create folder per each line of a file? 如何在Linux中创建自动运行文件并执行Shell文件? - How to create a autorun file in linux and execute a shell file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM