[英]Search for text files in a directory and append a (static) line to each of them
I have a directory with many subdirectories and files with suffixes in those subdirectories (eg FileA-suffixA FileB-SuffixB FileC-SuffixC FileD-SuffixA
, etc). 我有一个目录,其中包含许多子目录和这些子目录中带有后缀的文件(例如
FileA-suffixA FileB-SuffixB FileC-SuffixC FileD-SuffixA
等)。
How can I recursively search for files with a certain suffix, and append a user-defined line of text to those files? 如何递归搜索具有特定后缀的文件,并将用户定义的文本行附加到这些文件? I feel like this is a job for grep and sed, but I'm not sure how I would go about doing it.
我觉得这是grep和sed的工作,但我不确定如何去做。 I'm fairly new to scripting, so please bear with me.
我对脚本还很陌生,所以请多多包涵。
You can do it like 你可以做到
find /where/to/search -type f -iname '*.SUFFIX' -exec echo "USER DEFINED STRING" >> \{\} \;
find
searches in the suplied path find
搜索 -type f
finds only files -type f
仅查找文件 -iname '*.SUFFIX'
find the .SUFFIXed names, case ignored -iname '*.SUFFIX'
查找.SUFFIXed名称,忽略大小写 find ./ -name "*suffix" -exec bash -c 'echo "line_to_add" >> $1' -- {} \;
Basically you use find to get a list of the files. 基本上,您使用find来获取文件列表。 Then you use bash to echo append your line to that list.
然后,使用bash回显将行添加到该列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.