227

Is something like this:

cat "Some text here." > myfile.txt

Possible? Such that the contents of myfile.txt would now be overwritten to:

Some text here.

This doesn't work for me, but also doesn't throw any errors.

Specifically interested in a cat-based solution (not vim/vi/emacs, etc.). All examples online show cat used in conjunction with file inputs, not raw text...

0

14 Answers 14

280

That's what echo does:

echo "Some text here." > myfile.txt
2
  • 33
    Bonus: echo "Some text here." >> myfile.txt to append to the end o the file
    – Jim Aho
    Commented Nov 17, 2018 at 19:35
  • 9
    If you need to use double quotes in your text, encompass the whole thing in single quotes. This is useful for .json and the likes, e.g. echo '{"info1": "123456"}' > info.json
    – bkd
    Commented Nov 22, 2018 at 14:11
243

Sounds like you're looking for a Here document

cat > outfile.txt <<EOF
>some text
>to save
>EOF
4
  • On linux Kernel 2.6.32 centos 6 I had to omit the > characters to get expected output.
    – ecoe
    Commented Jun 4, 2018 at 16:45
  • 5
    The > characters represent the default value of $PS2; they show up automatically, and are not meant to be typed. If you have a different value for $PS2, that will show up instead.
    – gbrener
    Commented Jun 5, 2018 at 15:13
  • Whats the <<EOF syntax do? Commented May 2, 2022 at 6:33
  • <<EOF is starting the here doc and indicating the end string. The linked wikipedia page describes it.
    – Carl Norum
    Commented Sep 4, 2022 at 16:20
81

Here's another way -

cat > outfile.txt
>Enter text
>to save press ctrl-d
0
37

For text file:

cat > output.txt <<EOF
some text
some lines
EOF

For PHP file:

cat > test.php <<PHP
<?php
echo "Test";
echo \$var;
?>
PHP
14

I use the following code to write raw text to files, to update my CPU-settings. Hope this helps out! Script:

#!/bin/sh

cat > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor <<EOF
performance
EOF

cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF
performance
EOF

This writes the text "performance" to the two files mentioned in the script above. This example overwrite old data in files.

This code is saved as a file (cpu_update.sh) and to make it executable run:

chmod +x cpu_update.sh

After that, you can run the script with:

./cpu_update.sh

IF you do not want to overwrite the old data in the file, switch out

cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF

with

cat >> /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF

This will append your text to the end of the file without removing what other data already is in the file.

11
cat > filename.txt

enter the text until EOF for save the text use : ctrl+d

if you want to read that .txt file use

cat filename.txt

and one thing .txt is not mandatory, its for your reference.

6

You can do it like this too:

user@host: $ cat<<EOF > file.txt
$ > 1 line
$ > other line
$ > n line
$ > EOF
user@host: $ _

I believe there is a lot of ways to use it.

6

Write multi-line text with environment variables using echo:

echo -e "
Home Directory: $HOME \n
hello world 1 \n
hello world 2 \n
line n... \n
" > file.txt 
6

The Solution to your problem is :

echo " Some Text Goes Here " > filename.txt

But you can use cat command if you want to redirect the output of a file to some other file or if you want to append the output of a file to another file :

cat filename > newfile -- To redirect output of filename to newfile

cat filename >> newfile -- To append the output of filename to newfile

5

Another way to write text to file using cat would be something like this

cat >file.txt <<< Write something here
1
  • This command is really useful when you're trying to write an output of a command into a file. For instance cat > docker-inspect.txt <<< docker inspect image` ` Commented Nov 11, 2019 at 12:20
4

simply pipeline echo with cat

For example

echo write something to file.txt | cat > file.txt
2

cat can also be used following a | to write to a file, i.e. pipe feeds cat a stream of data

0

For my Jenkins on MacOS 11.4

{
   echo 'line 1' $var1
   echo 'line 2'
   echo 'result' $var5

} > ${destination_file}

I had tried many times by cat syntax, ex: cat > ${dest_file} <<EOF ... EOF, and got error syntax error: unexpected end of file. So, I use echo syntax to output multi-lines text to a file.

References:

0

Since nobody else answered the original question. Yes. cat can be used to write text to a file without a here doc.

cat - >file.txt
This is the world's worst
multiline text editor.
^d

Not the answer you're looking for? Browse other questions tagged or ask your own question.