[英]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
cd
into a directory to create a file there. 无需cd
进入目录即可在其中创建文件。 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. 如果您的文件或目录包含包含文字换行符或空格的名称,则解析它将会中断。 */
will match any directory in the current directory. 模式*/
将匹配当前目录中的任何目录。 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.