input a line at the beginning of every file in a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting input a line at the beginning of every file in a directory?
# 1  
Old 08-16-2006
input a line at the beginning of every file in a directory?

if need to input a word or anything at the beginning of every file in a directory. how do i accomplish this?


say the file is named hyperten. how do i make hyperten the first line of every file in a given directory?

thanks

Last edited by Terrible; 08-16-2006 at 02:59 AM..
Terrible
# 2  
Old 08-16-2006
cd to the destination directory and execute following script:

Code:
#!/bin/sh

find -maxdepth 1 -type f | while IFS= read vo
do
echo "some_string\
" > .tmp
cat "$vo" >> .tmp
mv .tmp "$vo"
done

# 3  
Old 08-16-2006
The -maxdepth is not available for all versions of find.
You can also use this more obscure syntax :

Code:
find . \( -type d ! -name . -prune  \) -o  -type f


Jean-Pierre.
# 4  
Old 08-16-2006
when i run that script this is what i get:

find: path-list predicate-list

this is the script i'm running. the line "/var/scripts/scramble" is what i want to put in the beginning of every file in the directory.


#!/bin/sh

find -maxdepth 1 -type f | while IFS= read vo
do
echo "/var/scripts/scramble\
" > .tmp
cat "$vo" >> .tmp
mv .tmp "$vo"
done
Terrible
# 5  
Old 08-16-2006
sorry guys. i have decided it would be best to add the line to a place other than the first line of the files.

now how do i do that? i need to input that one line script at the beginning of my files, right after the #!/bin/sh line

i thought sed could do this? wrong?
Terrible
# 6  
Old 08-16-2006
You can use sed (to insert the control/J character noted ^J do Ctrl/V Ctrl/J) :
Code:
#!/bin/sh

find . -maxdepth 1 -type f | \
while IFS= read vo
do
   sed '\_#!/bin/sh_a\^Jsome string' "$vo" > .tmp
   mv .tmp "$vo"
done

Jean-Pierre.
# 7  
Old 08-16-2006
thanks guys
Terrible
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

3. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

4. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

5. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

6. Shell Programming and Scripting

Insert output from a file to beginning of line with sed

Hi I've been trying to search but couldn't quite get the answer I was looking for. I have a a file that's like this Time, 9/1/12 0:00, 1033 0:10, 1044 ... 23:50, 1050 How do I make it so the file will be like this? 9/1/12, 0:00, 1033 9/1/12, 0:10, 1044 ... 9/1/12, 23:50, 1050 I... (4 Replies)
Discussion started by: diesel88
4 Replies

7. Shell Programming and Scripting

Append variable texts to the beginning of each line in all files in a directory

I am writing a code to append some numbers in the beginning of each line in all the files present in a directory. The number of files are really huge. The files are numbered as 1.sco, 2.sco, 4.sco (Note: 3.sco is missing). The files currently look like this: 1.sco 2 3 5 6 6 7My task is to... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

8. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

9. UNIX for Dummies Questions & Answers

write new line at the beginning of an existing file

I was trying to find out the easiest way to write new line to the beginning of an exisiting file. I am using KSH. (5 Replies)
Discussion started by: sailussr
5 Replies

10. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies
Login or Register to Ask a Question