Linux Unix命令在目录中所有文件的顶部插入注释
我想在Unix中为目录中的所有文件添加注释。如果我可以使用任何命令组合,请建议解决方案。尝试使用简单的Linux Unix命令在目录中所有文件的顶部插入注释,linux,file,shell,unix,find,ed,Linux,File,Shell,Unix,Find,Ed,我想在Unix中为目录中的所有文件添加注释。如果我可以使用任何命令组合,请建议解决方案。尝试使用简单的shell连接: for i in *; do { echo '# this is a comment'; cat "$i"; } > /tmp/_$$file && mv /tmp/_$$file "$i" done 使用find和sed: $ find . -maxdepth 1 -type f -exec sed -i '1i #comment' {
shell
连接:
for i in *; do
{ echo '# this is a comment'; cat "$i"; } > /tmp/_$$file &&
mv /tmp/_$$file "$i"
done
使用
find
和sed
:
$ find . -maxdepth 1 -type f -exec sed -i '1i #comment' {} \;
这将在当前目录中所有文件的顶部添加一行
#comment
,为了好玩,请尝试使用以下方法:
此处为文档版本:
ed -s file.txt <<EOF
1i
# comment
.
w
q
EOF
ed-s file.txt如果要向所有扩展名为“.rb”的文件添加注释:
和递归:
find . -type f -name "*.rb" -exec sed -i '1i #comment' {} \;
或更换\
使用+
在一次调用sed
中处理多个文件。
find . -maxdepth 1 -type f -name "*.rb" -exec sed -i '1i #comment' {} \;
find . -type f -name "*.rb" -exec sed -i '1i #comment' {} \;