Coder Perfect

Is it possible to write text to a file using the linux cat command?


Is something along these lines:

cat "Some text here." > myfile.txt

Possible? As a result, the contents of myfile.txt have been overwritten to:

Some text here.

This doesn’t work for me, but it doesn’t give me any errors either.

I’m looking for a cat-based solution (not vim/vim/emacs, etc.). Cat is shown in all online examples in connection with file inputs, not raw text…

Asked by IAmYourFaja

Solution #1

That is exactly what echo does:

echo "Some text here." > myfile.txt

Answered by Carl Norum

Solution #2

It appears that you are looking for a Here document.

cat > outfile.txt <<EOF
>some text
>to save

Answered by gbrener

Solution #3

Here’s another option:

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

Answered by stolen_leaves

Solution #4

For text file:

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

For PHP file:

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

Answered by Le Khiem

Solution #5

To change my CPU settings, I use the following code to write raw text to files. I hope this information is useful. Script:


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

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

The text “performance” is written to the two files stated earlier in the script. This sample overwrites files with old data.

To make this code executable, save it into a file (cpu and run it:

chmod +x

After that, you can use the following command to start the script:


If you don’t want to overwrite the file’s old data, replace it.

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


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

This will append your text to the end of the file without overwriting any existing information.

Answered by Arahkun

Post is based on