Insert copyright message into multiple files - unix

How would you insert a copyright message at the very top of every file?

#!/bin/bash
for file in *; do
echo "Copyright" > tempfile;
cat $file >> tempfile;
mv tempfile $file;
done
Recursive solution (finds all .txt files in all subdirectories):
#!/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
Use caution; if spaces exist in file names you might get unexpected behaviour.

sed
echo "Copyright" > tempfile
sed -i.bak "1i $(<tempfile)" file*
Or 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
to do it recursive, if you have 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
or using find
find /path -type f | while read -r file
do
echo "Copyright" > tempfile
cat "$file" >> tempfile;
mv tempfile "$file";
done

You may use this simple script
#!/bin/bash
# Usage: script.sh file
cat copyright.tpl $1 > tmp
mv $1 $1.tmp # optional
mv tmp $1
File list may be managed via find utility

Working in 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

Related

Not able to read file content with sed command

I am trying to read the below file line by line to perform the below operations
Extract the name of the file/directory alone and assign it one variable,
Extract the permission available in the line and add comma between the permission. Then assign it to another variable,
At last applying setfacl logic as shown in the output section.
File
# file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file: /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file: /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
Code
input="bkp_23.txt"
while IFS= read -r line;
do
echo $line
file_name=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{print $1}'`
echo $file_name
file_perm=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{$1=""}{print}' | tr ' ' ',' | awk
'{sub(",","")}1'`
echo $file_perm
echo "setfacl -m "$file_perm" "$file_name" executing"
done <"$input"
Output
setfacl -m user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x /disk1/script_1/
setfacl -m user::rw-,group::r--,other::r-- /disk1/script_1//hello.txt
setfacl -m user::rwx,group::r-x,other::r-x /disk1/script_1//bkp_10.txt
Error
sed: can't read # file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x: No such file or directory
$ cat input
# file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file: /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file: /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
$ while read _ _ path perms; do perms="$(echo "$perms" | tr -s ' ' ,)"; echo path="$path", perms="$perms"; done < input
path=/disk1/script_1/, perms=user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x
path=/disk1/script_1//hello.txt, perms=user::rw-,group::r--,other::r--
path=/disk1/script_1//bkp_10.txt, perms=user::rwx,group::r-x,other::r-x
Try to echo the line content along with sed logic like this
file_name=$(echo "$line" | sed 's/# file:\(.*\)/\1/g' | awk '{print $1}')
file_perm=$(echo "$line" | sed -e 's/# file:\(.*\)/\1/g' | awk '{$1=""}{print}' | tr ' ' ',' | awk '{sub(",","")}1')

Creating a .sh file through Batch script and executing .sh file through batch

Below is the part of a batch script that i have created:
{
REM ********* CONN SCRIPT CREATION ***************
echo #!/bin/sh >%conn_script%
echo >>%conn_script%
echo if [ %today% -eq 23 ] >>%conn_script%
echo then >>%conn_script%
echo **find . -maxdepth 0 -type f -mtime +0 -exec rm -rf {} \;>>%conn_script%
echo else >>%conn_script%**
echo echo Files are not from previous month >>%conn_script%
echo fi >>%conn_script%
type %conn_script%
::echo bye >>%conn_script%
echo The sftp_script is:
echo "command: call %executor%\RUN\plink -ssh %host% -batch -l %user% -pw ********** -m %conn_script%"
call %executor%\RUN\plink -ssh %host% -batch -l %user% -pw %password% -m %conn_script% >%logfile%
}
I have created a batch script that is creating a .sh file. That sh file is deleting files from a unix server. When batch script is executing sh file it is getting error "find: bad option -maxdepth
find: [-H | -L] path-list predicate-list" from the code which is in BOLD format.
Even i also want to append the log of deleted files in a .txt file which is in my local machine.
I have tried a lot but not able to append the log in .txt file.
Please provide yours valuable feedback for this issue.
Thanks
Have you tried /usr/xpg4/bin/find (Available in Solaris).
/usr/xpg4/bin/find . -maxdepth 0 -type f -mtime +0 | xargs rm -f

Not getting expected file result using awk

#!/bin/bash
delete_file () {
for file in processor_list.txt currnet_username.txt unique_username.txt
do
if [ -e $file ] ;then
rm $file
fi
done
}
delete_file
ps -elf > processor_list.txt ; chmod 755 processor_list.txt
awk '{print $3}' processor_list.txt > currnet_username.txt ; chmod 755 currnet_username.txt
sort -u currnet_username.txt > unique_username.txt ;chmod 755 unique_username.txt
while read line ; do
if [ -e $line.txt ] ;then
rm $line.txt
fi
grep $line processor_list.txt >$line.sh ;chmod 755 $line.sh
awk '{if($4 == "$line") print $0;}' $line.sh > ${line}1.txt ; #mv ${line}1.txt $line.txt;chmod 755 $line.txt
done < unique_username.txt
I'm a beginner of unix shell scripting. please suggested, i am not getting expected results in ${line}1.txt.
For example, I have two UID like kplus , kplustp. what is my requirement is find "kplus" string from ps -elf command and create a file as same name like kplus.txt and redirect or move the data whatever found data using grep command.
But I am getting kplus and kplustp data in kplus.txt file. I need only kplus value based on UID column from ps –elf in kplus.txt file.
This is wrong way to read variable using awk
awk '{if($4 == "$line") print $0;}' $line.sh
Use:
awk '{if($4 == var) print $0;}' var="$line" $line.sh
Or shorten to
awk '$4==var' var="$line" $line.sh
default action is {print $0} if no action is specified.
If you need to search for the text $line escape the $ in regex
awk '$4==/\$line/' $line.sh
or in text it should work directly
awk '$4=="$line"' $line.sh

Remove duplicate jars in a directory

I have a script to remove lower version jars files in a directory.
#!/bin/bash
#Script to remove lower version jar files.
for PREFIX in `ls *.jar|sed 's/-[0-9\.\0-9\.a-zA-Z]*\.jar//g'|uniq -d`; do
for FILE in `ls -r ${PREFIX}*|sed '1d'`; do
echo " $FILE"
rm $FILE
done
done
It has a bug.
I have below list of Duplicate jar files in a directory.
xyz-1.1.jar
xyz-1.1.1.jar
abc-1.6.jar
abc-1.3.jar
abc-xyz-pqr-1.9.6.jar
abc-xyz-pqr-1.9.2.jar
xyz-tom.jar
xyz-tom-20120423.jar
xyz-tom-20120410.jar
abc-toolkit-1.6-runtime-5.2.0.jar
abc-toolkit-1.6-runtime-5.0.0.jar
The bug is with xyz pattern jar files.
BUG:
Script is removing xyz-1.1.1.jar file instead of xyz-1.1.jar
Script is removing xyz-tom-20120423.jar and xyz-tom-20120410.jar files.
#!/bin/bash
if [ $# == 0 ]; then
dir='.'
elif [ $# == 1 ]; then
dir=$1
else
echo "Usage: $0 [dir]";
exit 1;
fi
for lib in `find $dir -name '*.jar'`; do
for class in `unzip -l $lib | egrep -o '[^ ]*.class$'`; do
class=`echo $class | sed s/\\\\.class// | sed s/[-.\\/$]/_/g`
existing=$( eval "echo \$CLS_${class}" )
if [ -n "$existing" ]; then echo "$lib $existing"; fi
eval CLS_${class}="\"${lib} ${existing}\""
done
done | sort | uniq -c | sort -nr
I find this code here

Unix script to delete file if it contains single line

Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it contains single line.
Something like, if 'wc -l abscde.txt' = 1 then rm abscde.txt
My system : Solaris
Here's a simple bash script:
#!/bin/bash
LINECOUNT=`wc -l abscde.txt | cut -f1 -d' '`
if [[ $LINECOUNT == 1 ]]; then
rm -f abscde.txt
fi
delifsingleline () {
if [ $(cat $1 | wc -l) = "1" ]
then
echo "Deleting $1"
echo "rm $1"
fi
}
Lightly tested on zsh. Should work on bash as well.
This is (mostly) just a reformat of Ben's answer:
wc -l $PATH | grep '^1 ' > /dev/null && rm -f $PATH

Resources