Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have been looking for some example of how to update a file that is already exist in bash. so far i haven found any. I'm using parameter as my first start for the existent, copy file to a different directory. can you guys help me please, show me some example of anyway to update a file.

thanks ahead guys..

share|improve this question
6  
Can you try rephrasing this? I'm not exactly sure what you're trying to do. –  gravyface Jan 26 '11 at 17:01
    
for instant, a file1 have data A,b,c. now file1 is re-written. and you want to update it. i think update and replace is the same right? i'm not sure if the word update is the correct word. it is like the file is old and you want to replace the old file with new one. does it has to have the same file name? i don't think it matter right? –  Alex Vo Jan 26 '11 at 21:04
    
Are you asking how to copy a new file over the top of an existing file? Perhaps cp newfile file1 is what you want? –  Zoredache Jan 26 '11 at 23:22
    
ok...it like this..File 1 have a,b, and c. later one File 1 has more feature which is d and e. i want to update the old file 1 so it have all feature which is a,b,c,d,and e. it like i have a older version and i want to up date it to a newer version. kinda like overwrite a file. –  Alex Vo Jan 27 '11 at 19:16
add comment

3 Answers 3

You can use the append operator ">>" to add to the end of a file.

For example: Echo "Job completed successfully." >> /var/log/backuplog

Will add "Job completed successfully." to the end of backuplog.

Hope that helps.

share|improve this answer
add comment

What do you mean by "updating a file"? Do you want to append text to it? This would be done with something like echo "My text" >> filename, which prints the text "My text" to the end of the file filename. If you just want to get the time of the last change updated, you can use touch filename. Many ways, so please tell us what you want to do.

share|improve this answer
add comment

If you aren't looking to append to a file, and instead you are looking to modify the content you may want to take a look at sed. A command like sed -i -e 's/foo/bar/' would replace all instances of foo i the file with bar. Far more complex edits are possible, sed is extremely versatile.

share|improve this answer
    
@Zoredache: echo...echo... –  Dennis Williamson Jan 26 '11 at 17:38
    
@Dennis Williamson, Well isn't that neat, I have never seen that before. I don't think I did anything that should have caused a dup post. –  Zoredache Jan 26 '11 at 17:40
    
i did try but it came out like this: sed: -i may not be used with stdin –  Alex Vo Jan 26 '11 at 23:04
    
@Alex Vo, the -i is meant for modifying a file in-place. If you want to do something with STDIN, then get rid of the -i. –  Zoredache Jan 26 '11 at 23:21
    
is sed and awk has something to do with overwrite a file. like i have a older version and i want to up date it to a newer version. kinda like overwrite a file. like File 1 have a,b, and c. later one File 1 has more feature which is d and e. i want to update the old file 1 so it have all feature which is a,b,c,d,and e. –  Alex Vo Jan 27 '11 at 19:18
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.