How to: Create Files in Linux from a Bash Shell Prompt
Q. I'm new to Linux and installed CentOS on my Laptop. How do I create a file from bash prompt without using GUI tools?
A. Linux / UNIX offer many command line tools and text editors for creating text files. You can use vi or JOE text editor. It is a terminal-based text editor for Linux/Unix systems, available under the GPL. It is designed to be easy to use.
Create a Text File using cat command
To create a text file called foo.txt, enter:
$ cat > foo.txt
Output:
This is a test.
Hello world!
press CTRL+D to save file
To display file contents, type
$ cat foot.txt
Create a Text File using joe text editor
JOE is text editor. To create a file called foo.txt, type:
$ joe -help foo.txt
You will see help menu on screen. Next type something. To save the file and leave joe, by typing ^KX (press CTRL+K+X).
Create a Text File using vi / vim text editor
vi / vim is another text editor. To create a file called bar.txt, type:
$ vi bar.txt
Press 'i' to insert new text. To save the file and leave vi, type ESC+:+x (press ESC key, type : followed by x and [enter] key).
Further readings
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Linux / UNIX FAQ:
- Bash Shell Loop Over Set of Files
- How to: create a temporary file securely
- Create and change to a new directory in a single shell command
- Bash shell display all hidden dot files in a directory
- How to: Change User’s bash profile under Linux / UNIX
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!
Tags: cat_command, CentOS, command_line_tools, gui_tools, joe_command, Linux, text_editors, text_files, unix_systems, vi_command ~ Last updated on: October 16, 2007



October 18th, 2007 at 9:58 am
You can also use touch command to create empty files:
touch foobar.txt
October 18th, 2007 at 8:09 pm
Pafcio,
thanks for pointing out touch command!
October 18th, 2007 at 11:46 pm
echo ‘ This is a multiline
text file created
from the bash shell
using echo command’ > multiline.txt
October 29th, 2007 at 8:30 am
Thanks a lot! It helps me.
Thank you very much! all of you
November 16th, 2007 at 5:42 pm
Excellent, cause I just used it at work, and it worked and helped me.
Thanks
December 4th, 2007 at 3:48 pm
Detailed information but would appreciate if someone can upload the solution for cat command as in executing or running the vi program. I am writing a finction in the vi and want to execute it outside the vi on the bash shell.
Thx- Nitish Anand
December 6th, 2007 at 2:12 am
thanks guys. i do try cat, it works perfectly.
but in vi mode i’m not able to save & exit the file. i had to exit using ctrl+z & the file was not saved.
please help me. mail on nehra13@gmail.com
December 23rd, 2007 at 12:36 pm
Have u tried using:-
!wq for save and exit VI
!w to just exit.
February 24th, 2008 at 3:35 am
have two files, ‘file1′ and ‘file2′.
file1 has:
1234
4567
6789
file2 has:
abcd
efgh
ijkl
would like to combine into one like:
1234 | abcd
4567 | efgh
6789 | ijkl
Please give direction.
Thanks
March 27th, 2008 at 12:07 pm
Hi chakjoy,
Did u manage to resolve your problem? Try the folling link if thats what you meant:-
http://www.linuxsa.org.au/tips/command-pipelines.html
OR
cd /var/log && ls -al
this switches to the /var/log directory if the directory change was sucessfuly it will then list the contents of that directory.
You can also list the contents of a directory without leaving your current location..
ls -al /var/log
Would list the contents of /var/log no matter your current location in the directory tree.
You can also use | to send the output of one command to the input of another..
mount | column -t
Show mount points in a column format
ls -al | grep *.jpg
List the contents of a directory buit only show files ending in .jpg
ps aux | grep X11
List processes but only show processes containing X11 > and list.txt
Will list the contents of the /var/log directory and save it in the file list.txt
hope this helps
====================
You can chain copmmands together using &&
cd /var/log && ls -al
this swithces to the /var/log directory if the directory change was sucessfuly it will then list the contents o fhte directory.
just to add to that, you can likewise chain commands together with the || operator (OR operator, not to be confused with a pipe | )
Code:
$ cd /mnt || cd /varthis would attempt to change to the /mnt directory. If it could not it would then switch to the /var directory. If the cd /mnt command was successfull then it would stop. Similarly,
Code:
$ cd /blah || cd /homeit’s probably safe to say there is no /blah directory on your computer so typing this command would attempt to change to a non-existant directory, fail, and consequently change to /home
have fun!