Can linux cat command be used for writing text to file?

后端 未结 12 1136
南方客
南方客 2021-01-29 19:03

Is something like this:

cat \"Some text here.\" > myfile.txt

Possible? Such that the contents of myfile.txt would now be overwr

相关标签:
12条回答
  • 2021-01-29 19:42
    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.

    0 讨论(0)
  • 2021-01-29 19:42

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

    cat >file.txt <<< Write something here
    
    0 讨论(0)
  • 2021-01-29 19:43

    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
    
    0 讨论(0)
  • 2021-01-29 19:43

    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.

    0 讨论(0)
  • 2021-01-29 19:50

    That's what echo does:

    echo "Some text here." > myfile.txt
    
    0 讨论(0)
  • 2021-01-29 19:50

    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.

    0 讨论(0)
提交回复
热议问题