Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create text file and fill it using bash

Tags:

bash

unix

I need to create a text file (unless it already exists) and write a new line to the file all using bash.

I'm sure it's simple, but could anyone explain this to me?

like image 824
switz Avatar asked Jan 11 '11 21:01

switz


People also ask

How do I create a .TXT file in bash?

To create a new file, run the "cat" command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file.

How do I fill a text file in Linux?

touch fileName – Create an empty file. cat > filename – To create a new text file under Ubuntu, use the cat command followed by the redirection symbol ( > ) and the name of the file you want to create. Press Enter, then type the text and once you are done, press the CRTL+D to save the file.

How do you write data into a text file in Unix?

You can use the cat command to append data or text to a file. The cat command can also append binary data. The main purpose of the cat command is to display data on screen (stdout) or concatenate files under Linux or Unix like operating systems.


1 Answers

Creating a text file in unix can be done through a text editor (vim, emacs, gedit, etc). But what you want might be something like this

echo "insert text here" > myfile.txt 

That will put the text 'insert text here' into a file myfile.txt. To verify that this worked use the command 'cat'.

cat myfile.txt 

If you want to append to a file use this

echo "append this text" >> myfile.txt 
like image 105
ostler.c Avatar answered Oct 13 '22 17:10

ostler.c