How to save a file in Unix using command line

last updated in Categories ,

I am a new Unix command-line user. I need to save a file in Unix. How do I save a text file in Unix?

UNIX operating system provides many command-line tools and text editors for creating a text file. You can use vi, vim, emacs, or joe/nano text editor. A text editor is designed to be easy to use. In short, you can use any one of the following commands to save a file in Unix:


Commands for saving files in Unix

  1. cat command
  2. Unix echo or printf command to save a text file
  3. vi text editor
  4. emacs text editor
  5. joe/nano text editor
  6. Any other console based text editor

Now you know various options.

1. Creating and saving a file using Unix echo command

Open the Terminal application and then type the following command to create a file called foo.txt, run:
echo "This is a test. Some data" > foo.txt
To append and save data to an existing file named bar.txt, run:
ls bar.txt
cat bar.txt
echo "Another data line" >> bar.txt

You just created the foo.txt and data saved it. However, how do you verify it? To examine the contents of the foo.txt type the following command at the shell prompt:
cat foo.txt
cat bar.txt

Command to save a file in Unix

2. Unix using cat command

In this example, we are going to create a file named quotes.txt. You need to enter one line of text in it, type the following at the shell prompt:
cat > quotes.txt
Next, press the [Enter] or [Return] key. Finally, type the desired text:

Twenty years from now you will be more disappointed by the things that you didn’t do than by the ones you did do.

Again, press the [Enter] or [Return] key. To stop and save the file on Linux press Control+d on a line by itself. That is press and hold down the Control key and type character d. On your screen:

How to save a file on Unix
How to create and saving a text file in Unix using cat command

3. Create and save a text file using vi / vim

vi/vim considered as an essential skill for Unix user. Hence, we are going to learn how to use vim/vi on Unix to edit files. Let us make a brand new file called file1.txt, type:
vi file1.txt
vim file.txt
Press i and type in the necessary text. For example:

"Our greatest fear should not be of failure... 
but of succeeding at things in life that don't really matter."
   -- Francis Chan

It is time to save the file with changes and exit Vim/vi. First, press the Esc key. Next, type (colon followed by character x)
Press the Enter key.


You just learned essential file management commands that work on BSD family of operating systems, Linux, macOS, and other Unix-like systems.


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at

Historical Comment Archive