简体   繁体   English

在具有唯一名称的所有子文件夹中创建空文件--bash

[英]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.

相关问题 bash列出所有目录,子目录,文件夹,子文件夹,然后输出到单独的文件 - bash list all directories, subdirectories, folders, subfolders in then output to a separate file 在文件夹bash中创建每个子文件夹的文件列表 - Creating list of files of every subfolders in folders bash 如何使用 linux bash 将特定文件夹下所有子文件夹的名称仅列出到变量中 - How to list only name of all subfolders under specific folder into a variable using linux bash 在 bash 中创建空文件的简写在 zsh 中无法正常工作 - shorthand for creating empty file in bash not working properly in zsh 创建一个 bash 脚本,将“watch lsusb”的输出记录到一个空文件中 - Creating a bash script that logs the output of 'watch lsusb' into an empty file 将文件夹拆分为多个文件夹,而无需在终端/ bash中创建子文件夹 - Splitting a folder into multiple folders without creating subfolders in terminal/ bash Bash - 列出所有子目录中的程序,目录名在文件之前 - Bash - listing programs in all subdirectories with directory name before file 使用 bash 脚本在文件中显示 Linux 中所有正在运行的进程的名称 - Display the name of all running processes in Linux in a file using a bash script 读取输出日志文件,并使用 bash/python 脚本打印所有唯一的文件路径 - read output log file, and print all unique file paths using bash/python script 在Bash中解析文件名 - Parsing file name in Bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM