
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a New File in Linux from Bash
Before getting into the ways of creating a file using Bash, let's first understand how Linux treats its files. Linux organizes all its data into files and files are organized into directories. Further, the directories are organized into tree-like structures called the file system. When you have to work in a Linux environment, you would definitely have to spend a lot of your time working on different types of files.
There are several different ways in which one can create a file in Linux. You can create a file from the Bash Shell or you can use the Desktop File Manager to do so. In this tutorial, we will focus on different Shell commands that you can use to create a file.
You can use any of the following five commands to create a new file in Linux ?
- The "touch" command
- The "cat" command
- The Redirection operator
- The "echo" command
- The "printf" command
We will elaborate each of these commands one by one. Let's start with the touch command.
Using the "touch" command
The touch command is by far the most frequently used command for creating a new file in Linux. To create a new file, you need to run the touch command followed by the name of the file. For example,
$ touch hello.txt
It will create an empty file called "hello.txt" in the current directory. Use the ls command to verify if the file has been created or not.
Using the "cat" command
Normally we use the cat command to read the contents of a file; however, we can also use this command to create a new file. Let's see how.
To create a new file, run the cat command and then use the redirection operator > followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press Ctrl+D" to save the file.
$ cat > secondFile.txt Welcome to Tutorialspoint!
The above command will create a new file called "secondFile.txt" and save it with the content "Welcome to Tutorialspoint".
Again, use the ls command to verify if the new file has been created or not.
$ ls hello.txt newdirectory secondFile.txt
Next, use the "cat" command to see the contents of "secondFile.txt".
$ cat secondFile.txt Welcome to Tutorialspoint!
Using the Redirection Operator
You can simply use the redirection operator ">" to create a new blank file in the current directory. Run the > operator followed by the name of the file.
$ > thirdFile.txt
Now use the "ls" command again to verify ?
$ ls hello.txt newdirectory secondFile.txt thirdFile.txt
Note that the > operator overwrites the contents of a file if it is already present. For example, the following command will overwrite the contents of "secondFile.txt" because the file already exists and we know it contains the line "Welcome to Tutorialspoint!"
$ > secondFile.txt
Now use the "cat" command to check the contents of "secondFile.txt".
$ cat secondFile.txt
It will display nothing because the file is now empty.
You can use the redirection operator >> to append the contents of a file into another. For example,
$ cat hello.txt This is the first file. $ cat secondFile.txt This is the Second File.
Now we can use the following command to append the contents of "secondFile.txt" at the end of "hello.txt".
$ cat secondFile.txt >> hello.txt $ cat hello.txt This is the first file. This is the Second File.
Using the "echo" command
The echo command takes a string as argument and displays it as output. For example,
$ echo "This is the Fourth File" This is the Fourth File
We can redirect this output to a new file, such as ?
$ echo "This is the Fourth File" > fourthFile.txt
The above command will create a new file (or overwrite the file if it already exists) with the string passed as the argument to echo. Verify using the cat command ?
$ cat fourthFile.txt This is the Fourth File
If you simply want to create a blank new file, use the echo command without any argument ?
$ echo > fourthFile.txt
Using the "printf" command
The printf command works just like the echo command with the only exception that the printf command provides additional formatting options that you can use to pass a formatted string as the argument.
The following "printf" command redirects the input formatted string into a new file "fifthFile.txt". If the file already exists, then it will overwrite its contents.
$ printf "First Line.
Second Line.
" > fifthFile.txt $ cat fifthFile.txt First Line. Second Line.
Creating a File Using the Vim Editor
Besides the above-mentioned Bash commands, you can consider using the screen-oriented text editor Vim to create a file. Vim is the improved version of the vi editor which is generally considered as the de facto standard in Unix editors. You can use the vim editor to create a new file, edit an existing file, or simply read a text file.
To open the Vim editor, type "vim" at the command prompt. You will get to see a screen like the one shown below:
In the Command mode, type "vi abc.txt" and press Enter to create a new text file called "abc.txt".
Next, press "i" to enter the Insert mode to insert some data into this newly created file.
Now it's time to save the file. Press "Esc" to return to Command mode and then use the command ":wq" to save the file and quit. It will take you back to the console.
Next, on the console, type the command "vim abc.txt" to open the newly created file for reading or editing:
It will open the file "abc.txt" for reading or editing:
To learn more about the Vim editor and its various options, you can check this page.
Conclusion
In this brief tutorial, we highlighted five different ways that you can use to create a new file in Linux from the terminal. In case you don't have Linux on your system, then use Windows Subsystem for Linux (WSL) to create a Linux environment on your Windows system.