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:
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:
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
OR
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)
:x
Press the Enter key.
Conclusion
You just learned essential file management commands that work on BSD family of operating systems, Linux, macOS, and other Unix-like systems.
ð§ 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Still need help? Try our forum thread here.