How to create and write data into files in Linux
Table of Contents
This article explained how to create and write data into files in the Linux and Unix flavors. By using different types of commands we can create the files and we can write the data into files.
Create Empty Files
touch
using the touch command, we can create the empty file
touch /tmp/sample.txt
Create Files via vi editor command
Using the vi editor command-line tool, we can create empty or data files.
vi /tmp/sample.txt
Create and Writing data into files
Using the below command to create and write the data into a file.
Syntax command > /tmp/sample.txt Example ls > /tmp/sample.txt
Append data into the file
Syntax
command >> /tmp/sample.txt
Example
ls >> /tmp/sample.txt
Sample data writing
echo "--------------" >> /tmp/sample.txt
echo "Domain information" >> /tmp/sample.txt
host www.narayanatutorial.com >> /tmp/sample.txt
echo "--------------" >> /tmp/sample.txt
Save the output of a command to a file
The tee command reads from standard input and writes to standard output and files.
The syntax is as follows for writing data into the file:
Syntax command | tee /tmp/sample.txt Example ls | tee /tmp/sample.txt date | tee /tmp/sample.txt
Append the data into the file
Syntax command | tee -a /tmp/sample.txt Example ls | tee -a /tmp/sample.txt date | tee -a /tmp/sample.txt
Here -a append the data into a file.
Comand | Description | Overwrite existing file? |
---|---|---|
command > sample.txt | Save terminal output (standard output) to a file named sample.txt | Yes |
command >> sample.txt | Append terminal output (standard output) to a file named sample.txt | No |
command | Takes standard input from sample.txt file | N/A |
command 0 | Takes standard input from sample.txt file | N/A |
command 1> sample.txt | Puts standard output to sample.txt file | Yes |
command 1>> sample.txt | Appends standard output to sample.txt | No |
command 2> sample.txt | Puts standard error to sample.txt | Yes |
command 2>> sample.txt | Appends standard error to sample.txt file | No |
command &> sample.txt | Puts both standard error and output to sample.txt | Yes |
command > sample.txt 2>&1 | {POSIX} Puts both standard error and output to file named sample.txt | Yes |
command &>> sample.txt | Appends both standard error and output to file named sample.txt | No |
command >> sample.txt 2>&1 | {POSIX} Appends both standard error and output to file called sample.txt | No |
command | tee sample.txt | Puts standard output to sample.txt while displaying output on screen | Yes |
command | tee -a sample.txt | Appends standard output to sample.txt while displaying output on screen | No |
command |& tee sample.txt | Puts both standard output and error to sample.txt while displaying output on terminal | Yes |
command 2>&1 | tee sample.txt | {POSIX} Puts both standard output and error to file named sample.txt while displaying output on terminal | Yes |
command |& tee -a sample.txt | Append both standard output and error to file called sample.txt while displaying output on terminal | No |
command 2>&1 | tee -a sample.txt | {POSIX} Append both standard output and error to file named sample.txt while displaying output on terminal | No |
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.