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
- cat command
- Unix echo or printf command to save a text file
- vi text editor
- emacs text editor
- joe/nano text editor
- 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:
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:
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:
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:
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.