How to save a file in Unix using command line

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

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

Comments are closed. Still have questions? Post it on our forum