2010-03-15 26 views

cevap

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

Recursive çözeltisi (bulduğu tüm alt dizinleri tüm .txt dosya):

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

tedbiri alın; dosya adlarında boşluk varsa, beklenmedik davranışlar alabilirsiniz.

+0

+1 Paul perl -e 'ile 5 bayt bunu yapıyor olacak –

+0

+1 Güzel! Araçlar havuzuma gideceğim. Merak ediyorum, bu nasıl tekrarlayıcı olur? –

+0

@Byron Whitlock: neden perl? sed döngü ile '*' ile '$ (bulabilirsiniz.)' –

0

Bunu özyinelemeli yapmak

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

kabuk

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

Ya sed

5

#!/bin/bash 

# Usage: script.sh file 

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

Dosya listesi bulmak yarar üzerinden yönetilebilir bu basit komut dosyası kullanabilir eğer basın varsa h 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 

veya Mac OSX Çalışma find

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

çok iyi. Burada teknikleri bir çok tanıtıldı. Onları sonra inceleyeceğim. teşekkür ederim. –

0

kullanarak: Onun sonraki numara için

#!/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 
İlgili konular