Hi I want to prepend text to a file. For example I want to add tasks to the beginning of a todo.txt file. I am aware of echo 'task goes here' >> todo.txt but that adds the line to the end of the file (not what I want).
- 4,541
- 3
- 18
- 6
-
3There if no efficient way to do that for large files: http://unix.stackexchange.com/questions/87772/add-lines-to-the-beginning-and-end-of-the-huge-file – Ciro Santilli OurBigBook.com Nov 22 '15 at 19:45
-
@ould you take a look at this method https://superuser.com/a/1352628/11116 – Evan Carroll Aug 27 '18 at 06:52
11 Answers
Linux :
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt
or
sed -i '1s/^/task goes here\n/' todo.txt
or
sed -i '1itask goes here' todo.txt
Mac os x :
sed -i '.bak' '1s/^/task goes here\'$'\n/g' todo.txt
or
echo -e "task goes here\n$(cat todo.txt)" > todo.txt
or
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt
- 3
- 2
- 106,229
- 19
- 167
- 187
-
10the first one works great! would you mind explaining the logic? im not particularly sure how to interpret the syntax. – user479534 Feb 18 '11 at 04:24
-
68@user8347: Pipe (`|`) the message (`echo '...'`) to `cat` which uses `-` (standard input) as the first file and `todo.txt` as the second. `cat` conCATenates multiple files. Send the output (`>`) to a file named `temp`. If there are no errors (`&&`) from `cat` then rename (`mv`) the `temp` file back to the original file (`todo.txt`). – Dennis Williamson Feb 18 '11 at 04:51
-
-
-
Instead of printing \n it may print a newline. Happened to me. Used the sed version just fine instead – itaifrenkel Jan 26 '14 at 21:37
-
1@itaifrenkel: I'd have to see what you did, but if `cat` receives a literal backslash n, it won't convert it to a newline. Something else must have done that. Instead of `cat`, try piping into `hexdump -C` to see if you're actually sending backslash and n or if it's a newline. You could also try `cat -e` to show line endings. – Dennis Williamson Jan 26 '14 at 21:58
-
2Using 2 and 3 (3 seems simpler to me) allows you to prepend text to *many* files at once. – Felix Jan 27 '14 at 15:24
-
cat can be freely used to concatenate binaries. Note how `split` and `join` do entirely different things; `cat` is the companion to `split`, not `join`. And that means, if your `cat` messed up `\n` your implementation is horribly broken. – SF. Aug 05 '14 at 10:14
-
Using the third option, I got the task in the file every other line....so I guess it put it on the first line, but didn't do so well for the rest of the file for my use case. – onaclov2000 Aug 22 '15 at 17:15
-
@onaclov2000: The second `sed` command in my answer says to insert a line of text before line 1. I'd have to see the command you used to know why it inserted it on every other line. You probably omitted the address (`1`). – Dennis Williamson Aug 22 '15 at 19:28
-
1I didn't know about this possible syntax in sed's last example, can someone tell me what is the name of this type of syntax so I can search more about it? – Kira Oct 22 '15 at 19:04
-
6@Kira: The `1` means do the next command only on line one of the file and the `i` command is insert. Look in the man page under the "Addresses" section and in the "Zero- or One- address commands" section. – Dennis Williamson Oct 23 '15 at 16:33
-
The echo line works perfect for DD-WRT firmware for a script I run. Using sed and putting in the \t for a tab, puts just a t then the next part. Where the echo line actually puts the tab in instead of the t. Thank you! – Terrance Jul 20 '16 at 20:37
-
1@Terrance: If your `sed` command looks something like this (inserting a leading tab): `'1i\tSome Text'`, you may need to double the backslash. Otherwise, your dialect of `sed` may not recognize `\t` as tab. – Dennis Williamson Jul 20 '16 at 21:07
-
@DennisWilliamson Unfortunately the `sed` doesn't recognize the `\t` as tab. I forgot to say that I had to do it as `echo -e` to make it work with the tab. – Terrance Jul 20 '16 at 21:14
-
1
-
1For what it's worth the cat version is around 3 times faster than the sed ones. At least on the test in did on a ~4GB file. – Xælias Jan 31 '20 at 15:20
-
A simpler option in my opinion is :
echo -e "task goes here\n$(cat todo.txt)" > todo.txt
This works because the command inside of $(...) is executed before todo.txt is overwritten with > todo.txt
While the other answers work fine, I find this much easier to remember because I use echo and cat every day.
EDIT: This solution is a very bad idea if there are any backslashes in todo.txt, because thanks to the -e flag echo will interpret them. Another, far easier way to get newlines into the preface string is...
echo "task goes here
$(cat todo.txt)" > todo.txt
...simply to use newlines. Sure, it isn't a one-liner anymore, but realistically it wasn't a one-liner before, either. If you're doing this inside a script, and are worried about indenting (e.g. you're executing this inside a function) there are a few workarounds to make this still fit nicely, including but not limited to:
echo 'task goes here'$'\n'"$(cat todo.txt)" > todo.txt
Also, if you care about whether a newline gets added to the end of todo.txt, don't use these. Well, except the second-to-last one. That doesn't mess with the end.
- 98
- 2
- 16
- 1,121
- 1
- 7
- 2
-
1
-
2That might work better (or at all) with double quotes instead of single… – raphink May 22 '13 at 07:25
-
6
-
2
-
printf would be a lot more consistent across platforms and should generally work more smoothly than echo -e – Peter Berg Feb 17 '18 at 20:10
-
Warning: The last one overwrites each file with the echo (or printf) statement. – xizdaqrian Mar 29 '18 at 05:31
-
I'm not sure this works with large files, bash string size is probably limited to a few MB. – jesjimher May 10 '18 at 11:06
-
-
a little warning here: it can be dangerous to read and directly overwrite a file this way... for one thing, what if the commands happen to operate like this: read position 10000000 to 10100000 and write to the file, but now that position 10000000 is already of the new file? Even if that won't happen, what if the file system becomes full during this, so the new file is being written, but disk full, and fail, and now where is that old file for you to redo this operation when you clean up some disk space? You may lose this file or (part of it) – nonopolarity Dec 29 '22 at 12:27
The moreutils have a nice tool called sponge:
echo "task goes here" | cat - todo.txt | sponge todo.txt
It'll "soak up" STDIN and then write to the file, which means you don't have to worry about temporary files and moving them around.
You can get moreutils with many Linux distros, through apt-get install moreutils, or on OS X using Homebrew, with brew install moreutils.
- 223,558
- 70
- 607
- 592
-
-
I would use `tee` instead of sponge which come by default on most distro `echo "task goes here" | cat - todo.txt | tee todo.txt` – Angel115 Apr 28 '20 at 17:12
-
1
-
@stevenpenny Good point. Now I remember why I didn't suggest it originally. I came back to this answer not noticing that the output file name was the same as the input, which is why you need sponge to buffer it all before writing it back out. – slhck May 02 '20 at 12:07
You can use the POSIX tool ex:
ex a.txt <<eof
1 insert
Sunday
.
xit
eof
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
- 1
- 24
- 120
- 163
You can create a new, temporary file.
echo "new task" > new_todo.txt
cat todo.txt >> new_todo.txt
rm todo.txt
mv new_todo.txt todo.txt
You might also use sed or awk. But basically the same thing happens.
- 8,013
- 1
- 31
- 34
-
3Say you're out of disk space so that `new_todo.txt` gets written only partially. Your solution appears to lose the original file. – NPE Feb 17 '11 at 10:31
-
1
-
3@Keith Someone working on a VM who didn't expect to need a particularly large virtual drive. Or someone moving a large file. In any case, the real argument against this is directory permissions; if you don't have permission to create new files in the given directory, the only command that will successfully execute in your script is the `rm` of the original file. – Parthian Shot Jul 01 '14 at 20:23
If the text file is small enough to fit in memory, you don't have to create a temporary file to replace it with. You can load it all into memory and write it back out to the file.
echo "$(echo 'task goes here' | cat - todo.txt)" > todo.txt
It's impossible to add lines to the beginning of the file without over writing the whole file.
- 742
- 8
- 13
-
Just to ask the obvious question: *Where's the character limit of shell variables?* – nixda Jan 09 '13 at 22:59
-
As far as I'm aware, it's only limited by the amount of memory available. I've filled up variables well over 100MB into memory. `text=$(cat file)`. Be careful to only use text though, because shell variables aren't binary clean http://mywiki.wooledge.org/BashFAQ/058 – Rucent88 Jan 14 '13 at 01:48
You cannot insert content at the beginning of a file. The only thing you can do is either replace existing content or append bytes after the current end of file.
Any solution to your question then requires a temporary file (or buffer) to be created (on memory or on disk) which will eventually overwrite the original file.
Beware of not losing data by preserving the original file while creating the new one, should the file system happen to be full during the process. eg:
cat <(echo task go there) todo.txt > todo.txt.new && mv todo.txt.new todo.txt
-
Downvoters are welcome to explain their motivation. None of the remaining answers, including the accepted one, do contradict anything in my reply. – jlliagre Apr 12 '16 at 13:13
-
This is difficult to parse as the < ... > look like brackets, which I assume they are not. A space between the < and the ( might help? – dumbledad Jun 01 '18 at 09:20
-
This is not working for me. `echo HOME=\"/g/Users/timregan/\" | cat - 'F:\Program Files\Git\etc\profile'` works but `cat
– dumbledad Jun 01 '18 at 09:35 -
@dumbledad You are overthinking my reply. There is nothing for you to parse in it. A space between the `<` and the `(` would break the syntax. Try `cat <(echo HOME=\"/g/Users/timregan/\") 'F:\Program Files\Git\etc\profile'` – jlliagre Jun 02 '18 at 01:11
Final answer
cat <<< "prepended text
$(cat test.txt)" > test.txt
Context
I wasn't too satisfied with the answers as they felt like too much typing. I liked John Alberts his answer but couldn't stand to type -e. Unfortunately, I accidentally read over John Alberts his echo 2 liner as well (significantly reducing the value of this answer and me 30 minutes playing around, but oh well, it happens).
In any case, I was focused on finding something that meant you only needed to type the filename and text you want to prepend.
Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.
So I tried an approach with herestrings since in the right context they reduce cognitive strain (i.e. typing < 3 times doesn't require too much thinking power).
I created a file test.txt with the word "monkeys".
And I typed:
cat <<< "prepend
> $(< test.txt)"
Output:
prepend
monkeys
A bit of clarification:
You need to manually press enter yourself.
On the second line the > is from the shell itself, you don't need to type that.
Notes:
(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.
(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:
The command substitution
$(cat file)can be replaced by the equivalent but faster$(< file).
So you could also do:
cat <<< "prepend
> $(cat test.txt)"
More typing, but I admit a bit less cognitive strain since cat is being typed twice and is more well-known than the trick of the Bash Reference Manual.
- 181
- 1
- 8
GitBash + Windows10 + Multline:
Here is a version that lets you use multi-line strings.
##############################################
## This section for demo purpose only, ##
## So you can save entire file as ##
## whatever.sh and run it. ##
## ##
##############################################
> MY_TARGET_FILE.txt ##Make Or Clear File
echo "[STARTER_CONTENT]" >> MY_TARGET_FILE.txt
##############################################
## Below is main code:
##################################################
TARGET_FILE_VARIABLE="MY_TARGET_FILE.txt"
ADD_TO_HEAD_VARIABLE=$(cat << "HEREDOC_HEAD_TEXT"
//| +-------------------------------------+ |//
//| | | |//
//| | MESSAGE_FOR_HEAD_OF_FILE | |//
//| | | |//
//| +-------------------------------------+ |//
HEREDOC_HEAD_TEXT
)
##################################################
TAR=$TARGET_FILE_VARIABLE ##
TEX=$ADD_TO_HEAD_VARIABLE ##
echo "$TEX" | cat - $TAR > TEMP && mv TEMP $TAR ##
##################################################
## Expected contents of MY_TARGET_FILE.txt :
## //| +-------------------------------------+ |//
## //| | | |//
## //| | MESSAGE_FOR_HEAD_OF_FILE | |//
## //| | | |//
## //| +-------------------------------------+ |//
## [STARTER_CONTENT]
- 357
- 3
- 6
Maybe this, in two commands, is easier to understand. Say that we want to prepend to hello.c from hello.h.
$ cat hello.c >> hello.h
$ mv hello.h hello.c
Before:
% cat hello.c
int main() {
printf("Hello, World!");
return 0;
}
% cat hello.h
#include <stdio.h
After:
% cat hello.c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
- 1,021
- 9
- 27
- 52