- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to 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 filesystem. 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 various 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 article, we will focus on different Shell commands that you can use to create a file.
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.
- Related Articles
- How to Create a New Ext4 File System in Linux?
- Count lines in a file using Linux bash
- How to create a CPU spike with bash command on Linux?
- How to replace spaces in file names using bash script on Linux?
- How to create a new directory in Linux using the terminal?
- How to create a new image from a JPEG file using the imagecreatefromjpeg() function in PHP?
- How to read a Specific Line From a File in Linux?
- How to Clear BASH Command Line History in Linux?
- How to create a zip file and ignore directory structure in Linux?
- How to Create a File in the Linux/Unix system using terminal?
- Create a new empty file in Java
- How to create a new image from a WBMP file or URL using imagecreatefromwbmp() function in PHP?
- How to create a new image from a WEBP file or URL using imagecreatefromwebp() function in PHP?
- Create a file of certain size in Linux
- Array Operations in Linux bash
