add to the end of a file
Add lines to a file without replacing it, also to a file only root can write.
Steps
-
echo 'new line' >> file.txtadd a line at the end
-
command >> log.txt 2>&1add a command's output and errors
-
command | tee -a log.txtsee it and add it to the file
-
echo 'new line' | sudo tee -a /etc/some.confadd to a file only root can write
Notes
- One
>replaces the file; two,>>, add to it. sudo echo x >> /etc/filefails: the redirection is done by your shell, not by sudo.
Commands used
teeecho
Each has a page in lore: lore tee opens it.
Related recipes
- argument list too long"Argument list too long" from rm, cp or mv: too many files for one command line. Hand them over in batches.
- get a file's name, folder and extensionTake a path apart in a script: the folder, the name, the name without its extension, the extension.
- rename many filesChange the ending of many files, replace spaces in names, or number them.
- add a folder to the pathLet the shell find programs in a folder, for good, and reload the settings without logging out.