Can linux cat command be used for writing text to

2019-03-07 20:13发布

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...

标签: linux cat
11条回答
做个烂人
2楼-- · 2019-03-07 20:37

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

查看更多
虎瘦雄心在
3楼-- · 2019-03-07 20:44

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

查看更多
闹够了就滚
4楼-- · 2019-03-07 20:45
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.

查看更多
萌系小妹纸
5楼-- · 2019-03-07 20:45

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

cat >file.txt <<< Write something here
查看更多
手持菜刀,她持情操
6楼-- · 2019-03-07 20:46

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 
查看更多
SAY GOODBYE
7楼-- · 2019-03-07 20:47

simply pipeline echo with cat

For example

echo write something to file.txt | cat > file.txt
查看更多
登录 后发表回答