133

This is probably a complex solution.

I am looking for a simple operator like ">>", but for prepending.

I am afraid it does not exist. I'll have to do something like

 mv myfile tmp
 cat myheader tmp > myfile

Anything smarter?

codeforester
  • 28,846
  • 11
  • 78
  • 104
elmarco
  • 27,816
  • 21
  • 58
  • 68

32 Answers32

104

This still uses a temp file, but at least it is on one line:

echo "text" | cat - yourfile > /tmp/out && mv /tmp/out yourfile

Credit: BASH: Prepend A Text / Lines To a File

Steven Penny
  • 82,115
  • 47
  • 308
  • 348
Jason Navarrete
  • 7,145
  • 5
  • 24
  • 22
  • 6
    What's doing `-` after `cat`? – makbol Apr 24 '15 at 08:56
  • Is there a way to add "text" without a new line after? – chishaku Oct 28 '15 at 15:41
  • @chishaku `echo -n "text"` – Sparhawk Jan 30 '16 at 09:23
  • 3
    A warning: if `yourfile` is a symlink this will not do what you want. – dshepherd May 10 '17 at 15:05
  • Is the answer different to this: echo "text" > /tmp/out ; cat yourfile >> /tmp/out ; mv /tmp/out yourfile ?? – Richard Oct 16 '19 at 17:03
  • @makboi the `-` after `cat` is a convention used in Shell programming to mean that the input of the command should be read from `stdin`. The output of commands such as `echo` in the answer is written to the `stdout`, and written into `stdin` for the `cat` command using the pipe `|`. Think of piping as passing `stdout` of one command to the `stdin` of another, hence the name - the left hand command's output flows into the right hand command's input – Nick Bull Jan 10 '20 at 15:28
  • 1
    Sorry to be a pedant, but `&&` means this is as much of a "one-liner" as minifiying Javascript files creates a "one-liner", and you might confuse newbies who don't know this operator separates two commands. It'd be clearer written as the separated lines `echo "text" | cat - yourfile > /tmp/out` `mv /tmp/out yourfile` - maybe `sed -i '1s/^/prepend me\n/' filename` would qualify a little better for a true "one line" prepender – Nick Bull Jan 10 '20 at 15:33
31
echo '0a
your text here
.
w' | ed some_file

ed is the Standard Editor! http://www.gnu.org/fun/jokes/ed.msg.html

fluffle
  • 311
  • 3
  • 2
29

The hack below was a quick off-the-cuff answer which worked and received lots of upvotes. Then, as the question became more popular and more time passed, outraged people started reporting that it sorta worked but weird things could happen, or it just didn't work at all, so it was furiously downvoted for a time. Such fun.

The solution exploits the exact implementation of file descriptors on your system and, because implementation varies significantly between nixes, it's success is entirely system dependent, definitively non-portable, and should not be relied upon for anything even vaguely important.

Now, with all that out of the way the answer was:


Creating another file descriptor for the file (exec 3<> yourfile) thence writing to that (>&3) seems to overcome the read/write on same file dilemma. Works for me on 600K files with awk. However trying the same trick using 'cat' fails.

Passing the prependage as a variable to awk (-v TEXT="$text") overcomes the literal quotes problem which prevents doing this trick with 'sed'.

#!/bin/bash
text="Hello world
What's up?"

exec 3<> yourfile && awk -v TEXT="$text" 'BEGIN {print TEXT}{print}' yourfile >&3
John Mee
  • 44,003
  • 31
  • 133
  • 171
  • 3
    WARNING: check the output to make sure nothing has changed but the first line. I used this solution, and although it seemed to work, I noticed that some new lines seemed to be added. I don't understand where these came from, so I cannot be sure that this solution really has a problem, but beware. – conradlee Mar 18 '10 at 15:58
  • 1
    Note in the bash example above that the double quotes span over the carriage return in order to demonstrate prepending multiple lines. Check your shell and text editor are being cooperative. \r\n comes to mind. – John Mee Mar 19 '10 at 05:05
  • 4
    Use this with caution! See the following comment for why: http://stackoverflow.com/questions/54365/prepend-to-a-file-one-liner-shell/2754039#2754039 – Alex Dec 30 '10 at 13:45
  • 3
    If it's now unreliable, how about updating your answer with a better solution? – zakdances Oct 25 '13 at 02:50
  • A _hack_ is useful _if and only if_ it is known precisely _why_ it works and, therefore, _under what carefully observed constraints_. Your disclaimer notwithstanding, your hack doesn't meet these criteria ("entirely system dependent", "seems to overcome", "works for me"). If we take your disclaimer seriously, _no one_ should use your hack - and I agree. So what are we left with? A noisy distraction. I see two options: (a) delete your answer, or (b) turn it into a cautionary tale that explains why - tempting as it may be - this approach _cannot_ work _generally_. – mklement0 May 17 '17 at 01:43
