How to append output to the end of a text file

2019-01-02 22:14发布

How do I append the output of a command to the end of a text file?

标签: bash shell
8条回答
地球回转人心会变
2楼-- · 2019-01-02 22:54

Use >> instead of > when directing output to a file:

your_command >> file_to_append_to

If file_to_append_to does not exist, it will be created.

Example:

$ echo "hello" > file
$ echo "world" >> file
$ cat file 
hello
world
查看更多
老娘就宠你
3楼-- · 2019-01-02 22:56

For example your file contains :

 1.  mangesh@001:~$ cat output.txt
    1
    2
    EOF

if u want to append at end of file then ---->remember spaces between 'text' >> 'filename'

  2. mangesh@001:~$ echo somthing to append >> output.txt|cat output.txt 
    1
    2
    EOF
    somthing to append

And to overwrite contents of file :

  3.  mangesh@001:~$ echo 'somthing new to write' > output.tx|cat output.tx
    somthing new to write
查看更多
The star\"
4楼-- · 2019-01-02 22:59

Use command >> file_to_append_to to append to a file.

For example echo "Hello" >> testFile.txt

CAUTION: if you only use a single > you will completely overwrite the contents of the file. To ensure that doesn't ever happen, you can add set -o noclobber to your .bashrc.

This ensures that if you accidentally type command > file_to_append_to to an existing file, it will alert you that the file exists already. Sample error message: file exists: testFile.txt

Thus, when you use > it will only allow you to create a new file, not overwrite an existing file.

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-02 23:05

To append a file use >>

echo "hello world"  >> read.txt   
cat read.txt     
echo "hello siva" >> read.txt   
cat read.txt

then the output should be

hello world   
hello siva

To overwrite a file use >

echo "hello tom" > read.txt
cat read.txt  

then the out put is

hello tom

查看更多
Fickle 薄情
6楼-- · 2019-01-02 23:05

I'd suggest you do two things:

  1. Use >> in your shell script to append contents to particular file. The filename can be fixed or using some pattern.
  2. Setup a hourly cronjob to trigger the shell script
查看更多
太酷不给撩
7楼-- · 2019-01-02 23:10

You can use the >> operator. This will append data from a command to the end of a text file.

To test this try running:

echo "Hi this is a test" >> textfile.txt

Do this a couple of times and then run:

cat textfile.txt

You'll see your text has been appended several times to the textfile.txt file.

查看更多
登录 后发表回答