Introduction – A file is nothing but a container in a Linux based system for storing information. For example, music stored in a file named foo.mp4. Similarly, my cat’s picture stored in kitten.jpg and so on. This page shows various methods to create a file in Linux using the terminal window.
How to create a file in Linux from terminal window?
- Create an empty text file named foo.txt:
- Make a text file on Linux:
cat > filename.txt
- Add data and press CTRL+D to save the filename.txt when using cat on Linux
- Run shell command:
echo 'This is a test' > data.txt
- Append text to existing file in Linux:
echo 'yet another line' >> data.txt
Let us see some examples for creating a text files on Linux operating systems.
How to create a text file using the cat command
To create a text file named sales.txt, type the following command and then press [Enter] key:
cat > sales.txt
Now type your lines of text. For example:
Month, Profit 01/Apr/2004, $1500 01/May/2004, $1600 01/Jun/2004, $1450 01/Jul/2004, $1950 01/Aug/2004, $3950 01/Sep/2004, $2950 01/Oct/2004, $1750 01/Nov/2004, $1950 01/Dec/2004, $3250
When done and you need to save and exit, press Ctrl+D to return to the bash shell prompt. To view file use cat or more command/less command:
See also: Redirection of standard output
How to create an empty text file using the touch command
Simply type any one of the following command:
Verify that empty files are created with the help of ls command:
ls -l data.txt test.txt
Creating a file in Linux using the echo or printf
Let us create a file called quote1.txt using echo command, enter:
echo "While I thought that I was learning how to live, I have been learning how to die." > quote1.txt
OR use the printf command
printf 'Study nature, love nature, stay close to nature. It will never fail you.\n' > quote2.txt
Use the the >> instead of > to append data to existing file and to avoid overwriting files. The syntax is:
echo "There is no path to happiness. Happiness is the path." >> quote1.txt printf 'It is ridiculous to think that somebody else can make you happy or unhappy.\n' >> quote2.txt printf 'Happiness does not depend on what you have or who you are. -- Buddha\n' >> quote2.txt
How to create a file in Linux using joe text editor
JOE is text editor. To create a file called delta.txt, type:
joe -help delta.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).
How to create a text file in Linux using vi / vim text editor
The vi / vim is another text editor. To create a file called purchase.txt, type:
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).
You learned various methods that allow you to create text files in a Linux or Unix/macOS terminal window quickly.
🐧 10 comments so far... add one ↓
|Category||List of Unix and Linux commands|
|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|