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.
3 Answers
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.
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.
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.
-
-
@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. Jan 26, 2011 at 17:40
-
i did try but it came out like this: sed: -i may not be used with stdin– Alex VoJan 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
. 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 VoJan 27, 2011 at 19:18
cp newfile file1
is what you want?