-1

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.

4
  • 7
    Can you try rephrasing this? I'm not exactly sure what you're trying to do.
    – gravyface
    Jan 26, 2011 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, 2011 at 21:04
  • 1
    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, 2011 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, 2011 at 19:16

3 Answers 3

1

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.

1

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.

0

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.

6
  • @Zoredache: echo...echo... Jan 26, 2011 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, 2011 at 17:40
  • i did try but it came out like this: sed: -i may not be used with stdin
    – Alex Vo
    Jan 26, 2011 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, 2011 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, 2011 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .