2010-03-15 5 views

답변

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 

주의하십시오; 파일 이름에 공백이 있으면 예기치 않은 동작이 발생할 수 있습니다.

+0

+1을, 바울은 펄 -e '와 5 바이트에서 일을하고있을 것 ' –

+0

+1 좋은! 도구 저장소에 들어갑니다. 나는 재귀 적으로 어떻게 만들 것인가? –

+0

@Byron Whitlock : 왜 perl입니까? sed는 재귀를 위해서 –

0

당신은 재귀 할

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

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

또는를 나오지

5

#!/bin/bash 

# Usage: script.sh file 

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

파일 목록 찾기 유틸리티를 통해 관리 할 수도 있습니다 간단한 스크립트를 사용할 수있다 , 만약 당신이 bas 시간 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 

또는 Mac OSX에서 작업 find

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

매우 잘되었습니다. 당신은 여기에 많은 기술을 도입했습니다. 나는 그들을 나중에 공부할 것이다. 고맙습니다. –

0

를 사용하여 : 그의 다음 해트트릭을

#!/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