[英]Creating empty file in all subfolders with unique name - bash
Similar to this question 与此问题类似
but I want to have file name be the same name as the directory with "_info.txt" appended to it. 但是我想让文件名与附加了“_info.txt”的目录同名。
I have tried this 我试过这个
#!/bin/bash
find . -mindepth 1 -maxdepth 1 -type d | while read line; do
touch /Users/xxx/git/xxx/$line/$line_info.txt
done
It is creating ".txt" in each subdirectory. 它在每个子目录中创建“.txt”。
What am I missing ? 我错过了什么?
The crucial mistake is that the underscore character is a valid character for a bash variable name: 关键的错误是下划线字符是bash变量名的有效字符:
$ a=1
$ a_b=2
$ echo $a_b
2
There are several ways around this: 有几种方法:
$ echo "$a"_b
1_b
$ echo $a'_b'
1_b
$ echo ${a}_b
1_b
As for your task, here's a fast way: 至于你的任务,这是一个快速的方法:
find . -mindepth 1 -maxdepth 1 -type d -printf "%p_info.txt\0" | xargs -0 touch
The find -printf
prints the path of each directory, and printf
's %p
is unaffected by an underscore after it. find -printf
打印每个目录的路径, printf
的%p
不受其后的下划线的影响。 Then, xargs
passes the filenames as many arguments to few runs of touch
, making the creation of the files much faster. 然后,
xargs
将文件名传递给很少的touch
,这使得文件的创建速度更快。
Figured it out: 弄清楚了:
#!/bin/bash
find . -mindepth 1 -maxdepth 1 -type d | while read line; do
touch /Users/xxxx/git/xxxx/$line/"$line"_info.txt
done
I think I can still do the "find ." 我想我仍然可以做“找到”。 better as I have to run in directory I want the files added to.
更好,因为我必须在目录中运行我想要添加的文件。 Ideally it should be able to be run anywhere.
理想情况下它应该可以在任何地方运行。
Try this script : 试试这个脚本:
#!/bin/bash
find $1 -type d | while read line
do
touch "$line/$(basename $( readlink -m $line))_info.txt"
done
Save it as, say, appendinfo
and run it as 保存为,例如,
appendinfo
并将其运行为
./appendinfo directory_name_which_include_symlinks
Before 之前
ssam@udistro:~/Documents/so$ tree 36973628
36973628
`-- 36973628p1
`-- 36973628p2
`-- 36973628p3
3 directories, 0 files
After 后
ssam@udistro:~/Documents/so$ tree 36973628
36973628
|-- 36973628_info.txt
`-- 36973628p1
|-- 36973628p1_info.txt
`-- 36973628p2
|-- 36973628p2_info.txt
`-- 36973628p3
`-- 36973628p3_info.txt
3 directories, 4 files
Note : readlink canonicalizes the path by following the symlinks. 注意: readlink通过遵循符号链接规范化路径。 It is not required if, say, you're not going to give arguments like
.
例如,如果您不打算提供类似的论据,则不需要
.
. 。
If you are not recursing at all, you don't need find
. 如果您根本没有递归,则不需要
find
。 To loop over subdirectories, all you need is 要遍历子目录,您只需要
for dir in */; do
touch /Users/xxxx/git/xxxx/"$dir/${dir%/}_info.txt"
done
Incidentally, notice that the variable needs to be in double quotes always. 顺便提一下,请注意变量总是需要双引号。
尝试这个 :
find . /Users/xxx/git/xxx/ -mindepth 1 -maxdepth 1 -type d -exec bash -c 'touch ${0}/${0##*/}_info.txt' {} \;
You're missing the path to the folder. 您错过了该文件夹的路径。 I think you meant to write
我想你打算写
touch "./$folder/*/*_info.txt"
Although this still only works if you start in the right directory. 虽然这仍然只适用于从正确的目录开始。 You should really have written something like
你应该真的写过类似的东西
touch "/foo/bar/user/$folder/*/*_info.txt"
However, your use of "*/*_info.txt" is very... questionable... to say the least. 但是,你使用“* / * _ info.txt”非常......有问题......至少可以说。 I'm no bash expert, but I don't expect it to work.
我不是bash专家,但我不指望它会起作用。 Reading everything in a directory (using "find" for example, and only accepting directories) and piping that output to "touch" would be a better approach.
读取目录中的所有内容(例如,使用“查找”,仅接受目录)和输出“触摸”的管道将是更好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.