20

John Mee: your method is not guaranteed to work, and will probably fail if you prepend more than 4096 byte of stuff (at least that's what happens with gnu awk, but I suppose other implementations will have similar constraints). Not only will it fail in that case, but it will enter an endless loop where it will read its own output, thereby making the file grow until all the available space is filled.

Try it for yourself:

exec 3<>myfile && awk 'BEGIN{for(i=1;i<=1100;i++)print i}{print}' myfile >&3

(warning: kill it after a while or it will fill the filesystem)

Moreover, it's very dangerous to edit files that way, and it's very bad advice, as if something happens while the file is being edited (crash, disk full) you're almost guaranteed to be left with the file in an inconsistent state.

palacsint
  • 26,486
  • 10
  • 75
  • 100
anonymous
  • 209
  • 2
  • 2
  • 6
    This should probably be a comment, not a separate answer. – lkraav Aug 13 '11 at 09:50
  • 11
    @Ikraav: maybe, but the "comment" is very long and elaborate (and useful), it will not look good and perhaps doesn't fit with StackOverflow's limited comment capacity anyway. – Hendy Irawan Oct 17 '11 at 09:45
18

Not possible without a temp file, but here goes a oneliner

{ echo foo; cat oldfile; } > newfile && mv newfile oldfile

You can use other tools such as ed or perl to do it without temp files.

Vinko Vrsalovic
  • 244,143
  • 49
  • 315
  • 361
16

If you need this on computers you control, install the package "moreutils" and use "sponge". Then you can do:

cat header myfile | sponge myfile
user2227573
  • 161
  • 1
  • 2
  • 1
    This worked nicely for me when combined with @Vinko Vrsalovic's answer: `{ echo "prepended text"; cat myfile } | sponge myfile` – Jesse Hallett Jan 24 '20 at 17:32
16

It may be worth noting that it often is a good idea to safely generate the temporary file using a utility like mktemp, at least if the script will ever be executed with root privileges. You could for example do the following (again in bash):

(tmpfile=`mktemp` && { echo "prepended text" | cat - yourfile > $tmpfile && mv $tmpfile yourfile; } )
ehdr
  • 814
  • 2
  • 10
  • 19
13

Using a bash heredoc you can avoid the need for a tmp file:

cat <<-EOF > myfile
  $(echo this is prepended)
  $(cat myfile)
EOF

This works because $(cat myfile) is evaluated when the bash script is evaluated, before the cat with redirect is executed.

Eric Woodruff
  • 5,907
  • 3
  • 31
  • 32
9

assuming that the file you want to edit is my.txt

$cat my.txt    
this is the regular file

And the file you want to prepend is header

$ cat header
this is the header

Be sure to have a final blank line in the header file.
Now you can prepend it with

$cat header <(cat my.txt) > my.txt

You end up with

$ cat my.txt
this is the header
this is the regular file

As far as I know this only works in 'bash'.

cb0
  • 7,631
  • 9
  • 49
  • 76
  • why does this work? Do the parentheses cause the middle cat to execute in a separate shell and dump the entire file at once? – Catskul Oct 26 '10 at 19:06
  • I think the – cb0 Nov 09 '10 at 15:08
  • Someone once showed me what you can do with the " – cb0 Nov 09 '10 at 15:11
  • 1
    Didn't work for me. Don't know why. I'm using GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14), I'm on OS X Yosemite. I ended up with two lines of `this is the header` in my.txt. Even after I updated Bash to `4.3.42(1)-release` I get the same result. – Kohányi Róbert Nov 19 '15 at 16:51
  • 1
    Bash doesn't create a temporary file, it creates a FIFO (pipe) to which the command in the [process substitution (` – mklement0 May 15 '17 at 04:42
9

When you start trying to do things that become difficult in shell-script, I would strongly suggest looking into rewriting the script in a "proper" scripting language (Python/Perl/Ruby/etc)

As for prepending a line to a file, it's not possible to do this via piping, as when you do anything like cat blah.txt | grep something > blah.txt, it inadvertently blanks the file. There is a small utility command called sponge you can install (you do cat blah.txt | grep something | sponge blah.txt and it buffers the contents of the file, then writes it to the file). It is similar to a temp file but you dont have to do that explicitly. but I would say that's a "worse" requirement than, say, Perl.

There may be a way to do it via awk, or similar, but if you have to use shell-script, I think a temp file is by far the easiest(/only?) way..

balki
  • 22,482
  • 26
  • 85
  • 135
dbr
  • 153,498
  • 65
  • 266
  • 333
8

Like Daniel Velkov suggests, use tee.
To me, that's simple smart solution:

{ echo foo; cat bar; } | tee bar > /dev/null
Max Tsepkov
  • 446
  • 6
  • 15
7

EDIT: This is broken. See Weird behavior when prepending to a file with cat and tee

The workaround to the overwrite problem is using tee:

cat header main | tee main > /dev/null
Community
  • 1
  • 1
Daniel
  • 24,697
  • 12
  • 57
  • 88
  • Hung for a while. Ended with "tee: main: No space left on device". main was several GBs, although both original text files were just a few KB. – Marcos Aug 14 '14 at 09:10
  • 4
    If the solution you posted is broken (and in fact fills up users harddrives), do the community a favor and delete your answer. – aioobe Sep 26 '14 at 09:06
  • 1
    Can you point me to a guideline which says that wrong answers should be deleted? – Daniel Sep 26 '14 at 17:51
4
sed -i -e "1s/^/new first line\n/" old_file.txt
Steven Penny
  • 82,115
  • 47
  • 308
  • 348
shixilun
  • 69
  • 1
3

Mostly for fun/shell golf, but

ex -c '0r myheader|x' myfile

will do the trick, and there are no pipelines or redirections. Of course, vi/ex isn't really for noninteractive use, so vi will flash up briefly.

benjwadams
  • 1,208
  • 10
  • 16
  • Best recipe from my point of view as it uses an incumbent command and does that in a straightforward way. – Diego May 25 '16 at 19:36
3

The one which I use. This one allows you to specify order, extra chars, etc in the way you like it:

echo -e "TEXTFIRSt\n$(< header)\n$(< my.txt)" > my.txt

P.S: only it's not working if files contains text with backslash, cause it gets interpreted as escape characters

nemisj
  • 9,598
  • 1
  • 22
  • 23
2
sed -i -e '1rmyheader' -e '1{h;d}' -e '2{x;G}' myfile
weakish
  • 23,766
  • 4
  • 44
  • 54
2

With $( command ) you can write the output of a command into a variable. So I did it in three commands in one line and no temp file.

originalContent=$(cat targetfile) && echo "text to prepend" > targetfile && echo "$originalContent" >> targetfile
JuSchu
  • 1,350
  • 1
  • 10
  • 12
2

A variant on cb0's solution for "no temp file" to prepend fixed text:

echo "text to prepend" | cat - file_to_be_modified | ( cat > file_to_be_modified ) 

Again this relies on sub-shell execution - the (..) - to avoid the cat refusing to have the same file for input and output.

Note: Liked this solution. However, in my Mac the original file is lost (thought it shouldn't but it does). That could be fixed by writing your solution as: echo "text to prepend" | cat - file_to_be_modified | cat > tmp_file; mv tmp_file file_to_be_modified

2

WARNING: this needs a bit more work to meet the OP's needs.

There should be a way to make the sed approach by @shixilun work despite his misgivings. There must be a bash command to escape whitespace when reading a file into a sed substitute string (e.g. replace newline characters with '\n'. Shell commands vis and cat can deal with nonprintable characters, but not whitespace, so this won't solve the OP's problem:

sed -i -e "1s/^/$(cat file_with_header.txt)/" file_to_be_prepended.txt

fails due to the raw newlines in the substitute script, which need to be prepended with a line continuation character () and perhaps followed by an &, to keep the shell and sed happy, like this SO answer

sed has a size limit of 40K for non-global search-replace commands (no trailing /g after the pattern) so would likely avoid the scary buffer overrun problems of awk that anonymous warned of.

Community
  • 1
  • 1
hobs
  • 15,252
  • 8
  • 75
  • 93
2

Why not simply use the ed command (as already suggested by fluffle here)?

ed reads the whole file into memory and automatically performs an in-place file edit!

So, if your file is not that huge ...

# cf. "Editing files with the ed text editor from scripts.",
# http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed

prepend() {
   printf '%s\n' H 1i "${1}" . wq | ed -s "${2}"
}

echo 'Hello, world!' > myfile
prepend 'line to prepend' myfile

Yet another workaround would be using open file handles as suggested by Jürgen Hötzel in Redirect output from sed 's/c/d/' myFile to myFile

echo cat > manipulate.txt
exec 3<manipulate.txt
# Prevent open file from being truncated:
rm manipulate.txt
sed 's/cat/dog/' <&3 > manipulate.txt

All this could be put on a single line, of course.

Community
  • 1
  • 1
torf
  • 21
  • 1
2

Here's what I discovered:

echo -e "header \n$(cat file)" >file
Aziz Shaikh
  • 15,104
  • 9
  • 55
  • 73
Hammad Akhwand
  • 700
  • 1
  • 8
  • 16
1

If you have a large file (few hundred kilobytes in my case) and access to python, this is much quicker than cat pipe solutions:

python -c 'f = "filename"; t = open(f).read(); open(f, "w").write("text to prepend " + t)'

crizCraig
  • 7,169
  • 4
  • 46
  • 50
1

A solution with printf:

new_line='the line you want to add'
target_file='/file you/want to/write to'

printf "%s\n$(cat ${target_file})" "${new_line}" > "${target_file}"

You could also do:

printf "${new_line}\n$(cat ${target_file})" > "${target_file}"

But in that case you have to be sure there aren’t any % anywhere, including the contents of target file, as that can be interpreted and screw up your results.

Alan H.
  • 15,001
  • 14
  • 74
  • 104
user137369
  • 4,260
  • 5
  • 25
  • 49
  • 2
    This seems to blow up (and truncate your file!) if you have anything in your target file that printf interprets as a formatting option. – Alan H. Aug 10 '15 at 22:51
  • `echo` seems like a safer option. `echo "my new line\n$(cat my/file.txt)" > my/file.txt` – Alan H. Aug 10 '15 at 22:58
  • @AlanH. I warn of the dangers in the answer. – user137369 Aug 10 '15 at 23:31
  • Actually, you only warned about percents in `${new_line}`, not the target file – Alan H. Aug 10 '15 at 23:53
  • Could you be more explicit? It’s a really big caveat IMO. “anywhere” doesn’t make this very clear. – Alan H. Aug 10 '15 at 23:58
  • and frankly I’m not sure why you are using printf in the last example anyway, when echo is more reliable and you aren’t actually using any string formatting options :) – Alan H. Aug 10 '15 at 23:59
  • Because echo *isn’t* reliable. Not on a Mac, anyway. It consistently fails for me, and [I’m not the only one](http://stackoverflow.com/a/2362886/1661012). The `printf` solution works and is indeed a single command. Personally I use the “echo with temporary file” solution, but this is just another one. The answer is small enough that I feel the caveat is well expressed. Either way, I also make clear the first one is better. – user137369 Aug 11 '15 at 00:12
1

You can use perl command line:

perl -i -0777 -pe 's/^/my_header/' tmp

Where -i will create an inline replacement of the file and -0777 will slurp the whole file and make ^ match only the beginning. -pe will print all the lines

Or if my_header is a file:

perl -i -0777 -pe 's/^/`cat my_header`/e' tmp

Where the /e will allow an eval of code in the substitution.

user5704481
  • 31
  • 1
  • 3
1

If you're scripting in BASH, actually, you can just issue:

cat - yourfile  /tmp/out && mv /tmp/out yourfile

That's actually in the Complex Example you yourself posted in your own question.

Tim Kennedy
  • 5,314
  • 1
  • 17
  • 15
  • An editor suggested changing this to `cat - yourfile << /tmp/out && mv /tmp/out yourfile`, however that's sufficiently different from my answer that it should be it's own answer. – Tim Kennedy Jul 19 '13 at 13:38
0

I think this is the cleanest variation of ed:

cat myheader | { echo '0a'; cat ; echo -e ".\nw";} | ed myfile

as a function:

function prepend() { { echo '0a'; cat ; echo -e ".\nw";} | ed $1; }

cat myheader | prepend myfile
Dave Butler
  • 1,248
  • 10
  • 17
0

IMHO there is no shell solution (and will never be one) that would work consistently and reliably whatever the sizes of the two files myheader and myfile. The reason is that if you want to do that without recurring to a temporary file (and without letting the shell recur silently to a temporary file, e.g. through constructs like exec 3<>myfile, piping to tee, etc.

The "real" solution you are looking for needs to fiddle with the filesystem, and so it's not available in userspace and would be platform-dependent: you're asking to modify the filesystem pointer in use by myfile to the current value of the filesystem pointer for myheader and replace in the filesystem the EOF of myheader with a chained link to the current filesystem address pointed by myfile. This is not trivial and obviously can not be done by a non-superuser, and probably not by the superuser either... Play with inodes, etc.

You can more or less fake this using loop devices, though. See for instance this SO thread.

Community
  • 1
  • 1
jaybee
  • 895
  • 4
  • 11
0
current=`cat my_file` && echo 'my_string' > my_file && echo $current >> my_file

where "my_file" is the file to prepend "my_string" to.

glenn jackman
  • 207,528
  • 33
  • 187
  • 305
vinyll
  • 9,833
  • 2
  • 40
  • 34
0

Quick and dirty, buffer everything in memory with python:

$ echo two > file
$ echo one | python -c "import sys; f=open(sys.argv[1]).read(); open(sys.argv[1],'w').write(sys.stdin.read()+f)" file
$ cat file
one
two
$ # or creating a shortcut...
$ alias prepend='python -c "import sys; f=open(sys.argv[1]).read(); open(sys.argv[1],\"w\").write(sys.stdin.read()+f)"'
$ echo zero | prepend file
$ cat file
zero
one
two
jozxyqk
  • 14,520
  • 8
  • 69
  • 153
0

I'm liking @fluffle's ed approach the best. After all, any tool's command line switches vs scripted editor commands are essentially the same thing here; not seeing a scripted editor solution "cleanliness" being any lesser or whatnot.

Here's my one-liner appended to .git/hooks/prepare-commit-msg to prepend an in-repo .gitmessage file to commit messages:

echo -e "1r $PWD/.gitmessage\n.\nw" | ed -s "$1"

Example .gitmessage:

# Commit message formatting samples:
#       runlevels: boot +consolekit -zfs-fuse
#

I'm doing 1r instead of 0r, because that will leave the empty ready-to-write line on top of the file from the original template. Don't put an empty line on top of your .gitmessage then, you will end up with two empty lines. -s suppresses diagnostic info output of ed.

In connection with going through this, I discovered that for vim-buffs it is also good to have:

[core]
        editor = vim -c ':normal gg'
Community
  • 1
  • 1
lkraav
  • 2,438
  • 3
  • 24
  • 25
0

variables, ftw?

NEWFILE=$(echo deb http://mirror.csesoc.unsw.edu.au/ubuntu/ $(lsb_release -cs) main universe restricted multiverse && cat /etc/apt/sources.list)
echo "$NEWFILE" | sudo tee /etc/apt/sources.list
Jayen
  • 4,499
  • 2
  • 34
  • 59
-2

Bah! No one cared to mention about tac.

endor@grid ~ $ tac --help
Usage: tac [OPTION]... [FILE]...
Write each FILE to standard output, last line first.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --before             attach the separator before instead of after
  -r, --regex              interpret the separator as a regular expression
  -s, --separator=STRING   use STRING as the separator instead of newline
      --help     display this help and exit
      --version  output version information and exit

Report tac bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report tac translation bugs to <http://translationproject.org/team/>