将版权信息插入多个文件

时间:2010-03-15 20:24:35

标签: unix shell scripting

如何在每个文件的最顶部插入版权信息?

4 个答案:

答案 0 :(得分:12)

#!/bin/bash
for file in *; do
  echo "Copyright" > tempfile;
  cat $file >> tempfile;
  mv tempfile $file;
done

递归解决方案(查找所有子目录中的所有.txt文件):

#!/bin/bash
for file in $(find . -type f -name \*.txt); do
  echo "Copyright" > copyright-file.txt;
  echo "" >> copyright-file.txt;
  cat $file >> copyright-file.txt;
  mv copyright-file.txt $file;
done

谨慎使用;如果文件名中存在空格,则可能会出现意外行为。

答案 1 :(得分:5)

SED

echo "Copyright" > tempfile
sed -i.bak "1i $(<tempfile)"  file*

或者shell

#!/bin/bash
shopt -s nullglob     
for file in *; do
  if [ -f "$file" ];then
    echo "Copyright" > tempfile
    cat "$file" >> tempfile;
    mv tempfile "$file";
  fi
done

如果你有bash 4.0

那么递归
#!/bin/bash
shopt -s nullglob
shopt -s globstar
for file in /path/**
do
      if [ -f "$file" ];then
        echo "Copyright" > tempfile
        cat "$file" >> tempfile;
        mv tempfile "$file";
      fi 
done

或使用find

find /path -type f  | while read -r file
do
  echo "Copyright" > tempfile
  cat "$file" >> tempfile;
  mv tempfile "$file";
done

答案 2 :(得分:0)

您可以使用这个简单的脚本

#!/bin/bash

# Usage: script.sh file

cat copyright.tpl $1 > tmp
mv $1 $1.tmp # optional
mv tmp $1

可以通过查找实用程序

管理文件列表

答案 3 :(得分:0)

在Mac OSX中工作:

#!/usr/bin/env bash

for f in `find . -iname "*.ts"`; do # just for *.ts files
  echo -e "/*\n * My Company \n *\n * Copyright © 2018 MyCompany. All rights reserved.\n *\n *\n */" > tmpfile
  cat $f >> tmpfile
  mv tmpfile $f
done